Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换.NET Rijndael';PHP语言中的s加密函数_C#_Php_Encryption_Rijndael - Fatal编程技术网

C# 转换.NET Rijndael';PHP语言中的s加密函数

C# 转换.NET Rijndael';PHP语言中的s加密函数,c#,php,encryption,rijndael,C#,Php,Encryption,Rijndael,我需要用PHP语言准确地转换这个.NET函数,有什么帮助吗 我尝试了StackOverflow上的不同解决方案,但似乎没有人适合我 internal string Encrypt(string plaintext, string password) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); byte[] plaintextByte = System.Text.Encoding.Unicode.GetByt

我需要用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));
}
}

我从web服务开发人员那里得到了解决方案,效果很好

<?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;
    }
}

plz分享您在PHP中尝试过的代码,谢谢大家,我们知道您尝试过什么吗,“但是似乎没有人适合我”的意思是什么,您肯定可以看到Rijndael的这个可能的副本的问题,也就是AES。你确定PHP中还没有AES加密库吗?@SteveTodd我想主要问题是
PasswordDeriveBytes
的使用。因此,我们还需要PHP中的PBKDF1功能,并为其提供PasswordDeriveBytes内部使用的任何值(尤其是迭代)。