【发布时间】:2014-07-12 05:57:05
【问题描述】:
我需要知道如何加密 SQL Server 数据库中的数据。我使用了各种代码,但在某些程序中 AES 类不存在,我搜索了解决方案,我尝试了很多次才得到它。需要帮助。一切正常,但Aes 出现红色下划线,我没有任何Aes 加密器类。
我使用了这个代码。
private void InsertQuery()
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand("Insert into data(username,password,age) values (@username,@password,@age)", con);
try
{
if (txtUsername.Text == "")
{
MessageBox.Show("Error ! \n Please enter Username");
}
else if (txtPassword.Text == "")
{
MessageBox.Show("Error ! \n Please enter Password");
}
else if (Convert.ToSingle(txtAge.Text) > 99)
{
MessageBox.Show("Error ! \n Please enter Age Correctly");
}
else
{
con.Open();
cmd.Parameters.AddWithValue("@username", txtUsername.Text.Trim());
cmd.Parameters.AddWithValue("@password",Encryptdata(txtPassword.Text.Trim()));
cmd.Parameters.AddWithValue("@age", txtAge.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("SUCCESS!");
con.Dispose();
}
}
catch(Exception e)
{
MessageBox.Show("Error! Age is not valid!");
}
}
private string Encryptdata(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
private string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
/// <summary>
/// Function is used to Decrypt the password
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
private string Decryptdata(string encryptpwd)
{
string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
decryptpwd = new String(decoded_char);
return decryptpwd;
}
【问题讨论】:
标签: sql encryption aes c#-2.0 sql-server-express