Click here to Skip to main content
11,706,323 members (47,027 online)
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)

  Print Answers RSS
0 OriginalGriff 197
1 Sergey Alexandrovich Kryukov 141
2 chainerlt 110
3 ppolymorphe 71
4 Mika Wendelius 70
0 OriginalGriff 9,070
1 Sergey Alexandrovich Kryukov 8,413
2 CPallini 5,189
3 Maciej Los 4,746
4 Mika Wendelius 3,676


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2015
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