有人可以帮助我使用 php 脚本来解密使用 Rijndael key 在 powershell 中创建的 php 中的值。
我用来创建它的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"
);
我显然在某个地方出错了,所以任何帮助将不胜感激。
最佳答案
感谢所有为此提供帮助的人。我将为遇到类似问题的其他人展示最终代码。
电源外壳
[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;
关于php - 从 php 脚本解密在 powershell 中使用 Rijndael 方法创建的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31338681/