我需要使用EncryptByPassPhrase解密在数据库上加密的值,但不能访问数据库。
如何从密码短语中获取加密密钥?
我看了
Replicate T-SQL DecryptByPassPhrase in C#
和
C# Decrypt bytes from 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();
}
答案 0 :(得分:0)
嗯,我的意思是,跳出来的第一件事就是你实际上并没有从你的方法中返回一个值。尝试将return decryptedString;
添加到最后,看看你得到了什么。