Flutter 在AES CTR模式下,输入数据必须是密码块大小的倍数

Flutter 在AES CTR模式下,输入数据必须是密码块大小的倍数,flutter,encryption,dart,aes,Flutter,Encryption,Dart,Aes,当我使用Dart使用AES CTR模式解密某些内容时,会出现此异常: E/flutter (19095): Invalid argument(s): Input data length must be a multiple of cipher's block size E/flutter (19095): #0 PaddedBlockCipherImpl.process (package:pointycastle/padded_block_cipher/padded_block_cip

当我使用Dart使用AES CTR模式解密某些内容时,会出现此异常:

E/flutter (19095): Invalid argument(s): Input data length must be a multiple of cipher's block size
E/flutter (19095): #0      PaddedBlockCipherImpl.process (package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart:55:9)
E/flutter (19095): #1      AES.decrypt (package:encrypt/src/algorithms/aes.dart:38:20)
这是我的密码:

final encrypter = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.ctr));
final decrypted = encrypter.decrypt(encrypt.Encrypted.fromBase16(cipher), iv: iv);
密码
是长度为10的十六进制字符串。我认为AES CTR模式不需要任何填充。如果它确实需要填充,我应该用什么填充?我试过这个:

final decrypted = encrypter.decrypt(encrypt.Encrypted.fromBase16(cipher.padRight(16, null)), iv: iv);
但我有以下例外:

E/flutter (19095): FormatException: Invalid radix-16 number (at character 1)
E/flutter (19095): nu
E/flutter (19095): ^

使用
'0'
作为填充会导致我描述的第一个异常。

这是Dart加密包中的一个问题。如果不是块大小的倍数,则无法处理使用AES CTR模式加密的内容。这个包是的包装器,我能够成功地使用它来解密使用AES CTR模式加密的字符串。代码如下:

import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:pointycastle/export.dart' as pc;

String decrypt(String cipher, Uint8List key, Uint8List iv) {
  final encryptedText = encrypt.Encrypted.fromBase16(cipher);
  final ctr = pc.CTRStreamCipher(pc.AESFastEngine())
    ..init(false, pc.ParametersWithIV(pc.KeyParameter(key.bytes), iv.bytes));
  Uint8List decrypted = ctr.process(encryptedText.bytes);

  print(String.fromCharCodes(decrypted));

  return String.fromCharCodes(decrypted);
}

cipher
是一个十六进制字符串。encrypt的包仍然很有用,因为它提供了将十六进制字符串转换为Uint8List的实用程序

我不熟悉这个包,但我认为这是一个错误-您的想法是正确的,CTR不需要填充。