Reputation: 6257
I have these PostgreSQL commands:
select encrypt_iv('test1','테스트2','测试3','aes-cbc');
select convert_from(decrypt_iv('\x8e9a657e13b64f4111ab1668dc0f5747','테스트2','测试3','aes-cbc'),'SQL_ASCII');
I'm trying to reproduce those commands in Dart by using Encrypt package:
void main(List<String> args) {
final plainText = 'test1';
final key = Key.fromUtf8('테스트2');
final iv = IV.fromUtf8('测试3');
final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print(decrypted);
print(encrypted.bytes);
print(encrypted.base16);
print(encrypted.base64);
}
I'm getting this error message:
Unhandled exception:
Invalid argument(s): Initialization vector must be the same length as block size
#0 CBCBlockCipher.init (package:pointycastle/block/modes/cbc.dart:52:7)
#1 PaddedBlockCipherImpl.init (package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart:47:12)
#2 AES.encrypt (package:encrypt/src/algorithms/aes.dart:35:9)
#3 Encrypter.encryptBytes (package:encrypt/src/encrypter.dart:12:19)
#4 Encrypter.encrypt (package:encrypt/src/encrypter.dart:20:12)
#5 main (file:///home/userx/Projects/dart/encrypt_example/bin/main.dart:11:31)
#6 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:281:32)
#7 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
Help is missing in this package so I don't know how to properly use it.
Can someone give me some guidance on its use or suggest another package for that purpose?
Upvotes: 0
Views: 1526
Reputation: 12085
Initialization vector must be the same length as block size
AES block size is 128 bit (16 bytes), therefore
The PostgreSQL documentation claims for IV:
It is clipped or padded with zeroes if not exactly block size.
I assume (!) it will be the same for the key. IMHO this is a terrible idea from security and interoperability perspective), but we have to live with it. So try to have a zero-byte array of required length and copy your shorter IV or key to the array.
Next
Upvotes: 2