1+ using System ;
2+ using System . IO ;
3+ using System . Security . Cryptography ;
4+ using System . Text ;
5+
6+ namespace ebOS
7+ {
8+ public static class Encrypt
9+ {
10+ // This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be
11+ // 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array.
12+ private const string initVector = "hfwlpghybmfaaptl" ;
13+ // This constant is used to determine the keysize of the encryption algorithm
14+ private const int keysize = 256 ;
15+ //Encrypt
16+ public static string EncryptString ( string plainText , string passPhrase )
17+ {
18+ byte [ ] initVectorBytes = Encoding . UTF8 . GetBytes ( initVector ) ;
19+ byte [ ] plainTextBytes = Encoding . UTF8 . GetBytes ( plainText ) ;
20+ PasswordDeriveBytes password = new PasswordDeriveBytes ( passPhrase , null ) ;
21+ byte [ ] keyBytes = password . GetBytes ( keysize / 8 ) ;
22+ RijndaelManaged symmetricKey = new RijndaelManaged ( ) ;
23+ symmetricKey . Mode = CipherMode . CBC ;
24+ ICryptoTransform encryptor = symmetricKey . CreateEncryptor ( keyBytes , initVectorBytes ) ;
25+ MemoryStream memoryStream = new MemoryStream ( ) ;
26+ CryptoStream cryptoStream = new CryptoStream ( memoryStream , encryptor , CryptoStreamMode . Write ) ;
27+ cryptoStream . Write ( plainTextBytes , 0 , plainTextBytes . Length ) ;
28+ cryptoStream . FlushFinalBlock ( ) ;
29+ byte [ ] cipherTextBytes = memoryStream . ToArray ( ) ;
30+ memoryStream . Close ( ) ;
31+ cryptoStream . Close ( ) ;
32+ return Convert . ToBase64String ( cipherTextBytes ) ;
33+ }
34+ //Decrypt
35+ public static string DecryptString ( string cipherText , string passPhrase )
36+ {
37+ byte [ ] initVectorBytes = Encoding . UTF8 . GetBytes ( initVector ) ;
38+ byte [ ] cipherTextBytes = Convert . FromBase64String ( cipherText ) ;
39+ PasswordDeriveBytes password = new PasswordDeriveBytes ( passPhrase , null ) ;
40+ byte [ ] keyBytes = password . GetBytes ( keysize / 8 ) ;
41+ RijndaelManaged symmetricKey = new RijndaelManaged ( ) ;
42+ symmetricKey . Mode = CipherMode . CBC ;
43+ ICryptoTransform decryptor = symmetricKey . CreateDecryptor ( keyBytes , initVectorBytes ) ;
44+ MemoryStream memoryStream = new MemoryStream ( cipherTextBytes ) ;
45+ CryptoStream cryptoStream = new CryptoStream ( memoryStream , decryptor , CryptoStreamMode . Read ) ;
46+ byte [ ] plainTextBytes = new byte [ cipherTextBytes . Length ] ;
47+ int decryptedByteCount = cryptoStream . Read ( plainTextBytes , 0 , plainTextBytes . Length ) ;
48+ memoryStream . Close ( ) ;
49+ cryptoStream . Close ( ) ;
50+ return Encoding . UTF8 . GetString ( plainTextBytes , 0 , decryptedByteCount ) ;
51+ }
52+ }
53+ }