作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试从 Linux 端管理的数据库表中读取 Base64 编码值。在那里面表中有一列名为 first_name。在 Linux 端,我可以在 PHP 中使用以下命令轻松解密:
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, "patient_fn_salt",
base64_decode("H6XmkH+VWvdD88THCliKJjLisGZIBk3CTNvyQMLnhpo="),
MCRYPT_MODE_ECB);
但是,我尽可能多地尝试在 C# 端复制此逻辑,但我得到的只是乱码。
我的 C# 代码在下面,我希望你能给我一些建议,因为我已经没有想法了:(
byte [] cipherText =
Convert.FromBase64String("H6XmkH+VWvdD88THCliKJjLisGZIBk3CTNvyQMLnhpo=");
byte [] key = Encoding.UTF8.GetBytes("patient_fn_salt");
Array.Resize(ref key, 32);
byte [] iv = new byte[32];
string fname = Utilities.Decrypt(cipherText, key, iv);
public static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// TDeclare the streams used
// to decrypt to an in memory
// array of bytes.
MemoryStream msDecrypt = null;
CryptoStream csDecrypt = null;
StreamReader srDecrypt = null;
// Declare the AesManaged object
// used to decrypt the data.
RijndaelManaged rj = new RijndaelManaged();
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
try
{
// Create an AesManaged object
// with the specified key and IV.
rj.Mode = CipherMode.ECB;
rj.BlockSize = 256;
rj.KeySize = 256;
rj.Padding = PaddingMode.Zeros;
rj.Key = Key;
rj.GenerateIV();
//rj.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rj.CreateDecryptor(rj.Key, rj.IV);
// Create the streams used for decryption.
msDecrypt = new MemoryStream(cipherText);
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
srDecrypt = new StreamReader(csDecrypt);
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
finally
{
// Clean things up.
// Close the streams.
if (srDecrypt != null)
srDecrypt.Close();
if (csDecrypt != null)
csDecrypt.Close();
if (msDecrypt != null)
msDecrypt.Close();
// Clear the AesManaged object.
if (rj != null)
rj.Clear();
}
return plaintext;
}
}
最佳答案
帖子很旧,但这可能对将来的人有所帮助。此函数使用参数 MCRYPT_RIJNDAEL_256 和 MCRYPT_MODE_ECB 加密,与 mcrypt_encrypt 完全相同
static byte[] EncryptStringToBytes(string plainText, byte[] key)
{
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
byte[] encrypted;
using (var rijAlg = new RijndaelManaged())
{
rijAlg.BlockSize = 256;
rijAlg.Key = key;
rijAlg.Mode = CipherMode.ECB;
rijAlg.Padding = PaddingMode.Zeros;
rijAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (var msEncrypt = new MemoryStream())
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
swEncrypt.Write(plainText);
encrypted = msEncrypt.ToArray();
}
}
return encrypted;
}
这里是解密函数
static string DecryptStringFromBytes(byte[] cipherText, byte[] key)
{
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
string plaintext;
using (var rijAlg = new RijndaelManaged())
{
rijAlg.BlockSize = 256;
rijAlg.Key = key;
rijAlg.Mode = CipherMode.ECB;
rijAlg.Padding = PaddingMode.Zeros;
rijAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
using (var msDecrypt = new MemoryStream(cipherText))
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (var srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
}
return plaintext;
}
关于c# - 如何在 C# 中解密由 mcrypt 在 PHP 中加密的加密 MCRYPT_RIJNDAEL_256 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7083440/
我是一名优秀的程序员,十分优秀!