Skip to content

Password encryption

Isham Khan

Asked Aug 22, 2014, 6:31 AM EDT2 answers
Share
LinkedInX
how to encrypt and decrypt password,,,, i am using c# visual studio 2012 front end and sql server 2005 back end thank u

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    C#
     public string Encrypt(string plainText)     {         if (plainText == null) throw new ArgumentNullException("plainText");          //encrypt data         var data = Encoding.Unicode.GetBytes(plainText);         byte[] encrypted = ProtectedData.Protect(data, null, Scope);          //return as base64 string         return Convert.ToBase64String(encrypted);     }
    
     public string Decrypt(string cipher)     {         if (cipher == null) throw new ArgumentNullException("cipher");          //parse base64 string         byte[] data = Convert.FromBase64String(cipher);          //decrypt data         byte[] decrypted = ProtectedData.Unprotect(data, null, Scope);         return Encoding.Unicode.GetString(decrypted);     }
    0
  • 0
    sorry my above post formatting looks odd, refer this clean code
    Please click checkbox "This is correct answer" if you find it helpful!

    public string Encrypt(string plainText)
        {
            if (plainText == null) throw new ArgumentNullException("plainText");

            //encrypt data
            var data = Encoding.Unicode.GetBytes(plainText);
            byte[] encrypted = ProtectedData.Protect(data, null, Scope);

            //return as base64 string
            return Convert.ToBase64String(encrypted);
        }

     public string Decrypt(string cipher)
        {
            if (cipher == null) throw new ArgumentNullException("cipher");

            //parse base64 string
            byte[] data = Convert.FromBase64String(cipher);

            //decrypt data
            byte[] decrypted = ProtectedData.Unprotect(data, null, Scope);
            return Encoding.Unicode.GetString(decrypted);
        }


    0