1: /// <summary>
2: /// Encrypts a string
3: /// </summary>
4: /// <param name="Data">Text to be encrypted</param>
5: /// <param name="Key">Password to encrypt with</param>
6: /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param>
7: /// <param name="Salt">Salt to encrypt with</param>
8: /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
9: /// <param name="PasswordIterations">Number of iterations to do</param>
10: /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
11: /// <param name="KeySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param>
12: /// <param name="EncodingUsing">Encoding that the original string is using (defaults to UTF8)</param>
13: /// <returns>An encrypted string (Base 64 string)</returns>
14: public static string Encrypt(this string Data, string Key,
15: Encoding EncodingUsing = null,
16: SymmetricAlgorithm AlgorithmUsing = null, string Salt = "Kosher",
17: string HashAlgorithm = "SHA1", int PasswordIterations = 2,
18: string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
19: {
20: if (string.IsNullOrEmpty(Data))
21: return "";
22: return Data.ToByteArray(EncodingUsing).Encrypt(Key, AlgorithmUsing, Salt, HashAlgorithm, PasswordIterations, InitialVector, KeySize).ToBase64String();
23: }
24:
25: /// <summary>
26: /// Encrypts a byte array
27: /// </summary>
28: /// <param name="Data">Data to be encrypted</param>
29: /// <param name="Key">Password to encrypt with</param>
30: /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param>
31: /// <param name="Salt">Salt to encrypt with</param>
32: /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
33: /// <param name="PasswordIterations">Number of iterations to do</param>
34: /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
35: /// <param name="KeySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param>
36: /// <returns>An encrypted byte array</returns>
37: public static byte[] Encrypt(this byte[] Data, string Key,
38: SymmetricAlgorithm AlgorithmUsing = null, string Salt = "Kosher",
39: string HashAlgorithm = "SHA1", int PasswordIterations = 2,
40: string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
41: {
42: if (Data == null)
43: return null;
44: if (AlgorithmUsing == null)
45: AlgorithmUsing = new RijndaelManaged();
46: if (string.IsNullOrEmpty(Key))
47: throw new ArgumentNullException("Key");
48: if (string.IsNullOrEmpty(Salt))
49: throw new ArgumentNullException("Salt");
50: if (string.IsNullOrEmpty(HashAlgorithm))
51: throw new ArgumentNullException("HashAlgorithm");
52: if (string.IsNullOrEmpty(InitialVector))
53: throw new ArgumentNullException("InitialVector");
54: using (PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Key, Salt.ToByteArray(), HashAlgorithm, PasswordIterations))
55: {
56: using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing)
57: {
58: SymmetricKey.Mode = CipherMode.CBC;
59: byte[] CipherTextBytes = null;
60: using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
61: {
62: using (MemoryStream MemStream = new MemoryStream())
63: {
64: using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
65: {
66: CryptoStream.Write(Data, 0, Data.Length);
67: CryptoStream.FlushFinalBlock();
68: CipherTextBytes = MemStream.ToArray();
69: MemStream.Close();
70: CryptoStream.Close();
71: }
72: }
73: }
74: SymmetricKey.Clear();
75: return CipherTextBytes;
76: }
77: }
78: }