小编Sti*_*ick的帖子

输入数据长度必须是 AES CTR 中密码块大小的倍数

我使用 Dart 的加密包加密字符串。我加密的代码如下。

\n
String encrypt(String kelime) {\n    final key = Key.fromUtf8(\'H4WtkvK4qyehIe2kjQfH7we1xIHFK67e\'); //32 length\n    final iv = IV.fromUtf8(\'HgNRbGHbDSz9T0CC\');\n    final encrypter = Encrypter(AES(key, mode: AESMode.cbc));\n    final encrypted = encrypter.encrypt(kelime, iv: iv);\n    return encrypted.base64;\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

然后我使用相同的包解码加密数据,并收到此错误输入数据长度必须是密码块大小的倍数。经过一番研究,我了解到加密包在破译 AES 加密算法时遇到了困难。我了解到可以使用 Pointycastle 包解密加密的单词。代码如下

\n
String decryptt(String cipher) {\n\n    final key = Key.fromUtf8(\'H4WtkvK4qyehIe2kjQfH7we1xIHFK67e\');\n\n    final iv = IV.fromUtf8(\'HgNRbGHbDSz9T0CC\');\n\n    final encryptedText = Encrypted.fromUtf8(cipher);\n    final ctr = pc.CTRStreamCipher(pc.AESFastEngine())\n      ..init(false, pc.ParametersWithIV(pc.KeyParameter(key.bytes), iv.bytes));\n    Uint8List decrypted = ctr.process(encryptedText.bytes);\n\n    print(String.fromCharCodes(decrypted));\n\n    return String.fromCharCodes(decrypted);\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

当我解密用 pointycastle 加密的数据时,我得到这样的输出。

\n
\n

c\xc3\xb3\xc2\xa5\xc3\x84\xc3\x90\xc3\x92\xc3\x8b.\xc3\xa5$[~?q{.. 9

\n …

aes dart flutter pointycastle

5
推荐指数
1
解决办法
3392
查看次数

标签 统计

aes ×1

dart ×1

flutter ×1

pointycastle ×1