FLUTER/DART AES-256-CBC在PHP中从加密中解密
有人能帮我找出使用AES-256-CBC在PHP中加密的数据的解密算法吗?我尝试了很多不同的方法,但我认为我在尝试复制在DART中重新创建键/IV的方法时搞砸了,并且不断得到异常,如:
RangeError (end): Invalid value: Not in inclusive range 0..16:
执行加密的PHP代码(由于加密字符串由第三方提供,因此无法更改)如下:
function encrypt( $string, $encrypt=true) {
$secret_key = 'SuperSecretKey';
$secret_iv = 'SuperSecretBLOCK';
$output = false;
$encrypt_method = "AES-256-CBC";
$key = hash( 'sha256', $secret_key );
$iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
if($encrypt) {
$output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
} else {
$output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
}
return $output;
}
例如,如果调用PHP中的加密例程来加密字符串";This is a Test!";,结果将是:
ZHArWURDY2FkelBtSGY5c1AzdTNBZz09
我正尝试在DART中解密此结果,但没有任何成功!
以下是我目前掌握的导致上述异常的信息:
import 'package:encrypt/encrypt.dart';
import 'package:crypto/crypto.dart';
import 'dart:convert' show utf8;
String extractPayload(String payload) {
String strPwd = 'SuperSecretKey';
String strIv = 'SuperSecretBLOCK';
var iv = sha256.convert(utf8.encode(strIv));
var key = sha256.convert(utf8.encode(strPwd));
IV ivObj = IV.fromUtf8(iv.toString());
Key keyObj = Key.fromUtf8(key.toString());
final encrypter = Encrypter(AES(keyObj));
final decrypted = encrypter.decrypt(Encrypted.from64(payload), iv: ivObj);
print(decrypted);
return decrypted;
}
欢迎提出任何建议, 谢谢
php>推荐答案
代码有许多不必要的弱点,会使移植变得复杂:
相关文章