1 public class RijndaelHelper
2 {
3 private byte[] _IV = null;
4 private byte[] _key = null;
5 private Rijndael _rijndael = null;
6 private string _lastExceptionDescription = "";
7
8 public string LastExceptionDescription
9 {
10 get { return _lastExceptionDescription; }
11 }
12
13 public RijndaelHelper()
14 {
15 _rijndael = Rijndael.Create();
16 }
17
18 public RijndaelHelper(byte[] key, byte[] IV)
19 {
20 this._key = key;
21 this._IV = IV;
22 _rijndael = Rijndael.Create();
23 }
24
25
26 /// <summary>
27 /// Esegue la cifratura dei dati
28 /// </summary>
29 /// <param name="data">Array di byte con i dati che devono essere cifrati</param>
30 /// <returns>Array di byte con i dati cifrati</returns>
31 public byte[] EncryptData(byte[] data)
32 {
33 try
34 {
35 byte[] encryptedData = null;
36
37 _lastExceptionDescription = "";
38
39 using (MemoryStream memStream = new MemoryStream())
40 {
41
42 //Crea un'istanza di oggetto Cryptographic per l'esecuzione dell'algoritmo di Rijndael
43 _rijndael = Rijndael.Create();
44
45 //Crea una nuova istanza CrypoStream in modalità scrittura
46 CryptoStream cryptoStream = new CryptoStream(memStream,
47 _rijndael.CreateEncryptor(_key, _IV), CryptoStreamMode.Write);
48
49 //Scrive i dati cifrati
50 cryptoStream.Write(data, 0, data.Length);
51 cryptoStream.FlushFinalBlock();
52
53 encryptedData = memStream.ToArray();
54
55 //Chiude gli stream
56 cryptoStream.Close();
57 memStream.Close();
58 }
59
60 return encryptedData;
61 }
62 catch (CryptographicException ex)
63 {
64 _lastExceptionDescription = ex.Message;
65 return null;
66 }
67 catch (System.Exception ex)
68 {
69 _lastExceptionDescription = ex.Message;
70 return null;
71 }
72 }
73
74 /// <summary>
75 /// Esegue la decifratura dei dati
76 /// </summary>
77 /// <param name="encryptedData">Array di byte contenente i dati da decifrare</param>
78 /// <returns>Array di byte con i dati decifrati</returns>
79 public byte[] DecryptData(byte[] encryptedData)
80 {
81 try
82 {
83 byte[] decryptedData = new byte[encryptedData.Length];
84 using (MemoryStream memStream = new MemoryStream(encryptedData, 0, encryptedData.Length))
85 {
86 _rijndael = Rijndael.Create();
87
88 //Crea una nuova istanza CrypoStream in modalità lettura
89 CryptoStream cryptoStream = new CryptoStream(memStream,
90 _rijndael.CreateDecryptor(_key, _IV), CryptoStreamMode.Read);
91
92 //Decifra i dati nell'array decryptedData
93 cryptoStream.Read(decryptedData, 0, encryptedData.Length);
94
95 //Chiude gli stream
96 cryptoStream.Close();
97 memStream.Close();
98 }
99
100 return decryptedData;
101 }
102 catch (CryptographicException ex)
103 {
104 _lastExceptionDescription = ex.Message;
105 return null;
106 }
107 catch (System.Exception ex)
108 {
109 _lastExceptionDescription = ex.Message;
110 return null;
111 }
112 }
113 }
1 byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 15, 16};
2 byte[] k = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
3 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
4
5 RijndaelHelper helper = new RijndaelHelper(k, iv);
6
7 byte[] a = helper.EncryptData(System.Text.UTF8Encoding.UTF8.GetBytes ("Che bella cosa è na jurnata e sole"));
8 MessageBox.Show(System.Text.UTF8Encoding.UTF8.GetString (helper.DecryptData(a)));