· 8 years ago · Jan 03, 2018, 10:38 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Security.Cryptography;
7using System.IO;
8
9namespace Common
10{
11 public class Encryption
12 {
13 //Admin password Test123
14 public string HashString(string mytext)
15 {
16 SHA256 myAlg = SHA256.Create();//initialize the algorithm instance
17
18 byte[] input = Encoding.UTF32.GetBytes(mytext); //converting from string to byte[]
19 byte[] digest = myAlg.ComputeHash(input); //converting from string to byte[]
20
21 return Convert.ToBase64String(digest); //converting back from byte[] to string
22 }
23
24 public byte[] HashBytes(byte[] input)
25 {
26 SHA256 myAlg = SHA256.Create();
27 byte[] digest = myAlg.ComputeHash(input);
28 return digest;
29 }
30
31 public SymetricParamaters GenerateSymmetricParamaters(string input)
32 {
33 Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(input, new byte[] { 20, 255, 1, 60, 54, 78, 52, 30 });
34
35 Rijndael myAlg = Rijndael.Create();
36
37 SymetricParamaters myParams = new SymetricParamaters()
38 {
39 SecretKey = rfc.GetBytes(myAlg.KeySize / 8),
40 IV = rfc.GetBytes(myAlg.BlockSize / 8)
41 };
42
43 return myParams;
44 }
45
46 //Still needs to be implemented in Index.cshtml
47 public string EncryptString(string input, string passwordToBeUsedInSecretKey)
48 {
49 SymetricParamaters myParameters = GenerateSymmetricParamaters(passwordToBeUsedInSecretKey);
50
51 Rijndael myAlg = Rijndael.Create();
52 myAlg.Key = myParameters.SecretKey;
53 myAlg.IV = myParameters.IV;
54
55 byte[] clearDataAsBytes = Encoding.UTF32.GetBytes(input);
56
57 MemoryStream msClearData = new MemoryStream(clearDataAsBytes);
58 CryptoStream cs = new CryptoStream(msClearData, myAlg.CreateEncryptor(), CryptoStreamMode.Read);
59
60 MemoryStream msEncrypteData = new MemoryStream();
61 cs.CopyTo(msEncrypteData);
62
63 byte[] encrypteDataAsBytes = msEncrypteData.ToArray();
64 string encrypteddata = Convert.ToBase64String(encrypteDataAsBytes); //converting to string the cryptographic data
65
66 encrypteddata = encrypteddata.Replace('=', '|');
67 encrypteddata = encrypteddata.Replace('/', '_');
68 encrypteddata = encrypteddata.Replace('+', '*');
69
70 return encrypteddata;
71 }
72
73 public string DecryptString(string input, string passwordToBeUsedInSecretKey)
74 {
75
76 input = input.Replace('|', '=');
77 input = input.Replace('_', '/');
78 input = input.Replace('*', '+');
79
80 //Need to change below
81 SymetricParamaters myParameters = GenerateSymmetricParamaters(passwordToBeUsedInSecretKey);
82
83 Rijndael myAlg = Rijndael.Create();
84 myAlg.Key = myParameters.SecretKey;
85 myAlg.IV = myParameters.IV;
86
87 byte[] encryptedDataAsBytes = Convert.FromBase64String(input); //Make decoding;
88
89 MemoryStream msEncryptedData = new MemoryStream(encryptedDataAsBytes);
90 CryptoStream cs = new CryptoStream(msEncryptedData, myAlg.CreateDecryptor(), CryptoStreamMode.Read);
91
92 MemoryStream msClearData = new MemoryStream();
93 cs.CopyTo(msClearData);
94
95 byte[] clearDataAsBytes = msClearData.ToArray();
96 string clearData = Encoding.UTF32.GetString(clearDataAsBytes); //converting to string the cryptographic data
97
98 return clearData;
99 }
100
101 public AsymmetricParameters GenerateAsymmetricParameters()
102 {
103 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
104 AsymmetricParameters myParameters = new AsymmetricParameters();
105 myParameters.PublicKey = rsa.ToXmlString(false);
106 myParameters.PrivateKey = rsa.ToXmlString(true);
107
108 return myParameters;
109 }
110
111 public static MemoryStream SymmetricallyEncryptFile(Stream input, SymetricParamaters parameters)
112 {
113 //initializing the algorithm
114 Rijndael myAlg = Rijndael.Create();
115
116 //importing the generated key and iv in the current algorithm
117 myAlg.IV = parameters.IV;
118 myAlg.Key = parameters.SecretKey;
119
120 //creating an instance of the stream that will encrypt/decrypt the data
121 CryptoStream myEncryptor = new CryptoStream(input, myAlg.CreateEncryptor(), CryptoStreamMode.Read);
122
123 //preparing an empty stream in memory where to store the encrypted data
124 MemoryStream msOut = new MemoryStream();
125 //copying the encrypted data to this empty space
126 myEncryptor.CopyTo(msOut);
127
128 return msOut;
129 }
130
131 public static MemoryStream SymmetricallyDecryptFile(Stream input, SymetricParamaters parameters)
132 {
133
134 Rijndael myAlg = Rijndael.Create();
135 input.Position = 0;
136
137 myAlg.Key = parameters.SecretKey;
138 myAlg.IV = parameters.IV;
139
140
141 CryptoStream myEncryptor = new CryptoStream(input, myAlg.CreateDecryptor(), CryptoStreamMode.Read);
142
143
144 MemoryStream msOut = new MemoryStream();
145
146 myEncryptor.CopyTo(msOut);
147 msOut.Position = 0;
148
149 return msOut;
150 }
151
152 public static byte[] EncryptAsymmetricallyTheKey(byte[] input, string publicKey)
153 {
154 RSACryptoServiceProvider myAlg = new RSACryptoServiceProvider();
155 myAlg.FromXmlString(publicKey);
156
157 byte[] outputAsBytes = myAlg.Encrypt(input, true);
158
159 return outputAsBytes;
160 }
161
162 public static byte[] DecryptAsymmetricallyTheKey(byte[] input, string privateKey)
163 {
164 RSACryptoServiceProvider myAlg = new RSACryptoServiceProvider();
165 myAlg.FromXmlString(privateKey);
166
167 byte[] outputAsBytes = myAlg.Decrypt(input, true);
168
169 return outputAsBytes;
170 }
171
172 public Stream HybridEncryptFile(Stream article, string username, string publicKey)
173 {
174 article.Position = 0;
175 SymetricParamaters myParameters = GenerateSymmetricParamaters(username);
176
177 Stream encryptedFile = SymmetricallyEncryptFile(article, myParameters);
178 encryptedFile.Position = 0;
179
180 byte[] encryptedSecretKey = EncryptAsymmetricallyTheKey(myParameters.SecretKey, publicKey);
181 byte[] encryptedIv = EncryptAsymmetricallyTheKey(myParameters.IV, publicKey);
182
183
184 MemoryStream myEncryptedFile = new MemoryStream();
185 myEncryptedFile.Write(encryptedSecretKey, 0, encryptedSecretKey.Length);
186 myEncryptedFile.Write(encryptedIv, 0, encryptedIv.Length);
187 encryptedFile.CopyTo(myEncryptedFile);
188
189 return myEncryptedFile;
190 }
191
192 public MemoryStream HybridDecryptFile(Stream article, string privateKey)
193 {
194 article.Position = 0;
195
196 byte[] encryptedSecretKey = new byte[128];
197 article.Read(encryptedSecretKey, 0, 128);
198
199 byte[] encIv = new byte[128];
200 article.Read(encIv, 0, 128);
201
202 byte[] encFile = new byte[article.Length - 256];
203 article.Read(encFile, 0, encFile.Length);
204
205 byte[] decryptedSecretKey = new byte[128];
206 decryptedSecretKey= DecryptAsymmetricallyTheKey(encryptedSecretKey, privateKey);
207
208 byte[] decryptedIv = new byte[128];
209 decryptedIv = DecryptAsymmetricallyTheKey(encIv, privateKey);
210
211 MemoryStream msOut = new MemoryStream(encFile); //contains the decrypted data
212 msOut.Position = 0;
213
214 SymetricParamaters myParameters = new SymetricParamaters() { SecretKey = decryptedSecretKey, IV = decryptedIv };
215 MemoryStream decryptedFile = SymmetricallyDecryptFile(msOut, myParameters);
216 decryptedFile.Position = 0;
217
218 return decryptedFile;
219 }
220
221 public string SignFile(Stream myFile, string privateKey)
222 {
223 RSACryptoServiceProvider myAlg = new RSACryptoServiceProvider();
224 myAlg.FromXmlString(privateKey);
225
226 MemoryStream ms = new MemoryStream();
227 myFile.CopyTo(ms);
228 ms.Position = 0;
229 byte[] myFileArray = ms.ToArray();
230
231 byte[] digest = HashBytes(myFileArray);
232
233 byte[] signature = myAlg.SignHash(digest, "SHA256");
234
235 return Convert.ToBase64String(signature);
236 }
237
238 public bool VerifyFile(Stream myFile, string publicKey, string signature)
239 {
240 //declare alg
241 RSACryptoServiceProvider myAlg = new RSACryptoServiceProvider();
242 //import the public key
243 myAlg.FromXmlString(publicKey);
244
245 //hash the file
246 MemoryStream ms = new MemoryStream();
247 myFile.CopyTo(ms);
248 ms.Position = 0;
249 byte[] myFileArray = ms.ToArray();
250 byte[] digest = HashBytes(myFileArray);
251
252 //convert back to byte[] the signature Convert.FromBase64String
253
254 byte[] signatureBytes = Convert.FromBase64String(signature);
255 //call the method myAlg.VerifyHash(hashdata, "alg name", signature)
256
257 bool verified = myAlg.VerifyHash(digest, "SHA256", signatureBytes);
258
259 return verified;
260 }
261 }
262 public class SymetricParamaters
263 {
264 public byte[] SecretKey { get; set; }
265 public byte[] IV { get; set; }
266 }
267
268 public class AsymmetricParameters
269 {
270 public string PublicKey { get; set; }
271 public string PrivateKey { get; set; }
272 }
273}