[英]Encrypting And Decrypting a string using Aes Encryption - C#
I wanna store an encrypted string inside a SQL Database as a byte array and I can't figure out what I'm doing wrong. 我想将一个加密的字符串作为字节数组存储在SQL数据库中,但无法弄清楚自己在做什么错。 The code is this:
代码是这样的:
private void loginBtn_Click(object sender, EventArgs e)
{
try
{
string password = passwordBox.Text.ToString();
using (Aes algorithm = Aes.Create())
{
byte[] encryptedPassword = EncryptString(password, algorithm.Key, algorithm.IV);
string roundTrip = DecryptString(encryptedPassword, algorithm.Key, algorithm.IV);
MessageBox.Show("Encrypted Password: " + encryptedPassword.ToString() + '\n' + "Round Trip: " + roundTrip.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
And the code used for the 'EncryptString' and 'DecryptString' is the one from Microsoft's Aes Class Reference (the Example situated at the end of the page). 用于'EncryptString'和'DecryptString'的代码是Microsoft的Aes类参考 (该示例位于页面末尾)中的代码。
I executed my code and all it gives me in a Message Box is this: 我执行了我的代码,在消息框中提供的所有内容是这样的:
Encrypted Password: System.Byte[]
加密密码:System.Byte []
Round Trip: (empty space)
往返:(空白处)
static byte[] EncryptString(string str, byte[] key, byte[] IV)
{
if (str == null || str.Length <= 0)
throw new ArgumentNullException("string");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
using (Aes algorithm = Aes.Create())
{
algorithm.Key = key;
algorithm.IV = IV;
ICryptoTransform encryptor = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(str);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
static string DecryptString(byte[] cipher, byte[] key, byte[] IV)
{
if (cipher == null || cipher.Length <= 0)
throw new ArgumentNullException("cipher");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
string decrypted;
using (Aes algorithm = Aes.Create())
{
algorithm.Key = key;
algorithm.IV = IV;
ICryptoTransform decryptor = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);
using (MemoryStream msDecrypt = new MemoryStream())
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
decrypted = srDecrypt.ReadToEnd();
}
}
}
}
return decrypted;
}
Can someone help me fix it, please? 有人可以帮我解决这个问题吗?
PS The Text Box has the Password Char setted to * PS文本框的密码字符设置为*
In DecryptString
method, you forgot to pass cipher
parameter to constructor of msDecrypt
memory stream as an input, thus method actually deciphers empty input stream, so result is empty too. 在
DecryptString
方法中,您忘记将cipher
参数传递给msDecrypt
内存流的构造函数作为输入,因此该方法实际上解密了空的输入流,因此结果也为空。
Line 线
using (MemoryStream msDecrypt = new MemoryStream())
should actually be: 实际上应该是:
using (MemoryStream msDecrypt = new MemoryStream(cipher))
and then everything works fine. 然后一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.