I want to create an ASP.Net application with C# and I’ll store a data on SQL server 2005, these data will be encrypted I want to find an algorithm to Encrypt a data with C# and decrypt it on SQL serve side and I want to Encrypt a some data with SQL and decrypt it with C# what is the best algorithm for this ??
private byte[] key = {
0x61,
0x72,
0x84,
0x7a,
0x24,
0x43,
0x65,
0x64,
0x73,
0x55,
0x64,
0x75,
0x66
};
const string PASSWORD = "TestPassword";
public object Encrypt(string sPlainText)
{
byte[] aPlainBytes = null;
PasswordDeriveBytes aPassword = default(PasswordDeriveBytes);
aPlainBytes = System.Text.Encoding.Unicode.GetBytes(sPlainText);
aPassword = new PasswordDeriveBytes(PASSWORD, key);
byte[] sEncryptedData = Encrypt(aPlainBytes, aPassword.GetBytes(32), aPassword.GetBytes(16));
//' MessageBox.Show(Convert.ToString(sEncryptedData.ToString))
return Convert.ToBase64String(sEncryptedData);
}
private byte[] Encrypt(byte[] sPlainData, byte[] aKey, byte[] aIV)
{
MemoryStream oMemoryStream = new MemoryStream();
Rijndael oRijndael = Rijndael.Create();
oRijndael.Key = aKey;
oRijndael.IV = aIV;
CryptoStream oCryptoStream = new CryptoStream(oMemoryStream, oRijndael.CreateEncryptor(), CryptoStreamMode.Write);
oCryptoStream.Write(sPlainData, 0, sPlainData.Length);
oCryptoStream.Close();
byte[] aEncryptedData = oMemoryStream.ToArray();
return aEncryptedData;
}
C#: System.Security.Cryptography
SQL Server: Sql Server Encryption
C# Example from here:
SQL Server Example from here: