Component Page /Crypto.cs (C#)
1: using System;  
2: using System.Text;  
3: using System.Security.Cryptography;  
4: using System.IO;  
5: using System.Configuration;  
6:   
7: namespace QuizNetOnline.Logic  
8: {  
9:     public class Crypto : ICrypto  
10:     {  
11:         public static string Encrypt(string s,ICrypto crypto)  
12:         {  
13:             string encryptedString;  
14:   
15:   
16:             // Initialize Rijndael cryptography with app settings values  
17:             RijndaelManaged rm = new RijndaelManaged();  
18:             rm.Key = crypto.HexStringToByteArray(ConfigurationManager.AppSettings["rmk"]);  
19:             rm.IV = crypto.HexStringToByteArray(ConfigurationManager.AppSettings["rmv"]);  
20:   
21:             // Create the encryptor  
22:             ICryptoTransform encryptor = rm.CreateEncryptor(rm.Key, rm.IV);  
23:   
24:             // Write data out to the crypto stream  
25:             MemoryStream msEncrypt = new MemoryStream();  
26:             CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);  
27:   
28:             try  
29:             {  
30:                 byte[] bytesToEncrypt = Encoding.UTF8.GetBytes(s);  
31:                 csEncrypt.Write(bytesToEncrypt, 0, bytesToEncrypt.Length);  
32:                 csEncrypt.FlushFinalBlock();  
33:   
34:                 byte[] encryptedBytes = msEncrypt.ToArray();  
35:                 encryptedString = Convert.ToBase64String(encryptedBytes);  
36:             }  
37:             finally  
38:             {  
39:                 msEncrypt.Close();  
40:                 csEncrypt.Close();  
41:             }  
42:   
43:             return encryptedString;  
44:         }  
45:   
46:         public static string Decrypt(string s, ICrypto crypto)  
47:         {  
48:             string decryptedString;  
49:             s = s.Replace(" ", "+");  
50:   
51:             // Initialize Rijndael cryptography with app settings values  
52:             RijndaelManaged rm = new RijndaelManaged();  
53:             rm.Key = crypto.HexStringToByteArray(ConfigurationManager.AppSettings["rmk"]);  
54:             rm.IV = crypto.HexStringToByteArray(ConfigurationManager.AppSettings["rmv"]);  
55:   
56:             // Create the decryptor  
57:             ICryptoTransform decryptor = rm.CreateDecryptor(rm.Key, rm.IV);  
58:   
59:             byte[] bytesToDecrypt = Convert.FromBase64String(s);  
60:             MemoryStream msDecrypt = new MemoryStream(bytesToDecrypt);  
61:             CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);  
62:   
63:             try  
64:             {  
65:                 // Read the data out of the crypto stream  
66:                 byte[] decryptedBytes = new byte[bytesToDecrypt.Length];  
67:                 int decryptedByteCount = csDecrypt.Read(decryptedBytes, 0, decryptedBytes.Length);  
68:   
69:                 decryptedString = Encoding.UTF8.GetString(decryptedBytes, 0, decryptedByteCount);  
70:             }  
71:   
72:             finally  
73:             {  
74:                 msDecrypt.Close();  
75:                 csDecrypt.Close();  
76:             }  
77:   
78:             return decryptedString;  
79:         }  
80:   
81:         public byte[] HexStringToByteArray(string s)  
82:         {  
83:             int numBytes = s.Length / 2;  
84:             byte[] bytes = new byte[s.Length / 2];  
85:             int iStart = 0;  
86:             for (int i = 0; i < numBytes; ++i, iStart = iStart + 2)  
87:             {  
88:                 bytes[i] = Convert.ToByte(s.Substring(iStart, 2), 16);  
89:             }  
90:   
91:             return bytes;  
92:         }  
93:     }  
94: }  

.NET Documentation Tool is © 2002 - 2009 Winnersh Triangle Web Solutions Limited.