我使用 Dart 的加密包加密字符串。我加密的代码如下。
\nString 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 }\nRun Code Online (Sandbox Code Playgroud)\n然后我使用相同的包解码加密数据,并收到此错误输入数据长度必须是密码块大小的倍数。经过一番研究,我了解到加密包在破译 AES 加密算法时遇到了困难。我了解到可以使用 Pointycastle 包解密加密的单词。代码如下
\nString 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 }\nRun Code Online (Sandbox Code Playgroud)\n当我解密用 pointycastle 加密的数据时,我得到这样的输出。
\n\nc\xc3\xb3\xc2\xa5\xc3\x84\xc3\x90\xc3\x92\xc3\x8b.\xc3\xa5$[~?q{.. 9
\n …
我正在尝试用 flutter 和 Javascript 编写两个函数,我可以在整个项目中使用它们在交换数据时使用 AES 加密或解密数据。对于 Flutter,我根据说明使用 pointycastle 包 https://gist.github.com/proteye/e54eef1713e1fe9123d1eb04c0a5cf9b?signup=true
import 'dart:convert';
import 'dart:typed_data';
import "package:pointycastle/export.dart";
import "./convert_helper.dart";
// AES key size
const KEY_SIZE = 32; // 32 byte key for AES-256
const ITERATION_COUNT = 1000;
class AesHelper {
static const CBC_MODE = 'CBC';
static const CFB_MODE = 'CFB';
static Uint8List deriveKey(dynamic password,
{String salt = '',
int iterationCount = ITERATION_COUNT,
int derivedKeyLength = KEY_SIZE}) {
if (password == null || password.isEmpty) {
throw new ArgumentError('password must not be …Run Code Online (Sandbox Code Playgroud)