1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System;
24: using System.IO;
25: using System.Security.Cryptography;
26: using System.Text;
27: #endregion
28:
29: namespace Utilities.Encryption
30: {
31: /// <summary>
32: /// Utility class that handles encryption
33: /// </summary>
34: public static class AESEncryption
35: {
36: #region Static Functions
37:
38: /// <summary>
39: /// Encrypts a string
40: /// </summary>
41: /// <param name="PlainText">Text to be encrypted</param>
42: /// <param name="Password">Password to encrypt with</param>
43: /// <param name="Salt">Salt to encrypt with</param>
44: /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
45: /// <param name="PasswordIterations">Number of iterations to do</param>
46: /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
47: /// <param name="KeySize">Can be 128, 192, or 256</param>
48: /// <returns>An encrypted string</returns>
49: public static string Encrypt(string PlainText, string Password,
50: string Salt = "Kosher", string HashAlgorithm = "SHA1",
51: int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY",
52: int KeySize = 256)
53: {
54: if (string.IsNullOrEmpty(PlainText))
55: return "";
56: byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
57: byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
58: byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
59: PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
60: byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
61: RijndaelManaged SymmetricKey = new RijndaelManaged();
62: SymmetricKey.Mode = CipherMode.CBC;
63: byte[] CipherTextBytes = null;
64: using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
65: {
66: using (MemoryStream MemStream = new MemoryStream())
67: {
68: using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
69: {
70: CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
71: CryptoStream.FlushFinalBlock();
72: CipherTextBytes = MemStream.ToArray();
73: MemStream.Close();
74: CryptoStream.Close();
75: }
76: }
77: }
78: SymmetricKey.Clear();
79: return Convert.ToBase64String(CipherTextBytes);
80: }
81:
82: /// <summary>
83: /// Decrypts a string
84: /// </summary>
85: /// <param name="CipherText">Text to be decrypted</param>
86: /// <param name="Password">Password to decrypt with</param>
87: /// <param name="Salt">Salt to decrypt with</param>
88: /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
89: /// <param name="PasswordIterations">Number of iterations to do</param>
90: /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
91: /// <param name="KeySize">Can be 128, 192, or 256</param>
92: /// <returns>A decrypted string</returns>
93: public static string Decrypt(string CipherText, string Password,
94: string Salt = "Kosher", string HashAlgorithm = "SHA1",
95: int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY",
96: int KeySize = 256)
97: {
98: if (string.IsNullOrEmpty(CipherText))
99: return "";
100: byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
101: byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
102: byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
103: PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
104: byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
105: RijndaelManaged SymmetricKey = new RijndaelManaged();
106: SymmetricKey.Mode = CipherMode.CBC;
107: byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
108: int ByteCount = 0;
109: using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
110: {
111: using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
112: {
113: using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
114: {
115:
116: ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
117: MemStream.Close();
118: CryptoStream.Close();
119: }
120: }
121: }
122: SymmetricKey.Clear();
123: return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
124: }
125:
126: #endregion
127: }
128: }