· 7 years ago · May 03, 2018, 04:00 AM
1public static string DecryptFile(string secretKey, string inputFile, string outputFile)
2 {
3 try
4 {
5 // We are using an AES Crypto with ECB and PKCS7 Padding
6 using (RijndaelManaged aes = new RijndaelManaged())
7 {
8 byte[] key = ASCIIEncoding.UTF8.GetBytes(secretKey);
9 byte[] IV = ASCIIEncoding.UTF8.GetBytes(secretKey);
10
11 aes.Mode = CipherMode.ECB;
12 aes.Padding = PaddingMode.PKCS7;
13
14 using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open))
15 {
16 using (FileStream fsOut = new FileStream(outputFile, FileMode.Create))
17 {
18 using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV))
19 {
20 Console.WriteLine(decryptor.ToString());
21 using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read))
22 {
23 int data;
24 while ((data = cs.ReadByte()) != -1)
25 {
26 fsOut.WriteByte((byte)data);
27 // Console.WriteLine((byte)data);
28 }
29 cs.Flush();
30 cs.Close();
31 fsOut.Flush();
32 fsOut.Close();
33 fsCrypt.Close();
34 }
35 }
36 }
37 }
38 }
39 }
40
41 catch (Exception ex)
42 {
43 // failed to decrypt file
44 Console.WriteLine("Errors : "+ex.Message);
45 }
46 return "success";
47 }