My PHP server uses the encrypt as follows.
openssl_encrypt('data', 'AES-256-CBC', '1234567890123456', 0, '1234567890123456')
the result is adVh7c/vcyascTS0Z669IA==
.
My dart server uses encrypt package as follows.
import 'package:encrypt/encrypt.dart' as encrypt;
Encrypter(AES(encryptKey, mode: AESMode.cbc)).encrypt('data', iv: '1234567890123456').base64
final encrypt.Key encryptKey = encrypt.Key.fromUtf8('1234567890123456');
final encrypt.IV encryptIvKey = encrypt.IV.fromUtf8('1234567890123456');
final encrypt.Encrypter encrypter = encrypt.Encrypter(encrypt.AES(encryptKey, mode: encrypt.AESMode.cbc));
print(encrypter.encrypt('data', iv: encryptIvKey).base64);
The result is KQjJ76efmVlgGKDsj6dCog==
.
These result values are different. I saw the cipher method of PHP. If I change the cipher method in the PHP server from
AES-256-CBC
to
aes-128-cbc // or aes-128-cbc-hmac-sha1, aes-128-cbc-hmac-sha256
The result will be KQjJ76efmVlgGKDsj6dCog==
. (same as the result from the dart server)
But editing files in the PHP server is the last choice.
What I can do in the dart server to make the result the same as the result from the PHP server (AES-256-CBC
method)?
How to use the AES-256-CBC
method in encrypt package?
If I must edit files in the PHP server, what method I should use?
The aes-128-cbc
, aes-128-cbc-hmac-sha1
and aes-128-cbc-hmac-sha256
give the same result. Or some method better than this and it is available in encrypt package as follows in this image. Suggestion me, please.
The summary from the comment in my post by @Topaco. The
aes-256-cbc
cipher method requires a 32 bytes key. Use the key with a string length of 32 or use thepadRight(32, '\x00')
function.example:
Regarding
aes-128-cbc
,aes-128-cbc-hmac-sha1
andaes-128-cbc-hmac-sha256
: Applyaes-128-cbc
(ref)