locked
Encrypt and decrypt the password in asp.net using c# RRS feed

  • Question

  • Step 1:-  Add the below Namespaces

    using System.Security.Cryptography;
    using System.IO;

    Step 2:- paste the code inside the Partial Class 

        public static byte[] KEY_64 = { 42, 16, 93, 156, 78, 4, 218, 32 };
        public static byte[] IV_64 = { 55, 103, 246, 79, 36, 99, 167, 3 };

    Step 3:- Code for Encrypt the String Value

    public static string Encryption(string value)
        {
            //byte[] KEY_64 = { 42, 16, 93, 156, 78, 4, 218, 32 };
            //byte[] IV_64 = { 55, 103, 246, 79, 36, 99, 167, 3 };
            if (value != "")
            {
                DESCryptoServiceProvider CryptoProvidor = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, CryptoProvidor.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cs);
                sw.Write(value);
                sw.Flush();
                cs.FlushFinalBlock();
                ms.Flush();
                return Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
            }
            return string.Empty;
        }

    Step 4 :- Code for Decrypt the Encrypt value

    Step 5:-

      public static string Decryption(string value)
      {
          byte[] KEY_64 = { 42, 16, 93, 156, 78, 4, 218, 32 };
          byte[] IV_64 = { 55, 103, 246, 79, 36, 99, 167, 3 };
          if (value != "")
          {
              DESCryptoServiceProvider CryptoProvidor = new DESCryptoServiceProvider();
              Byte[] buf = Convert.FromBase64String(value);
              MemoryStream ms = new MemoryStream(buf);
              CryptoStream cs = new CryptoStream(ms, CryptoProvidor.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read);
              StreamReader sr = new StreamReader(cs);
              return sr.ReadToEnd();
          }
          return string.Empty;
      }

     


    • Edited by Mohamed sithik Tuesday, February 5, 2013 12:34 PM forgot something
    Tuesday, February 5, 2013 12:02 PM

Answers

  • http://chandradev819.wordpress.com/2011/04/11/how-to-encrypt-and-decrypt-password-in-asp-net-using-c/

    thanks and regard love4csharp

    • Proposed as answer by Bob Shen Wednesday, February 6, 2013 5:00 AM
    • Marked as answer by Bob Shen Thursday, February 7, 2013 4:48 AM
    Tuesday, February 5, 2013 4:09 PM

All replies

  • Hi,

    Unclear. What is the question or issue you have ? Else please use a "discussion" rather than a "question"...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Marked as answer by Mohamed sithik Tuesday, February 5, 2013 12:31 PM
    • Unmarked as answer by Mohamed sithik Tuesday, February 5, 2013 12:31 PM
    Tuesday, February 5, 2013 12:17 PM
  • http://chandradev819.wordpress.com/2011/04/11/how-to-encrypt-and-decrypt-password-in-asp-net-using-c/

    thanks and regard love4csharp

    • Proposed as answer by Bob Shen Wednesday, February 6, 2013 5:00 AM
    • Marked as answer by Bob Shen Thursday, February 7, 2013 4:48 AM
    Tuesday, February 5, 2013 4:09 PM