Link to home
Start Free TrialLog in
Avatar of logicthought
logicthought

asked on

Ecrypt Password C#

Hi Experts, i need a simple solution .
i have a program i wrote in C# the requiers gmail user name and password inorder to send email notification to the user.
i currntly store these details is the registry , i would like to encrypt the password.
so if someone explores the registry using regedit he won't be able to extract the password.

this is in C# (Visual Studio 2010)
Thanks
ASKER CERTIFIED SOLUTION
Avatar of msimn
msimn
Flag of Thailand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or, you might try a salted base-64 encode as the following example.

Note that you might want to change the string manipulation part to anything you could think of.
public static string EncodeTo64(string toEncode)
    {
      byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
      string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
      return (new Random()).Next(0, 9) + returnValue + (new Random()).Next(0, 9);
    }

public static string DecodeFrom64(string encodedData)
    {
      encodedData = encodedData.substring(1, encodedData.Length - 2);
      byte[] encodedDataAsBytes
          = System.Convert.FromBase64String(encodedData);
      string returnValue =
         System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
      return returnValue;
    }

public static void main(String[] args)
    {
      string encodedPassword = EncodeTo64("MyPassword");
      Console.WriteLine("Encoded password = " + encodedPassword);

      string decodedPassword = DecodeFrom64(encodedPassword);
      Console.WriteLine("Decoded password = " + decodedPassword);
    }

Open in new window

I hope this works for u.. I am attaching a .cs file with my comment plz do check it..
EncryptDecrypt.cs
Compile this above file using the command line compiler which comes with visual studio.. as follows :
csc /t:winexe EncryptDecrypt.cs