flutter php AES 加解密
程序员文章站
2024-03-14 17:32:10
...
flutter
https://pub.flutter-io.cn/packages/encrypt
引入 encrypt: ^4.1.0
import 'package:encrypt/encrypt.dart' as MyEncrypt;
final plainText = 'test 123';
final key = MyEncrypt.Key.fromUtf8('my 32 length key................');
// final iv = MyEncrypt.IV.fromLength(16);
final iv = MyEncrypt.IV.fromUtf8("0102030405060708");
final encrypter = MyEncrypt.Encrypter(MyEncrypt.AES(key,mode:MyEncrypt.AESMode.cbc));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt64("5J/PzlzmvuEcXQrIRv4wWA==", iv: iv);
print(encrypted.base64); // 5J/PzlzmvuEcXQrIRv4wWA==
print(decrypted.toString());// test 123
php
<?php
/**
* openssl 实现
*/
define('KEY', 'my 32 length key................');
$iv= "0102030405060708";
//$iv= null;
$str="test 123";
# 加密 md5->true 为16位md5
//echo $strEncode= base64_encode(openssl_encrypt($str, 'AES-256-CBC',KEY, PKCS7_BINARY , $iv)); # AES-256-CBC
$strEncode= openssl_encrypt($str, 'AES-256-CBC',KEY, PKCS7_BINARY , $iv); # AES-256-CBC
var_dump($strEncode);
/**
* 解密
*/
//echo openssl_decrypt(base64_decode($strEncode), 'AES-128-CBC', KEY, OPENSSL_RAW_DATA, $iv);
$strDecode = openssl_decrypt("5J/PzlzmvuEcXQrIRv4wWA==", 'AES-256-CBC', KEY, PKCS7_BINARY, $iv);
var_dump($strDecode);