Symmetric encryption using password

B

Bernie

this is a simple class for encrypting and decrypting a string with a
password.



public class CryptoManager {

public CryptoManager(){
}

private const int BLOCK_SIZE_BITS = 128;

public static string Encrypt(string stringToEncrypt, string
password){

string res = null;
//Generate a byte array from stringToEncrypt.
Byte[] bytesToEncrypt =
new System.Text.UTF8Encoding().GetBytes(stringToEncrypt);
//Genarate a memory stream on which cryptographic transformation
will be performed.
MemoryStream memStream = new System.IO.MemoryStream();

try {
//Generate a symmetric algorithem.
SymmetricAlgorithm crypto = SymmetricAlgorithm.Create("Rijndael");
try {
//Generate a key from password and assign it to algorithem.
crypto.Key = CreateKeyFromPassword(password);
crypto.BlockSize = BLOCK_SIZE_BITS; // This is 128 in this
particular case
//Generates the initialization vector to be used by the
algorithem.
crypto.GenerateIV();

crypto.Mode = CipherMode.ECB;
crypto.Padding = PaddingMode.PKCS7;

//Generates a stream which links data streams to cryptographic
transformations.
CryptoStream encryptionStream = new CryptoStream(memStream,
crypto.CreateEncryptor(crypto.Key, crypto.IV),
CryptoStreamMode.Write);
//Write transformation to byte array.
encryptionStream.Write(bytesToEncrypt, 0, bytesToEncrypt.Length);
//Close the CryptoStream.
encryptionStream.Close();
//Generate an encrypted byte array.
Byte[] encryptedArray = memStream.ToArray();
//Convert byte encrypted byte array to string.
res = System.Convert.ToBase64String(encryptedArray);
//return encrypted stream.
return res;
}
finally {
crypto.Clear();
}
}
finally {
if (memStream != null)
memStream.Close();
}

}

public static string Decrypt(string stringToDecrypt, string
password){

string res = null;
//Generate stringTodecrypte as byte array.
Byte[] bytesToDecrypt = Convert.FromBase64String(stringToDecrypt);
//Genarate a memory stream on which cryptographic transformation
will be performed.
MemoryStream memStream = new System.IO.MemoryStream();
try {
SymmetricAlgorithm crypto = SymmetricAlgorithm.Create("Rijndael");
try {
//Generate a key from password and assign it to algorithem.
crypto.Key = CreateKeyFromPassword(password);
crypto.BlockSize = BLOCK_SIZE_BITS; // This is 128 in this
particular case
//Generates the initialization vector to be used by the
algorithem.
crypto.GenerateIV();

crypto.Mode = CipherMode.ECB;
crypto.Padding = PaddingMode.PKCS7;

//Generates a stream which links data streams to cryptographic
transformations.
CryptoStream decryptionStream =
new CryptoStream(memStream,
crypto.CreateDecryptor(crypto.Key, crypto.IV),
CryptoStreamMode.Write);
//Write transformation to byte array.
decryptionStream.Write(bytesToDecrypt,0,bytesToDecrypt.Length);
decryptionStream.FlushFinalBlock();
//Close the CryptoStream.
decryptionStream.Close();

//assign decrypted stream to res.
res = Encoding.UTF8.GetString(memStream.ToArray());
//return result.
return res;

} finally {
crypto.Clear();
}

} finally {
if (memStream!=null)
memStream.Close();
}

}

private static byte[] CreateKeyFromPassword(string password) {
SHA256Managed hasher = new SHA256Managed();
byte[] pwdBytes = new
System.Text.UTF8Encoding().GetBytes(password);
return hasher.ComputeHash(pwdBytes);
}
}

Hope this can help anyone,

Bernie

This posting is provided "AS IS" with no warranties, and confers no
rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top