【问题标题】:Convert .NET Rijndael's encryption function to PHP language将.NET Rijndael的加密功能转换为PHP语言
【发布时间】:2019-10-25 15:59:18
【问题描述】:

我需要用 PHP 语言准确转换这个 .NET 函数,有什么帮助吗?

我尝试了 StackOverflow 上的不同解决方案,但似乎没有一个适合我。

internal string Encrypt(string plaintext, string password)
{
    RijndaelManaged rijndaelCipher = new RijndaelManaged();

    byte[] plaintextByte = System.Text.Encoding.Unicode.GetBytes(plaintext);
    byte[] saltByte = Encoding.ASCII.GetBytes(password.Length.ToString());

    PasswordDeriveBytes secretKey = new PasswordDeriveBytes(password, saltByte);
    ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
    MemoryStream memoryStream = new MemoryStream();
    CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);

    cryptoStream.Write(plaintextByte, 0, plaintextByte.Length);
    cryptoStream.FlushFinalBlock();

    byte[] cipherBytes = memoryStream.ToArray();

    memoryStream.Close();
    cryptoStream.Close();
    encryptor.Dispose();

    return Convert.ToBase64String(cipherBytes);
}

谢谢!

编辑:

这是我尝试过的代码之一:

class Crypt
{
private $key,$iv_size,$iv;

/**
 * constructor
 * @param $key (string:'TheKey')
 * @return void
 */
function __construct($key='TheKey'){
    $this->key = trim($key);
    $this->iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND);
}

public function encrypt($string){
    $string=trim($string);
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $string, MCRYPT_MODE_ECB, $this->iv));
}
}

【问题讨论】:

  • 请分享您在 PHP 中尝试过的代码,谢谢
  • 我们怎么知道你尝试了什么,“but no-one似乎对我有用”是什么意思,你肯定能看出这个问题
  • Rijndael 也称为 AES。你确定 PHP 中还没有 AES 加密库吗?
  • @SteveTodd 我猜主要问题是PasswordDeriveBytes 的使用。因此,我们还需要 PHP 中的 PBKDF1 功能,并为其提供 PasswordDeriveBytes 内部使用的任何值(尤其是迭代)。

标签: c# php encryption rijndael


【解决方案1】:

我从网络服务开发者那里得到了解决方案,它工作正常!

<?php
class RijndaelOpenSSL
{
    const METHOD = 'aes-256-cbc';
    private $pbkdfBase = '';
    private $pbkdfExtra = '';
    private $pbkdfExtracount = 0;
    private $pbkdfHashno = 0;
    private $pbkdfState = 0;
    private $iterations = 100;

    public function reset()
    {
        $this->pbkdfBase = '';
        $this->pbkdfExtra = '';
        $this->pbkdfExtracount = 0;
        $this->pbkdfHashno = 0;
        $this->pbkdfState = 0;
    }

    public function decrypt($inputText, $password)
    {
        $this->reset();
        $salt = (string) mb_strlen($password);
        $key = $this->pbkdf1($password, $salt, 32);
        $iv = $this->pbkdf1($password, $salt, 16);
        $decrypted = openssl_decrypt(base64_decode($inputText), self::METHOD, $key, OPENSSL_RAW_DATA, $iv);
        return mb_convert_encoding($decrypted, 'UTF-8', 'UTF-16LE');
    }

    public function encrypt($inputText, $password)
    {
        $this->reset();
        $salt = (string) mb_strlen($password);
        $key = $this->pbkdf1($password, $salt, 32);
        $iv = $this->pbkdf1($password, $salt, 16);
        $textUTF = mb_convert_encoding($inputText, 'UTF-16LE');
        $encrypted = openssl_encrypt($textUTF, self::METHOD, $key, OPENSSL_RAW_DATA, $iv);
        return base64_encode($encrypted);
    }

    private function pbkdf1($pass, $salt, $countBytes)
    {
        if ($this->pbkdfState == 0) {
            $this->pbkdfHashno = 0;
            $this->pbkdfState = 1;
            $key = $pass . $salt;
            $this->pbkdfBase = sha1($key, true);
            for ($i = 2; $i < $this->iterations; $i++) {
                $this->pbkdfBase = sha1($this->pbkdfBase, true);
            }
        }
        $result = '';
        if ($this->pbkdfExtracount > 0) {
            $rlen = strlen($this->pbkdfExtra) - $this->pbkdfExtracount;
            if ($rlen >= $countBytes) {
                $result = substr($this->pbkdfExtra, $this->pbkdfExtracount, $countBytes);
                if ($rlen > $countBytes) {
                    $this->pbkdfExtracount += $countBytes;
                } else {
                    $this->pbkdfExtra = null;
                    $this->pbkdfExtracount = 0;
                }
                return $result;
            }
            $result = substr($this->pbkdfExtra, $rlen, $rlen);
        }
        $current = '';
        $clen = 0;
        $remain = $countBytes - strlen($result);
        while ($remain > $clen) {
            if ($this->pbkdfHashno == 0) {
                $current = sha1($this->pbkdfBase, true);
            } else if ($this->pbkdfHashno < 1000) {
                $num = sprintf('%d', $this->pbkdfHashno);
                $tmp = $num . $this->pbkdfBase;
                $current .= sha1($tmp, true);
            }
            $this->pbkdfHashno++;
            $clen = strlen($current);
        }
        // $current now holds at least as many bytes as we need
        $result .= substr($current, 0, $remain);
        // Save any left over bytes for any future requests
        if ($clen > $remain) {
            $this->pbkdfExtra = $current;
            $this->pbkdfExtracount = $remain;
        }
        return $result;
    }
}

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 2019-11-10
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 2012-01-03
    • 1970-01-01
    相关资源
    最近更新 更多