I am sending the simple example that encrypt n decrypt my database string.
I put them in one file and decrypt it later.
Hope this will help
using System.Security;
using System.Security.Cryptography;
public void encrypt(String raw_data)
{
// The DB string that we want to encrypt/decrypt:
String strConnectionString = raw_data;
// Call the custom method for encrypting the string:
this.EncryptDBString(strConnectionString);
}
// This is a button click event - to unencrypt the saved DB string:
public string decrypt()
{
String strConnectionString = this.DecryptDBString();
// Do something with the unencrypted DB string ...
return(strConnectionString);
}
// This method will encrypt the provided DB string to disk
private void EncryptDBString(String DBString)
{
/**************** Encrypt/Protect Database String ****************/
// Convert the string to a byte array:
byte[] arrDBString =
System.Text.Encoding.Unicode.GetBytes(DBString);
// Provide additional protection via entropy with another byte
array:
byte[] arrEntropy = { 4, 5, 7, 9, 4, 5, 7, 9 }; // Save this for
unprotecting later
// Encrypt/protect the DB string:
byte[] arrEncryptedDBString = ProtectedData.Protect(arrDBString,
arrEntropy,DataProtectionScope.CurrentUser);
// Write the encrypted DB string to disk:
using (FileStream fs = new FileStream(_file,
FileMode.OpenOrCreate))
{
fs.Write(arrEncryptedDBString, 0, arrEncryptedDBString.Length);
}
}
// This method will decrypt the saved encrypted DB string from file
private String DecryptDBString()
{
/**************** Decrypt/Unprotect Database String
****************/
// Setup the unencrypted DB string to return:
String strUnencryptedDBString = null;
// Provide additional protection via entropy with another byte
array:
byte[] arrEntropy = { 4, 5, 7, 9, 4, 5, 7, 9 }; // Save this for
protecting later
// Setup the byte array that will hold the encrypted DB string:
byte[] arrEncryptedDBString = new byte[0];
// Read the encrypted DB string from disk:
if (File.Exists(_file))
{
using (FileStream fs = new FileStream(_file, FileMode.Open))
{
// Reset the byte array's length based on read length:
arrEncryptedDBString = new byte[fs.Length];
// Read the encrypted file into the byte array:
fs.Read(arrEncryptedDBString, 0, (int) fs.Length);
}
}
if (arrEncryptedDBString.Length > 0)
{
// Decrypt/unprotect the DB string:
byte[] arrUnencryptedDBString =
ProtectedData.Unprotect(arrEncryptedDBString,arrEntropy,
DataProtectionScope.CurrentUser);
// Convert the byte array to a string:
strUnencryptedDBString =
System.Text.Encoding.Unicode.GetString(arrUnencryptedDBString);
}
// Return the unencrypted DB string:
return strUnencryptedDBString;
}
}
[EMAIL PROTECTED] wrote:
>
> public static string GetEncriptedPassword(string plainText)
> {
> return
> FormsAuthentication.HashPasswordForStoringInConfigFile(plainText, "SHA1");
> }
>
> On Fri, Nov 14, 2008 at 5:18 PM, karthi keyan <[EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
>
> Hi Vikas,
>
> When I searched msdn, I found it can be done using
> System.Security.Cryptography;
>
> But I am not aware of how to do this... I am very new to c#
>
>
>
>
>
> On Fri, Nov 14, 2008 at 3:46 PM, VIKAS GARG
> <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:
>
> That you can do by using the inbuilt class used for encryption.
> C# provides inbuilt class for doing the encryption
>
>
> On Fri, Nov 14, 2008 at 3:38 PM, karthi keyan
> <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:
>
> Hi,
>
> I have just designed a registration fom (Windows
> application) in C#. I am using Ms-access for storing the
> user information. I need to store the password entered by
> the user in a encrypted manner.
>
> Can any one help me out / guide me on this?
>
> Regards,
> Karthikeyan
>
>
>
>
>
>
> --
> Raju.M
> Snr:Software Engineer
> 3kInfoTech
> [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> m +919995117011
> of +16783673348
> http://www.makhaai.blogspot.com