在 AES CTR 模式下,输入数据必须是密码块大小的倍数
Input data must be multiple of cipher block size in AES CTR mode
当我使用 Dart 的 encrypt package 使用 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);
cipher
是长度为 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 模式加密的内容。这个包是 Pointy Castle 的包装器,我能够成功地使用它来解密使用 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 的实用程序
我在尝试解密未加密的文件时遇到了这个问题。只需将输入更改为已加密的文件即可。
当我使用 Dart 的 encrypt package 使用 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);
cipher
是长度为 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 模式加密的内容。这个包是 Pointy Castle 的包装器,我能够成功地使用它来解密使用 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 的实用程序
我在尝试解密未加密的文件时遇到了这个问题。只需将输入更改为已加密的文件即可。