I was provided this code on EE and it has worked well for me. But I am curious of the name of the algorithm.
Also, what basic technology is it based on?
How hard would it be for someone to break it? How does it compare to PGP grade encryption?
thanks,
newbieweb
class EncryptIt
{
// Encrypt a string into a string using a password
// Uses Encrypt(byte[], byte[], byte[])
public static string Encrypt(string clearText, string Password)
{
byte[] clearBytes =
System.Text.Encoding.Unico
de.GetByte
s(clearTex
t);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Passwo
rd, System.Text.ASCIIEncoding.
UTF8.GetBy
tes(salter
GUID));
byte[] encryptedData = Encrypt(clearBytes,
pdb.GetBytes(32), pdb.GetBytes(16));
return Convert.ToBase64String(enc
ryptedData
);
}
// Decrypt a string into a string using a password
// Uses Decrypt(byte[], byte[], byte[])
public static string Decrypt(string cipherText, string Password)
{
byte[] cipherBytes = Convert.FromBase64String(c
ipherText)
;
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Passwo
rd, System.Text.ASCIIEncoding.
UTF8.GetBy
tes(salter
GUID));
byte[] decryptedData = Decrypt(cipherBytes,
pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unico
de.GetStri
ng(decrypt
edData);
}
#region internal methods
internal static string salterGUID = "{7DBA82CF-DEB2-4c6f-AF6E-
FD9C02B0E1
EA}";
// Encrypt a byte array into a byte array using a key and an IV
internal static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
// Create a MemoryStream to accept the encrypted bytes
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms,
alg.CreateEncryptor(), CryptoStreamMode.Write);
// Write the data and make it do the encryption
cs.Write(clearData, 0, clearData.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
return encryptedData;
}
// Encrypt bytes into bytes using a password
// Uses Encrypt(byte[], byte[], byte[])
internal static byte[] Encrypt(byte[] clearData, string Password)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Passwo
rd, System.Text.ASCIIEncoding.
UTF8.GetBy
tes(salter
GUID));
return Encrypt(clearData, pdb.GetBytes(32), pdb.GetBytes(16));
}
// Decrypt a byte array into a byte array using a key and an IV
internal static byte[] Decrypt(byte[] cipherData,
byte[] Key, byte[] IV)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms,
alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
byte[] decryptedData = ms.ToArray();
return decryptedData;
}
#endregion
}
Start Free Trial