我用 C# 编写了一个代码,用于使用 Rijndael 算法进行加密。现在我想解密 php.ini 中的加密值。我试过了,但没有得到我加密的确切字符串。下面是 C# 中的加密代码。
public string Encrypt(string textToBeEncrypted, string Password)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
ICryptoTransform Encryptor = null;
byte[] plainText = null;
try
{
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric encryptor object.
Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
plainText = Encoding.Unicode.GetBytes(textToBeEncrypted);
}
catch (Exception ex)
{
string str = "Method Name: " + MethodBase.GetCurrentMethod().Name + " | Description: " + ex.Message + ex.InnerException;
log.Error(str);
}
return Convert.ToBase64String(Encryptor.TransformFinalBlock(plainText, 0, plainText.Length));
}
php中的解密代码是
function decryptData($value){
$key = "same key used in above c# code";
$crypttext = $value;
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
return trim($decrypttext);
}
我得到了用于解密的 c# 代码,如下所示
public string Decrypt(string TextToBeDecrypted, string Password) {
RijndaelManaged RijndaelCipher = new RijndaelManaged();
string DecryptedData;
byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
//Making of the key for decryption
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric Rijndael decryptor object.
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32),SecretKey.GetBytes(16));
byte[] plainText = Decryptor.TransformFinalBlock(EncryptedData, 0, EncryptedData.Length);
//Converting to string
DecryptedData = Encoding.Unicode.GetString(plainText);
return DecryptedData;
}
但是希望 PHP.Key 中的代码与用于加密的代码相同。请指教....