Ask a Question related to ASP.NET Security, Design and Development.
-
Bernie #1
Symmetric encryption using password
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(stringToEncryp t);
//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,bytesToDec rypt.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.
Bernie Guest
-
Datasource password encryption
I need an answer for the following: Does MX7 encrypt the password stored for a Data Source created within coldfusion? If so, what encryption does it... -
Encryption - DSN password
I need an answer for the following. Does MX7 encrypt the password stored for a Data Source created within coldfusion? If so, what encryption does... -
Account creations (password encryption)
I am creating a new login system for a website to the administrator functions and I've never created encrypted passwords before. Any simple methods... -
achieve password encryption
Hello folks, how can I encrypt the password that an user enters in my normal login window. Thank you Anton -
Password encryption
I've experimented with this and it works well. http://www.aspin.com/func/search?hidden1x=23&hidden1y=4&tree=aspin&qry=cast128&cat= Also,...



Reply With Quote

