我需要用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));
}
}
答案 0 :(得分:0)
我从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;
}
}