May 16, 2013

Password Encryption Decryption


To secure the data of any type is the critical issue now a days. There are plenty of method available on internet to secure your data from breaching / hacking. Developers around the world using different techniques to secure the password of their applications. In this post I add another method to secure your passwords.

------
C# :
------

------------------
NameSpaces :
------------------

using System.Security.Cryptography;
using System.Configuration;
using System.Data.SqlClient;

------------------
Initialization : 
------------------

const string passphrase = "password";

---------------
Encryption :
---------------

private static string EncryptPassword(string password)
{
    byte[] results;
    var uTF8Encoding = new UTF8Encoding();
    var HashProvider = new MD5CryptoServiceProvider();
    byte[] TDESKey = HashProvider.ComputeHash(uTF8Encoding.GetBytes(passphrase));
    var tDESAlgorithm = new TripleDESCryptoServiceProvider();

    tDESAlgorithm.Key = TDESKey;
    tDESAlgorithm.Mode = CipherMode.ECB;
    tDESAlgorithm.Padding = PaddingMode.PKCS7;
    byte[] dataToEncrypt = uTF8Encoding.GetBytes(password);

    try
    {
        ICryptoTransform Encryptor = tDESAlgorithm.CreateEncryptor();
        results = Encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
    }
    finally
    {
        tDESAlgorithm.Clear();
        HashProvider.Clear();
    }

    return Convert.ToBase64String(results);
}

---------------
Decryption :
---------------

private static string DecryptPassword(string password)
{
    byte[] results;
    var uTF8Encoding = new UTF8Encoding();
    var hashProvider = new MD5CryptoServiceProvider();
    byte[] tDESKey = hashProvider.ComputeHash(uTF8Encoding.GetBytes(passphrase));
    var tDESAlgorithm = new TripleDESCryptoServiceProvider();

    tDESAlgorithm.Key = tDESKey;
    tDESAlgorithm.Mode = CipherMode.ECB;
    tDESAlgorithm.Padding = PaddingMode.PKCS7;
    byte[] dataToDecrypt = Convert.FromBase64String(password);

    try
    {
        ICryptoTransform Decryptor = tDESAlgorithm.CreateDecryptor();
        results = Decryptor.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);
    }
    finally
    {
        tDESAlgorithm.Clear();
        hashProvider.Clear();
    }

    return uTF8Encoding.GetString(results);
}

-------------
VB.Net :
-------------

-----------------
Initialization :
--------------------

Imports System.Security.Cryptography
Imports System.Configuration
Imports System.Data.SqlClient


---------------
Encryption :
---------------


Private Shared Function EncryptPassword(ByVal password As String) As String
 Dim results As Byte()
 Dim uTF8Encoding = New UTF8Encoding()
 Dim HashProvider = New MD5CryptoServiceProvider()
 Dim TDESKey As Byte() = HashProvider.ComputeHash(uTF8Encoding.GetBytes(passphrase))
 Dim tDESAlgorithm = New TripleDESCryptoServiceProvider()

 tDESAlgorithm.Key = TDESKey
 tDESAlgorithm.Mode = CipherMode.ECB
 tDESAlgorithm.Padding = PaddingMode.PKCS7
 Dim dataToEncrypt As Byte() = uTF8Encoding.GetBytes(password)

 Try
  Dim Encryptor As ICryptoTransform = tDESAlgorithm.CreateEncryptor()
  results = Encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length)
 Finally
  tDESAlgorithm.Clear()
  HashProvider.Clear()
 End Try

 Return Convert.ToBase64String(results)
End Function



------------------
Dencryption :
------------------


Private Shared Function DecryptPassword(ByVal password As String) As String
 Dim results As Byte()
 Dim uTF8Encoding = New UTF8Encoding()
 Dim hashProvider = New MD5CryptoServiceProvider()
 Dim tDESKey As Byte() = hashProvider.ComputeHash(uTF8Encoding.GetBytes(passphrase))
 Dim tDESAlgorithm = New TripleDESCryptoServiceProvider()

 tDESAlgorithm.Key = tDESKey
 tDESAlgorithm.Mode = CipherMode.ECB
 tDESAlgorithm.Padding = PaddingMode.PKCS7
 Dim dataToDecrypt As Byte() = Convert.FromBase64String(password)

 Try
  Dim Decryptor As ICryptoTransform = tDESAlgorithm.CreateDecryptor()
  results = Decryptor.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length)
 Finally
  tDESAlgorithm.Clear()
  hashProvider.Clear()
 End Try

 Return uTF8Encoding.GetString(results)
End Function

No comments:

Post a Comment