Password Encrypt and Decrypt Utility

Password Encrypt and Decrypt Utility

Developing a secure application you need to apply security features, like securing your password. Explaining the how to encrypt and decrypt password. First create static class name is PasswordUtility. Here I am using SHA1 algorithm you can use the MD5 algorithm also. This is 16 bit encryption.

public static class PasswordUtility

{

private static string strPassPhrase = “pa$$w0rd”;// can be any string

private static string strSaltValue = “s@1tValue”;// can be any string

private static string strHashAlgorithm = “SHA1”;// can be “MD5”

private static int intPasswordIterations = 2; // can be any number

private static string strInitVector = “@1B2c3D4e5F6g7H8”;

// must be 16 bytes

private static int intKeySize = 128; // can be 192 or 128

// Here calling Encrypt method, the given string(password)

// Parameter string to encrypt

//return type is encrypted password

public static string Encrypt(string plainPassword)

{

return Encrypt(plainPassword, strPassPhrase, strSaltValue, strHashAlgorithm, intPasswordIterations, strInitVector, intKeySize);

}

// Encrypt the given string(password)

//Parameters are Password to encrypt,can be any string,can be any //string,MD5,no of iterations,must be 16 bytes,keysize 256,128 or 192

//Return type encrypted value

public static string Encrypt(string plainText,

string passPhrase,

string saltValue,

string hashAlgorithm,

int passwordIterations,

string initVector,

int keySize)

{

// Convert strings into byte arrays.

// Let us assume that strings only contain ASCII codes.

// If strings include Unicode characters, use Unicode, UTF7, or UTF8

// encoding.

byte[] byInitVectorBytes = Encoding.ASCII.GetBytes(strInitVector);

byte[] bySaltValueBytes = Encoding.ASCII.GetBytes(strSaltValue);

// Convert our plaintext into a byte array.

// Let us assume that plaintext contains UTF8-encoded characters.

byte[] byPlainTextBytes = Encoding.UTF8.GetBytes(plainText);

// First, we must create a password, from which the key will be derived.

// This password will be generated from the specified passphrase and

// salt value. The password will be created using the specified hash

// algorithm. Password creation can be done in several iterations.

PasswordDeriveBytes password = new PasswordDeriveBytes(

passPhrase,

bySaltValueBytes,

hashAlgorithm,

passwordIterations);

// Use the password to generate pseudo-random bytes for the encryption

// key. Specify the size of the key in bytes (instead of bits).

byte[] byKeyBytes = password.GetBytes(keySize / 8);

// Create uninitialized Rijndael encryption object.

RijndaelManaged symmetricKey = new RijndaelManaged();

// It is reasonable to set encryption mode to Cipher Block Chaining

// (CBC). Use default options for other symmetric key parameters.

symmetricKey.Mode = CipherMode.CBC;

// Generate encryptor from the existing key bytes and initialization

// vector. Key size will be defined based on the number of the key

// bytes.

ICryptoTransform encryptor = symmetricKey.CreateEncryptor(

byKeyBytes,

byInitVectorBytes);

// Define memory stream which will be used to hold encrypted data.

MemoryStream memoryStream = new MemoryStream();

// Define cryptographic stream (always use Write mode for encryption).

CryptoStream cryptoStream = new CryptoStream(memoryStream,

encryptor,

CryptoStreamMode.Write);

// Start encrypting.

cryptoStream.Write(byPlainTextBytes, 0, byPlainTextBytes.Length);

// Finish encrypting.

cryptoStream.FlushFinalBlock();

// Convert our encrypted data from a memory stream into a byte array.

byte[] byCipherTextBytes = memoryStream.ToArray();

// Close both streams.

memoryStream.Close();

cryptoStream.Close();

// Convert encrypted data into a base64-encoded string.

string strCipherText = Convert.ToBase64String(byCipherTextBytes);

// Return encrypted string.

return strCipherText;

}

//Calling Decrypts method the given string(password)

//Parameter is Encrypted string

//Retrun type is decrypted string

// If condition was added to check empty and null

public static string Decrypt(string encryptedPassword)

{

if (encryptedPassword == “” || encryptedPassword == null)

return string.Empty;

return Decrypt(encryptedPassword, strPassPhrase, strSaltValue, strHashAlgorithm, intPasswordIterations, strInitVector, intKeySize);

}

public static string Decrypt(string cipherText,

string passPhrase,

string saltValue,

string hashAlgorithm,

int passwordIterations,

string initVector,

int keySize)

{

// Convert strings defining encryption key characteristics into byte

// arrays. Let us assume that strings only contain ASCII codes.

// If strings include Unicode characters, use Unicode, UTF7, or UTF8

// encoding.

byte[] byInitVectorBytes = Encoding.ASCII.GetBytes(strInitVector);

byte[] bySaltValueBytes = Encoding.ASCII.GetBytes(strSaltValue);

// Convert our ciphertext into a byte array.

byte[] byCipherTextBytes = Convert.FromBase64String(cipherText);

// First, we must create a password, from which the key will be

// derived. This password will be generated from the specified

// passphrase and salt value. The password will be created using

// the specified hash algorithm. Password creation can be done in

// several iterations.

PasswordDeriveBytes password = new PasswordDeriveBytes(

passPhrase,

bySaltValueBytes,

hashAlgorithm,

passwordIterations);

// Use the password to generate pseudo-random bytes for the encryption

// key. Specify the size of the key in bytes (instead of bits).

byte[] byKeyBytes = password.GetBytes(keySize / 8);

// Create uninitialized Rijndael encryption object.

RijndaelManaged symmetricKey = new RijndaelManaged();

// It is reasonable to set encryption mode to Cipher Block Chaining

// (CBC). Use default options for other symmetric key parameters.

symmetricKey.Mode = CipherMode.CBC;

// Generate decryptor from the existing key bytes and initialization

// vector. Key size will be defined based on the number of the key

// bytes.

ICryptoTransform decryptor = symmetricKey.CreateDecryptor(

byKeyBytes,

byInitVectorBytes);

// Define memory stream which will be used to hold encrypted data.

MemoryStream memoryStream = new MemoryStream(byCipherTextBytes);

// Define cryptographic stream (always use Read mode for encryption).

CryptoStream cryptoStream = new CryptoStream(memoryStream,

decryptor,

CryptoStreamMode.Read);

// Since at this point we don’t know what the size of decrypted data

// will be, allocate the buffer long enough to hold ciphertext;

// plaintext is never longer than ciphertext.

byte[] byPlainTextBytes = new byte[byCipherTextBytes.Length];

// Start decrypting.

int iDecryptedByteCount = cryptoStream.Read(byPlainTextBytes, 0, byPlainTextBytes.Length);

// Close both streams.

memoryStream.Close();

cryptoStream.Close();

// Convert decrypted data into a string.

// Let us assume that the original plaintext string was UTF8-encoded.

string strPlainText = Encoding.UTF8.GetString(byPlainTextBytes, 0, iDecryptedByteCount);

// Return decrypted string.

return strPlainText;

}

}//Class ends here

You can call the these methods like this

PasswordUtility.Encrypt(strUserPwd)

PasswordUtility.Decrypt(strUserPwd)

Leave a comment