Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi,
I am creating a create_user page and an user_login page using asp.net. I am trying to encrypt the password during creating the user(insert password into table) and decrypt the password during login(retrive password from table).
Will you pls help me in encrypting and decrypting the password ?
Posted 16-Oct-12 18:27pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

using System.Security.Cryptography;
 
public static string Encrypt(string strEncrypt)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(strEncrypt);
 
            if (true)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes("123"));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes("123");
 
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;
 
            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
 
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
 
        public static string Decrypt(string strDecrypt)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(strDecrypt);
 
            if (true)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes("123"));
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes("123");
 
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;
 
            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
 
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
  Permalink  
v2
Comments
NayakMamuni at 18-Oct-12 9:39am
   
thanks for it...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Refer this Link[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

While the two solutions you have been given are what you asked for, please don't use this approach
 
It's much more secure to store a hash of the password at the server end, than any symmetrically encrypted alternative
 
When the password is created, the server stores a hash
When the password is entered for authentication, only the hash is sent to the server for comparison
 
The Key to a Secure Password[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100