如何以PHP的同样方式加密和解密飞镖/颤动?

发布于 2025-02-07 19:43:35 字数 2168 浏览 0 评论 0原文

我有以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2025-02-14 19:43:35

尝试此 encrypt

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==
}

Try this encrypt

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==
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文