Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i created a web application with asp.net mvc4 C#, but i don't know how to handle password for user registeration. how can i write a encrypt to save the password into database (sql server) and also decrypt password for verification before sign in into the web. any reference for me?
Posted
Comments
[no name] 22-Jul-15 2:35am    
are you using any kinda ORM?
Athul MS 22-Jul-15 2:54am    
no

1 solution

C#
public static string Encrypt(string toEncrypt, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
    System.Configuration.AppSettingsReader settingsReader = 
                                        new AppSettingsReader();
    string key = (string)settingsReader.GetValue("SecurityKey", 
                                                     typeof(String));
    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        hashmd5.Clear();
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
    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);
    tdes.Clear();
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}



C#
public static string Decrypt(string cipherString, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = Convert.FromBase64String(cipherString);
    System.Configuration.AppSettingsReader settingsReader = 
                                        new AppSettingsReader();
    string key = (string)settingsReader.GetValue("SecurityKey", 
                                                 typeof(String));
            
    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        hashmd5.Clear();
    }
    else
    {
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
    }
    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);            
    tdes.Clear();
    return UTF8Encoding.UTF8.GetString(resultArray);
}

use this code to encrypt decrypt password. store the value as encrypted format and retrieve the password from DB and decrypt it and compare with user entered value.Here i am using tripleDES algorthim in cryptography

thats all
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900