|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.IO; |
| 6 | +using System.Security.Cryptography; |
| 7 | + |
| 8 | +namespace EncryptorDecryptorRijndael.Encrypt |
| 9 | +{ |
| 10 | + sealed internal class Decryptor : BaseEncrypt |
| 11 | + { |
| 12 | + public Decryptor(string inputFile, string outputFile, string password) |
| 13 | + : base(inputFile, outputFile, password) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + public void decrypt(object s, System.ComponentModel.DoWorkEventArgs e) |
| 18 | + { |
| 19 | + System.ComponentModel.BackgroundWorker worker = s as System.ComponentModel.BackgroundWorker; |
| 20 | + loadData(); |
| 21 | + worker.ReportProgress(10); |
| 22 | + if (worker.CancellationPending) { e.Cancel = true; return; } |
| 23 | + |
| 24 | + byte[] header, cipherText; |
| 25 | + splitDataIntoHeaderAndCiphertext(out header, out cipherText); |
| 26 | + worker.ReportProgress(25); |
| 27 | + if (worker.CancellationPending) { e.Cancel = true; return; } |
| 28 | + |
| 29 | + parseHeader(header); |
| 30 | + worker.ReportProgress(40); |
| 31 | + if (worker.CancellationPending) { e.Cancel = true; return; } |
| 32 | + |
| 33 | + encryptedData = cipherText; |
| 34 | + decryptCipherText(s, e); |
| 35 | + worker.ReportProgress(90); |
| 36 | + if (worker.CancellationPending) { e.Cancel = true; return; } |
| 37 | + |
| 38 | + saveData(); |
| 39 | + worker.ReportProgress(100); |
| 40 | + } |
| 41 | + |
| 42 | + private void decryptCipherText(object s, System.ComponentModel.DoWorkEventArgs e) |
| 43 | + { |
| 44 | + System.ComponentModel.BackgroundWorker worker = s as System.ComponentModel.BackgroundWorker; |
| 45 | + ICryptoTransform decryptor = cipherRijndael.CreateDecryptor(); |
| 46 | + |
| 47 | + // Create the streams used for decryption. |
| 48 | + using (MemoryStream msDecrypt = new MemoryStream(encryptedData)) |
| 49 | + { |
| 50 | + worker.ReportProgress(60); |
| 51 | + using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) |
| 52 | + { |
| 53 | + worker.ReportProgress(65); |
| 54 | + |
| 55 | + using (BinaryReader srDecrypt = new BinaryReader(csDecrypt)) |
| 56 | + { |
| 57 | + worker.ReportProgress(70); |
| 58 | + decryptedData = srDecrypt.ReadAllBytes(); |
| 59 | + //csDecrypt.FlushFinalBlock(); |
| 60 | + |
| 61 | + worker.ReportProgress(90); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void parseHeader(byte[] header) |
| 68 | + { |
| 69 | + string str = Encoding.ASCII.GetString(header); |
| 70 | + |
| 71 | + int start, end; |
| 72 | + start = str.IndexOf("<IV>") + "<IV>".Length; |
| 73 | + end = str.IndexOf("</IV>") - 1; |
| 74 | + string iv = (str.Substring(start, end - start + 1)); |
| 75 | + |
| 76 | + start = str.IndexOf("<EncryptedKey>") + "<EncryptedKey>".Length; |
| 77 | + end = str.IndexOf("</EncryptedKey>") - 1; |
| 78 | + string encryptedKey = (str.Substring(start, end - start + 1)); |
| 79 | + |
| 80 | + |
| 81 | + start = str.IndexOf("<BlockSize>") + "<BlockSize>".Length; |
| 82 | + end = str.IndexOf("</BlockSize>") - 1; |
| 83 | + string blocksize = (str.Substring(start, end - start + 1)); |
| 84 | + |
| 85 | + start = str.IndexOf("<FeedbackSize>") + "<FeedbackSize>".Length; |
| 86 | + end = str.IndexOf("</FeedbackSize>") - 1; |
| 87 | + string feedbacksize = (str.Substring(start, end - start + 1)); |
| 88 | + |
| 89 | + start = str.IndexOf("<CipherMode>") + "<CipherMode>".Length; |
| 90 | + end = str.IndexOf("</CipherMode>") - 1; |
| 91 | + string mode = (str.Substring(start, end - start + 1)); |
| 92 | + |
| 93 | + start = str.IndexOf("<KeySize>") + "<KeySize>".Length; |
| 94 | + end = str.IndexOf("</KeySize>") - 1; |
| 95 | + string keySize = (str.Substring(start, end - start + 1)); |
| 96 | + |
| 97 | + cipherRijndael.Mode = (CipherMode)Enum.Parse(typeof(CipherMode), mode); |
| 98 | + cipherRijndael.FeedbackSize = int.Parse(feedbacksize); |
| 99 | + cipherRijndael.BlockSize = int.Parse(blocksize); |
| 100 | + cipherRijndael.KeySize = int.Parse(keySize); |
| 101 | + cipherRijndael.IV = stringToByteArray(iv); |
| 102 | + var key = decryptEncryptedKey(stringToByteArray(encryptedKey)); |
| 103 | + cipherRijndael.Key = key; |
| 104 | + } |
| 105 | + |
| 106 | + private byte[] decryptEncryptedKey(byte[] encryptedKey) |
| 107 | + { |
| 108 | + ICryptoTransform decryptor = encryptedKeyRijndael.CreateDecryptor(); |
| 109 | + byte[] decryptedKey; |
| 110 | + // Create the streams used for decryption. |
| 111 | + using (MemoryStream msDecrypt = new MemoryStream(encryptedKey)) |
| 112 | + { |
| 113 | + using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) |
| 114 | + { |
| 115 | + using (BinaryReader srDecrypt = new BinaryReader(csDecrypt)) |
| 116 | + { |
| 117 | + decryptedKey = srDecrypt.ReadAllBytes(); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return decryptedKey; |
| 122 | + } |
| 123 | + |
| 124 | + private byte[] stringToByteArray(string source) |
| 125 | + { |
| 126 | + string[] strings = source.Split('.'); |
| 127 | + byte[] bytes = new byte[strings.Length]; |
| 128 | + |
| 129 | + for (int i = 0; i < strings.Length; i++) |
| 130 | + bytes[i] = byte.Parse(strings[i]); |
| 131 | + return bytes; |
| 132 | + } |
| 133 | + |
| 134 | + private void splitDataIntoHeaderAndCiphertext(out byte[] header, out byte[] cipherText) |
| 135 | + { |
| 136 | + int first = 0, last = 0; |
| 137 | + /* for (int i = 0; i < encryptedData.Length; i++) |
| 138 | + { |
| 139 | + if (encryptedData[i] == '<' && encryptedData[i + 1] == 'h' && encryptedData[i + 2] == 'e' && encryptedData[i + 3] == 'a' && encryptedData[i + 4] == 'd') |
| 140 | + { |
| 141 | + first = i; |
| 142 | + } |
| 143 | + else if (encryptedData[i] == '<' && encryptedData[i + 1] == '/' && encryptedData[i + 2] == 'h' && encryptedData[i + 3] == 'e' && encryptedData[i + 4] == 'a' && encryptedData[i + 5] == 'd') |
| 144 | + { |
| 145 | + last = i + 8; |
| 146 | + break; |
| 147 | + } |
| 148 | + if (i > encryptedData.Length - 9) |
| 149 | + throw new Exception("Encrypted file is corrupted or not compatible, cannot decrypt"); |
| 150 | + }*/ |
| 151 | + first = encryptedData.Locate(Encoding.ASCII.GetBytes("<EncryptedFileHeader>")); |
| 152 | + last = encryptedData.Locate(Encoding.ASCII.GetBytes("</EncryptedFileHeader>")) + "</EncryptedFileHeader>".Length-1; |
| 153 | + |
| 154 | + if (first==-1 || last==-1) |
| 155 | + throw new Exception("Encrypted file is corrupted or not compatible, cannot decrypt"); |
| 156 | + |
| 157 | + header = new byte[last - first + 1]; |
| 158 | + cipherText = new byte[encryptedData.Length - last - 1]; |
| 159 | + Array.Copy(encryptedData, first, header, 0, last - first + 1); |
| 160 | + var strtr = Encoding.ASCII.GetString(header); |
| 161 | + //System.Windows.Forms.MessageBox.Show(Encoding.ASCII.GetString(header)); |
| 162 | + Array.Copy(encryptedData, last + 1, cipherText, 0, encryptedData.Length - last - 1); |
| 163 | + |
| 164 | + // System.Windows.Forms.MessageBox.Show(Encoding.ASCII.GetString(header)); |
| 165 | + } |
| 166 | + |
| 167 | + protected override void loadData() |
| 168 | + { |
| 169 | + encryptedData = File.ReadAllBytes(inputFilePath); |
| 170 | + } |
| 171 | + |
| 172 | + protected override void saveData() |
| 173 | + { |
| 174 | + File.WriteAllBytes(outputFilePath, decryptedData); |
| 175 | + } |
| 176 | + |
| 177 | + private byte[] decryptedData; |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + public static class ByteArrayExtension |
| 184 | + { |
| 185 | + static readonly int[] Empty = new int[0]; |
| 186 | + |
| 187 | + public static int Locate(this byte[] self, byte[] candidate) |
| 188 | + { |
| 189 | + if (IsEmptyLocate(self, candidate)) |
| 190 | + return -1; |
| 191 | + |
| 192 | + for (int i = 0; i < self.Length; i++) |
| 193 | + { |
| 194 | + if (!IsMatch(self, i, candidate)) |
| 195 | + continue; |
| 196 | + |
| 197 | + return i; |
| 198 | + } |
| 199 | + |
| 200 | + return -1; |
| 201 | + } |
| 202 | + |
| 203 | + static bool IsMatch(byte[] array, int position, byte[] candidate) |
| 204 | + { |
| 205 | + if (candidate.Length > (array.Length - position)) |
| 206 | + return false; |
| 207 | + |
| 208 | + for (int i = 0; i < candidate.Length; i++) |
| 209 | + if (array[position + i] != candidate[i]) |
| 210 | + return false; |
| 211 | + |
| 212 | + return true; |
| 213 | + } |
| 214 | + |
| 215 | + static bool IsEmptyLocate(byte[] array, byte[] candidate) |
| 216 | + { |
| 217 | + return array == null |
| 218 | + || candidate == null |
| 219 | + || array.Length == 0 |
| 220 | + || candidate.Length == 0 |
| 221 | + || candidate.Length > array.Length; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + |
| 226 | + public static class BinaryReaderExtension |
| 227 | + { |
| 228 | + public static byte[] ReadAllBytes(this BinaryReader reader) |
| 229 | + { |
| 230 | + const int bufferSize = 4096; |
| 231 | + using (var ms = new MemoryStream()) |
| 232 | + { |
| 233 | + byte[] buffer = new byte[bufferSize]; |
| 234 | + int count; |
| 235 | + while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) |
| 236 | + ms.Write(buffer, 0, count); |
| 237 | + return ms.ToArray(); |
| 238 | + } |
| 239 | + |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments