无法解密数据扑

问题描述 投票:0回答:1

我想向我的应用程序添加自动登录功能。它还应该包括加密/解密功能。

我的代码如下:

这是我获取参数并加密它们的地方。

final FirebaseAuth auth = FirebaseAuth.instance;
                        final User? user = auth.currentUser;
                    
                        final storage =  FlutterSecureStorage();
                        SharedPreferences pref = await SharedPreferences.getInstance();
                         final userId = user!.uid;
                         final newDateString = "1kr95ye3mcfgjt124p-54";  
                         final subUserId = userId.substring(0, 11);  
                         final key = subUserId + newDateString;
                        // ignore: prefer_typing_uninitialized_variables
                         final encryptMail = EncryptData.encryptAES(email, key);
                         final encryptPassword = EncryptData.encryptAES(sifre, key); 
                         await storage.write(key: 'mail2', value: encryptMail!.base64);
                         await storage.write(key: 'password2', value: encryptPassword!.base64);
                         await storage.write(key: 'loginState2', value: true.toString());
                        pref.setString('mail', encryptMail.base64);
                        pref.setString('password', encryptPassword.base64);
                        pref.setBool('loginState', true);

import 'package:encrypt/encrypt.dart';

class EncryptData {
//for AES Algorithms

  static Encrypted? encrypted;
  static var decrypted;

  static encryptAES(plainText, String userKey) {
    print(plainText);
    final key = Key.fromUtf8(userKey);
    final iv = IV.fromLength(16);
    final encrypter = Encrypter(AES(key));
    encrypted = encrypter.encrypt(plainText, iv: iv);
    print(encrypted!.base64);
    return encrypted;
  }

  static decryptAES(encrypted, String userKey) {
    print("decrpyt------------------");
    final key = Key.fromUtf8(userKey);
    final iv = IV.fromLength(16);
    final encrypter = Encrypter(AES(key));
    decrypted = encrypter.decrypt64(encrypted!, iv: iv);
    print(decrypted);
    print("decrpyt------------------");
    return decrypted.toString();
  }
}

我对此有一个错误:

enter image description here

flutter dart encryption
1个回答
0
投票

encryptAES 方法和 decryptAES 中,初始化向量 (IV) 是独立生成的,导致解密期间出现“无效或损坏的挂锁”错误。解密时“iv”应该与用于加密字符串的相同。

class EncryptData {
static Encrypted? encrypted;
static var decrypted;

static final iv = IV.fromLength(16);

static encryptAES(plainText, String userKey) {
print(plainText);
final key = Key.fromUtf8(userKey);
// final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key));
encrypted = encrypter.encrypt(plainText, iv: iv);
print(encrypted!.base64);
return encrypted;
 }

 static decryptAES(encrypted, String userKey) {
final key = Key.fromUtf8(userKey);
// final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key));
decrypted = encrypter.decrypt64(encrypted!, iv: iv);
print(decrypted);
return decrypted.toString();
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.