如何解决openssl_decrypt() 用于加密 c#
openssl_decrypt() 是链接下已经定义好的函数PHP
https://www.meridianoutpost.com/resources/etools/php-functions/php-openssl-decrypt.php?
我想用c#实现这个功能
因为下面已经在PHP中完成了
public function decrypt($data,$password)
{
$iv = hex2bin(substr($data,16 * 2));
$data = hex2bin(substr($data,16 * 2));
$plain_data = openssl_decrypt($data,'aes-256-cbc',$password,true,$iv);
return $plain_data;
}
c#
public static string OpenSSLDecryptNew(string encrypted,string passphrase)
{
// base 64 decode
byte[] encryptedBytesWithSalt = Convert.FromBase64String(encrypted);
// extract salt (first 8 bytes of encrypted)
byte[] salt = new byte[8];
byte[] encryptedBytes = new byte[encryptedBytesWithSalt.Length - salt.Length - 8];
Buffer.Blockcopy(encryptedBytesWithSalt,8,salt,salt.Length);
Buffer.Blockcopy(encryptedBytesWithSalt,salt.Length + 8,encryptedBytes,encryptedBytes.Length);
// get key and iv
byte[] key,iv;
DeriveKeyAndIV(passphrase,out key,out iv);
return DecryptStringFromByteSAEs(encryptedBytes,key,iv);
}
private static void DeriveKeyAndIV(string passphrase,byte[] salt,out byte[] key,out byte[] iv)
{
// generate key and iv
List<byte> concatenatedHashes = new List<byte>(48);
byte[] password = Encoding.UTF8.GetBytes(passphrase);
byte[] currentHash = new byte[0];
MD5 md5 = MD5.Create();
bool enoughBytesForKey = false;
// See http://www.openssl.org/docs/crypto/EVP_BytesToKey.html#KEY_DERIVATION_ALGORITHM
while (!enoughBytesForKey)
{
int preHashLength = currentHash.Length + password.Length + salt.Length;
byte[] preHash = new byte[preHashLength];
Buffer.Blockcopy(currentHash,preHash,currentHash.Length);
Buffer.Blockcopy(password,currentHash.Length,password.Length);
Buffer.Blockcopy(salt,currentHash.Length + password.Length,salt.Length);
currentHash = md5.ComputeHash(preHash);
concatenatedHashes.AddRange(currentHash);
if (concatenatedHashes.Count >= 48)
enoughBytesForKey = true;
}
key = new byte[32];
iv = new byte[16];
concatenatedHashes.copyTo(0,32);
concatenatedHashes.copyTo(32,iv,16);
md5.Clear();
}
static string DecryptStringFromByteSAEs(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("iv");
// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext;
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged { Mode = CipherMode.CBC,Padding = PaddingMode.None,KeySize = 256,BlockSize = 128,Key = key,IV = iv };
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key,aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
srDecrypt.Close();
}
}
}
return plaintext;
}
请参考上面的代码,c#以与openssl_decrypt相同的方式解密,我在问是否会基于c#和PHP产生相同的结果,是否有任何c#代码返回与PHP中openssl_decrypt()相同的行为>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。