firebase - 未处理的异常 : Invalid argument: Instance of 'Encrypted'

标签 firebase flutter dart encryption google-cloud-firestore

我正在尝试向我的聊天应用程序添加加密。但这样做时遇到问题,我不能 将数据添加到 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/

相关文章:

android - 如何在 Android Studio 模块中更改 Firebase 项目

flutter - 如何根据百分比指示器更改文本

flutter - 为什么我的通用扩展方法需要动态而不是具体类型?

reactjs - Firebase在 Electron 应用程序中默默失败

javascript - 使用 Firebase 进行 Shopify 身份验证?

ios - Swift & Firebase - Cloud firestore 可扩展吗?

flutter - 按startWith分组列表

flutter 错误: A value of type 'OutletListModel' can't be assigned to a variable of type 'List<OutletListModel>?'

DART lang,使用 map 列表,如何?

flutter - Flutter无法加载 Assets :但目录不存在