如何以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
I have the following 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;
}
}
and I need a .dart file to comunicate properly with the api.
I tried:
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}";
}
}
But not working. I don't get how to code on dart to do the same encrypt/decrypt proccess. When I encrypt on dart and decrypt on php I don't get the same text.
Any suggestions?
thanks <3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试此 encrypt
Try this encrypt