|
1 | | -using System.IO; |
| 1 | +using System; |
| 2 | +using System.IO; |
2 | 3 | using System.Security.Cryptography; |
3 | 4 |
|
4 | 5 | namespace Encryption_App.Backend |
@@ -28,14 +29,18 @@ public void EncryptBytes(string inputFile, string outFile, byte[] passwordBytes) |
28 | 29 | aes.Key = key.GetBytes(aes.KeySize / 8); |
29 | 30 | aes.IV = key.GetBytes(aes.BlockSize / 8); |
30 | 31 |
|
| 32 | + long len = new FileInfo(inputFile).Length; |
| 33 | + |
31 | 34 | using (var outFileStream = new FileStream(outFile, FileMode.Create)) |
32 | 35 | using (var cs = new CryptoStream(outFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write)) |
33 | 36 | using (var inFileStream = new FileStream(inputFile, FileMode.Create)) |
34 | 37 | { |
35 | | - |
36 | | - sbyte data; |
37 | | - while ((data = (sbyte)inFileStream.ReadByte()) != -1) |
38 | | - cs.WriteByte((byte)data); |
| 38 | + long its = 0L; |
| 39 | + while (len > its) |
| 40 | + { |
| 41 | + cs.WriteByte((byte)inFileStream.ReadByte()); |
| 42 | + its++; |
| 43 | + } |
39 | 44 | } |
40 | 45 | } |
41 | 46 | } |
@@ -66,13 +71,16 @@ public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes) |
66 | 71 | try |
67 | 72 | { |
68 | 73 | using (var outFileStream = new FileStream(outFile, FileMode.Create)) |
69 | | - using (var cs = new CryptoStream(outFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write)) |
70 | | - using (var inFileStream = new FileStream(inputFile, FileMode.Create)) |
| 74 | + using (var cs = new CryptoStream(outFileStream, aes.CreateDecryptor(), CryptoStreamMode.Write)) |
| 75 | + using (var inFileStream = new FileStream(inputFile, FileMode.Open)) |
71 | 76 | { |
72 | | - |
73 | | - sbyte data; |
74 | | - while ((data = (sbyte)inFileStream.ReadByte()) != -1) |
75 | | - cs.WriteByte((byte)data); |
| 77 | + ulong len = Convert.ToUInt64(new FileInfo(inputFile).Length); |
| 78 | + ulong its = 0UL; |
| 79 | + while (len > its) |
| 80 | + { |
| 81 | + cs.WriteByte((byte)inFileStream.ReadByte()); |
| 82 | + its++; |
| 83 | + } |
76 | 84 | } |
77 | 85 | } |
78 | 86 | catch (CryptographicException) |
|
0 commit comments