我正在尝试向我的聊天应用程序添加加密。但这样做时遇到问题,我不能
将数据添加到 firebase ,它显示错误 Unhandled Exception: Invalid argument: Instance of 'Encrypted'
。我如何发送从客户端加密的消息并将其发送到 firebase,然后接收消息,然后解密它。因为我使用库加密 5.0.0 来解密它,所以它只接受 Encrypted
作为参数。
这里是加密和解密的类。
import 'package:encrypt/encrypt.dart' as encrypt;
class MyEncryptionDecryption{
///for encrption
static final key = encrypt.Key.fromLength(32);
static final iv = encrypt.IV.fromLength(16);
static final encrypter = encrypt.Encrypter(encrypt.AES(key));
static encryptAES(message ){
final encrypted = encrypter.encrypt(message ,iv: iv);
print(encrypted.base16);
print(encrypted.base64);
return encrypted;
}
static decryptedAES(message){
return encrypter.decrypt(message,iv: iv);
}
}
使用它向 firebase 发送消息,如下所示..
var aes = MyEncryptionDecryption.encryptAES(message);
Map<String, dynamic> messageInfoMap = {
"message": aes,
"sendBy": myUserName,
"ts": lastMessageTs,
"imgUrl": myProfilePic
};
DatabaseMethods()
.addMessage(chatRoomId, messageId, messageInfoMap)
.then((value) {
Map<String, dynamic> lastMessageInfoMap = {
"lastMessage": aes,
"lastMessageSendTs": lastMessageTs,
"lastMessageSendBy": myUserName,
"readStatus" : false ,
"count" : count,
"show" : true,
};
DatabaseMethods().updateLastMessageSend(chatRoomId, lastMessageInfoMap);
这是数据库函数..
Future addMessage(String chatRoomId , String messageId,messageInfoMap) async {
return await FirebaseFirestore.instance.
collection("chatrooms").
doc(chatRoomId).collection('chats').doc(messageId).set(messageInfoMap);
}
updateLastMessageSend(String chatRoomId, lastMessageInfoMap){
return FirebaseFirestore
.instance.collection('chatrooms').doc(chatRoomId)
.update(lastMessageInfoMap);
}
最佳答案
加密
是一个对象。您可以使用 the fields defined in the API of Encrypted
检索实际值,例如属性 bytes
获取对象的字节表示形式,或者 base64
如果您需要密文作为文本。
当然,您可以使用其中一个构造函数恢复为 Encrypted
:仅使用用于字节的 Encrypted
构造函数或用于文本的 fromBase64
构造函数。
关于firebase - 未处理的异常 : Invalid argument: Instance of 'Encrypted' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68572427/