我如何加密和解密上的dart/flutter相同的方式,我对php?



我有以下api:

class AESEncrypter
{
public static function EncryptString($plainText, $phrase)
{
if(strlen($phrase) < 32)
{
while(strlen($phrase) < 32)
{
$phrase .= $phrase;
}
$phrase = substr($phrase,0,32);
}
if(strlen($phrase) > 32)
{
$phrase = substr($phrase,0,32);
}
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
echo('valor: '.openssl_cipher_iv_length('aes-256-cbc'));
$string = openssl_encrypt($plainText,"aes-256-cbc",$phrase, OPENSSL_RAW_DATA , $iv);
return base64_encode($iv.$string);
}
public static function DecryptString($plainText, $phrase)
{
if(strlen($phrase) < 32)
{
while(strlen($phrase) < 32)
{
$phrase .= $phrase;
}
$phrase = substr($phrase,0,32);
}
if(strlen($phrase) > 32)
{
$phrase = substr($phrase,0,32);
}
$plainText = base64_decode($plainText);
$encodedData = substr($plainText, openssl_cipher_iv_length('aes-256-cbc'));
$iv = substr($plainText,0,openssl_cipher_iv_length('aes-256-cbc'));
$decrypted = openssl_decrypt($encodedData, "aes-256-cbc", $phrase, OPENSSL_RAW_DATA, $iv);
return $decrypted;
}
}

和我需要一个.dart文件来与api正确通信。

我试着:

class AESEncrypter {
static encryptString(plainText, phrase) {
if ((phrase.length) < 32) {
while ((phrase.length) < 32) {
phrase = phrase + phrase;
}
phrase = phrase.substring(0, 32);
}
if ((phrase.length) > 32) {
phrase = phrase.substring(0, 32);
}
final iv = IV.fromSecureRandom(16);
final key = Key.fromUtf8(phrase);
final encrypter = Encrypter(AES(key));
final encrypted = encrypter.encrypt(plainText, iv: iv);
return encrypted.base64;
//return "${iv.base64}${encrypted.base64}";
}
}

但不工作。我不知道如何在dart上编写代码来执行相同的加密/解密过程。当我在dart上加密和在php上解密时,我不会得到相同的文本。

有什么建议吗?谢谢& lt; 3

试试这个加密

import 'package:encrypt/encrypt.dart';
void main() {
final plainText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit';
final key = Key.fromUtf8('my 32 length key................');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(key));
final encrypted = encrypter.encrypt(plainText, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print(decrypted); // Lorem ipsum dolor sit amet, consectetur adipiscing elit
print(encrypted.base64); // R4PxiU3h8YoIRqVowBXm36ZcCeNeZ4s1OvVBTfFlZRdmohQqOpPQqD1YecJeZMAop/hZ4OxqgC1WtwvX/hP9mw==
}

相关内容

  • 没有找到相关文章

最新更新