[英]Decrypt C# RIJNDAEL encoded text in PHP
I need to decrypt a string as part of an authorization process. 作为授权过程的一部分,我需要解密字符串。
The documentation specifies that the authorization string was encrypted with the following settings: 该文档指定使用以下设置对授权字符串进行加密:
PKCS*7
输入数据的填充: PKCS*7
The C# example: C#示例:
/// <summary>
/// Decrypts a string.
/// </summary>
/// <param name="content">The string to decrypt.</param>
/// <param name="password">The password to use.</param>
/// <returns>The decrypted string.</returns>
private static string DecryptString(string content, string password)
{
Rijndael aes;
byte[] retVal = null;
byte[] contentBytes;
byte[] passwordBytes;
byte[] ivBytes;
try
{
contentBytes = Convert.FromBase64String(content);
//Create the password and initial vector bytes
passwordBytes = new byte[32];
ivBytes = new byte[16];
Array.Copy(Encoding.Unicode.GetBytes(password), passwordBytes, Encoding.Unicode.GetBytes(password).Length);
Array.Copy(passwordBytes, ivBytes, 16);
//Create the cryptograpy object
using (aes = Rijndael.Create())
{
aes.Key = passwordBytes;
aes.IV = ivBytes;
aes.Padding = PaddingMode.PKCS7;
//Decrypt
retVal = aes.CreateDecryptor().TransformFinalBlock(contentBytes, 0, contentBytes.Length);
}
}
catch
{
}
return Encoding.Unicode.GetString(retVal);
}
The same function was discussed here, but for JAVA: Decrypt C# RIJNDAEL encoded text 这里讨论了相同的功能,但对于JAVA: 解密C#RIJNDAEL编码的文本
I tried to decrypt it with the following function but the result is different than expected: 我尝试使用以下功能对其进行解密,但结果与预期的不同:
function decrypt($string, $pass){
$iv = substr($pass, 0, 16);
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,
$pass,
base64_decode($string),
MCRYPT_MODE_CBC,
$iv);
$pad = ord($data[strlen($data) - 1]);
return substr($data, 0, -$pad);
}
The ecrypted string "7iTdZnp0DtGnIfwwqY4W/glbLLVZ0+asVLAuz13PzrW0wM6HC7rNuQvcG8JDSehyYeBJARdXHgLo9hRL9sBz3fN5LJ8cro3o0kFnAao2YRU="
加密的字符串"7iTdZnp0DtGnIfwwqY4W/glbLLVZ0+asVLAuz13PzrW0wM6HC7rNuQvcG8JDSehyYeBJARdXHgLo9hRL9sBz3fN5LJ8cro3o0kFnAao2YRU="
should decrypt to 应该解密为
"ldYWMFlSbcki6LMl3rkNfGavnt8VqmZd"
using the password "GAT"
使用密码"GAT"
I think it has something to do with the password / iv / encoding 我认为这与密码/ iv /编码有关
function decrypt($string, $pass)
{
$encodedPass = mb_convert_encoding($pass, 'utf-16le');
$encodedPass = substr($encodedPass, 0, 32);
$encodedPass = str_pad($encodedPass, 32, "\0", STR_PAD_RIGHT);
$iv = substr($encodedPass, 0, 16);
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
$encodedPass,
base64_decode($string),
MCRYPT_MODE_CBC,
$iv);
$pad = ord($data[strlen($data) - 1]);
return substr($data, 0, -$pad);
}
In PHP, the MCRYPT_RIJNDAEL_128
/ MCRYPT_RIJNDAEL_256
isn't the size of the key, but the size of the block (see https://www.chilkatsoft.com/p/php_aes.asp ). 在PHP中, MCRYPT_RIJNDAEL_128
/ MCRYPT_RIJNDAEL_256
不是密钥的大小,而是块的大小(请参阅https://www.chilkatsoft.com/p/php_aes.asp )。 C# normally uses 16 bytes blocks, so MCRYPT_RIJNDAEL_128
. C#通常使用16个字节的块,因此MCRYPT_RIJNDAEL_128
。 The size of the key is auto-detected. 密钥的大小是自动检测的。 Note that I've moved the encoding/resizing of the password inside the method. 请注意,我已经在方法内部移动了密码的编码/大小调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.