In the UI of my winForm I ask the user for a user name and password. I store this password as text in the Settings (the .settings file) of my project. The app is a visual St
I dd it with this:
public static string Decrypt(string stringToDecrypt)
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = byteConverter.GetBytes(stringToDecrypt);
byte[] decryptedData = null;
try
{
using (RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider())
{
rsaCryptoServiceProvider.FromXmlString(_key);
byte[] decryptBytes = Encoding.Default.GetBytes(Properties.Settings.Default.SqlPassword);
decryptedData = rsaCryptoServiceProvider.Decrypt(decryptBytes, false);
}
}
catch (Exception ex)
{
//TODO Do proper logging
Console.WriteLine("Decrypt failed: " + ex.Message);
}
return byteConverter.GetString(decryptedData);
}
public static string Encrypt(string stringToEncrypt)
{
try
{
UnicodeEncoding byteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = byteConverter.GetBytes(stringToEncrypt);
using (RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider())
{
rsaCryptoServiceProvider.FromXmlString(_key);
byte[] encryptedData = rsaCryptoServiceProvider.Encrypt(dataToEncrypt, false);
return Encoding.Default.GetString(encryptedData);
}
}
catch (Exception ex)
{
Console.WriteLine("Encrypt failed: " + ex.Message);
}
}