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

标签 php powershell encryption

有人可以帮助我使用 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/

相关文章:

php - 使用 Wordpress wp_query 搜索存储在数组中的多个变量的出现

php - 如何在 PDO 中绑定(bind)当前列值?

c# - 通过 C# Powershell 变量设置

python - 安装3.5后如何从win7 powershell启动python 2.7?

php - 我如何使用 PHP 给 MySQL 中的每一行删除按钮?

linux - 是否可以编写一个在 bash/shell 和 PowerShell 中运行的脚本?

c# - 无法解密使用 AesManaged 加密的文件

android - SSL 固定客户端加密

c# - 在 App.Config 文件的 appsettings 部分加密单个 key

php - 为什么我的变量通过引用传递?