相当于c#中的DecryptByPassPhrase
本文关键字:DecryptByPassPhrase 中的 相当于 | 更新日期: 2023-09-27 18:02:50
我需要解密一个使用EncryptByPassPhrase在数据库上加密的值,但不访问数据库。
如何从密码短语中获得加密密钥?
我看过
在c#中复制T-SQL DecryptByPassPhrase和
c#解密字节从SQL Server EncryptByPassPhrase?
,我的代码是:
public static string AESDatabaseDecrypt(string encryptedString)
{
passphrase = "S0meFakePassPhrase01234!";
encryptedString = "AQAAAOmuc52dnbVwTqEx1kp+4WhI89LYKHh3jg=="; // temporarily hard coded
// setup encryption settings to match decryptbypassphrase
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.Key = UTF8Encoding.UTF8.GetBytes(passphrase).Take(16).ToArray(); // stuck on getting key from passphrase
provider.KeySize = 128;
provider.Padding = PaddingMode.Zeros;
// setup data to be decrypted
byte[] encryptedStringAsByteArray = Convert.FromBase64String(encryptedString);
// hack some extra bytes up to a multiple of 8
encryptedStringAsByteArray = encryptedStringAsByteArray.Concat(new byte[] { byte.MinValue, byte.MinValue, byte.MinValue, byte.MinValue }).ToArray(); // add 4 empty bytes to make 32 bytes
MemoryStream encryptedStringAsMemoryStream = new MemoryStream(encryptedStringAsByteArray);
// decrypt
CryptoStream cryptoStream = new CryptoStream(encryptedStringAsMemoryStream, provider.CreateDecryptor(), CryptoStreamMode.Read);
// return the result
StreamReader cryptoStreamReader = new StreamReader(cryptoStream);
string decryptedString = cryptoStreamReader.ReadToEnd();
}
我的意思是,我看到的第一件事是你实际上并没有从你的功能返回一个值。尝试将return decryptedString;
添加到末尾,看看会得到什么。