用Dart中的AES ECB加密二进制数组
我正在寻找一种在Dart中加密二进制数组的方法。我已经看过一些比较常见的库,例如,但其中许多库只能处理字符串形式的AES密钥和数据,而不能处理二进制数组 我也看了一下,它似乎能够处理二进制数组中的AES密钥和数据,但我不太明白如何正确使用它。包含数据的二进制数组的长度始终与键的长度相同,因此不需要任何填充 这是我目前的代码:用Dart中的AES ECB加密二进制数组,dart,flutter,pointycastle,Dart,Flutter,Pointycastle,我正在寻找一种在Dart中加密二进制数组的方法。我已经看过一些比较常见的库,例如,但其中许多库只能处理字符串形式的AES密钥和数据,而不能处理二进制数组 我也看了一下,它似乎能够处理二进制数组中的AES密钥和数据,但我不太明白如何正确使用它。包含数据的二进制数组的长度始终与键的长度相同,因此不需要任何填充 这是我目前的代码: class Encr { static List<int> encrCmd(List<int> inputData, List<int
class Encr {
static List<int> encrCmd(List<int> inputData, List<int> aesKey) {
Uint8List keyList = Uint8List.fromList(aesKey);
Uint8List dataList = Uint8List.fromList(inputData);
CipherParameters cip = new PaddedBlockCipherParameters(newKeyParameter(keylist), null);
BlockCipher cipherImpl = new BlockCipher("AES/ECB");
cipherImpl.init(true, cip);
Uint8List encrypted = cipherImpl.process(dataList);
print("encrypted data: " + encrypted.toString());
}
}
这将导致以下错误消息:
I/flutter (55555): The following assertion was thrown while handling a gesture:
I/flutter (55555): type 'PaddedBlockCipherParameters<KeyParameter, Null>' is not a subtype of type 'KeyParameter' of 'params'
不幸的是,关于如何使用PointyCastle的信息并不多。是否有更好的方法来实现我的目标?您不需要填充的BlockCipherParameters,因为您没有使用填充的密码
尝试:
非常感谢你!它马上就可以工作了,正是我想要的工作方式!
import 'dart:typed_data';
import 'package:pointycastle/api.dart';
import 'package:pointycastle/block/aes_fast.dart';
import 'package:pointycastle/block/modes/ecb.dart';
main() {
Uint8List key = Uint8List.fromList(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
);
Uint8List plainText = Uint8List.fromList(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
);
BlockCipher cipher = ECBBlockCipher(AESFastEngine());
cipher.init(
true,
KeyParameter(key),
);
Uint8List cipherText = cipher.process(plainText);
}