从php脚本中解密powershell中使用Rijndael方法创建的文本


Decrypt text created with Rijndael method in powershell from a php script

有人能帮我用php脚本解密php中正在使用Rijndael密钥创建的值。

我用来创建它的powershell脚本在 下面
[Reflection.Assembly]::LoadFile('C:'WINDOWS'Microsoft.NET'Framework'v2.0.50727'System.Web.dll') | out-null 
$XCADMPass = "CloudyTest"
$r = new-Object System.Security.Cryptography.RijndaelManaged  # use Rijndael  symmetric key encryption
$c = $r.CreateEncryptor((1..16), (1..16))    # Set the key and initialisation vector to 128-bytes each of (1..16)
$ms = new-Object IO.MemoryStream
$cs = new-Object Security.Cryptography.CryptoStream $ms,$c,"Write" # Target data stream, transformation, and mode
$sw = new-Object IO.StreamWriter $cs
$sw.Write($XCADMPass)       # Write the string through the crypto stream into the memory stream
$sw.Close()
$cs.Close()
$ms.Close()
$r.Clear()
[byte[]]$result = $ms.ToArray()      # Byte array from the encrypted memory stream
$encPass = [Convert]::ToBase64String($result)

加密值为yD3/YeGk64JJ2LUI20mo8Q==

我试过使用mcrypt_decrypt,但无法找出正确的方法来获取我的原始文本。

这是我尝试过的

$input = "yD3/YeGk64JJ2LUI20mo8Q==";
$key = "12345678910111213141516";
$data = base64_decode($input);
$iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
$decrypted = rtrim(
mcrypt_decrypt(
    MCRYPT_RIJNDAEL_128,
    $key,
    substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)),
    MCRYPT_MODE_CBC,
    $iv
),
"'0"
);

我显然是在某个地方出错了,所以任何帮助都会非常感激。

感谢所有帮助我的人。我将向遇到类似问题的其他人展示最后的代码。

Powershell

[Reflection.Assembly]::LoadFile('C:'WINDOWS'Microsoft.NET'Framework'v2.0.50727'System.Web.dll') | out-null 
$XCADMPass = "CloudyTest"
$r = new-Object System.Security.Cryptography.RijndaelManaged  # use Rijndael  symmetric key encryption
$c = $r.CreateEncryptor((1..16), (1..16))    # Set the key and initialisation vector to 128-bytes each of (1..16)
$ms = new-Object IO.MemoryStream
$cs = new-Object Security.Cryptography.CryptoStream $ms,$c,"Write" # Target data stream, transformation, and mode
$sw = new-Object IO.StreamWriter $cs
$sw.Write($XCADMPass)       # Write the string through the crypto stream into the memory stream
$sw.Close()
$cs.Close()
$ms.Close()
$r.Clear()
[byte[]]$result = $ms.ToArray()      # Byte array from the encrypted memory stream
$encPass = [Convert]::ToBase64String($result)
PHP

$input = "yD3/YeGk64JJ2LUI20mo8Q==";
$iv = "'x01'x02'x03'x04'x05'x06'x07'x08'x09'x0a'x0b'x0c'x0d'x0e'x0f'x10";
$key = "'x01'x02'x03'x04'x05'x06'x07'x08'x09'x0a'x0b'x0c'x0d'x0e'x0f'x10";    
$data = base64_decode($input);

$decrypted = rtrim(
mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$key,
$data,
MCRYPT_MODE_CBC,
$iv
),
"'0"
);
echo $decrypted;