Flutter 未处理的异常:无效参数:密钥长度不是 128/192/256 位

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

如何解决此错误:

未处理的异常:无效参数:密钥长度不是 128/192/256 位。

在此颤振代码中:

final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();

final encrypt.Key key = encrypt.Key.fromUtf8(
    phoneId.length > 32 ? phoneId.substring(0, 32) : phoneId);

final encrypt.IV iv = encrypt.IV.fromLength(16);

final encrypt.Encrypter encrypter = encrypt.Encrypter(encrypt.AES(key));

final String encrypted =
    encrypter.encrypt(timestamp + phoneId, iv: iv).base16;

谢谢你。

flutter
1个回答
0
投票

这是因为

key
不是 32 位长,只需您进入代码并验证密钥是否为 32 个字符长。

我已经更改了你的代码,如下所示,它有效


  String phoneId = "324234234234234";
  
  //final String timestamp = DateTime.now().millisecondsSinceEpoch.toString();

  //final encrypt.Key key = encrypt.Key.fromUtf8(
  //    phoneId.length > 32 ? phoneId.substring(0, 32) : phoneId);

  //create a 32 chars long key
  final key = encrypt.Key.fromUtf8('secretkey:hapilyeverafter1234567'); // 32 chars

  final encrypt.IV iv = encrypt.IV.fromLength(16);

  final encrypt.Encrypter encrypter = encrypt.Encrypter(encrypt.AES(key));

  final encrypted =
    encrypter.encrypt(phoneId, iv: iv);
 
```flutter
© www.soinside.com 2019 - 2024. All rights reserved.