Конвертировать функцию шифрования .NET Rijndael в язык PHP - PullRequest
0 голосов
/ 11 июня 2019

Мне нужно точно преобразовать эту функцию .NET в PHP Language, любая помощь?

Я пробовал разные решения, найденные в 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);
}

Спасибо!

EDIT:

Вот один из кодов, которые я пробовал:

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

1 Ответ

0 голосов
/ 12 июня 2019

Я получил решение от разработчика веб-службы, оно отлично работает!

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

}

...