|
1 | | -using System; |
2 | | -using System.IO; |
| 1 | +using System.IO; |
3 | 2 | using System.Security.Cryptography; |
4 | 3 |
|
5 | | -namespace Encryption_App |
| 4 | +namespace Encryption_App.Backend |
6 | 5 | { |
7 | | - class AESCryptoManager |
| 6 | + internal class AesCryptoManager |
8 | 7 | { |
9 | | - public void EncryptBytes(string iF, string oF, byte[] passwordBytes) |
| 8 | + public void EncryptBytes(string inputFile, string outFile, byte[] passwordBytes) |
10 | 9 | { |
11 | 10 |
|
12 | | - byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 11 | + var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; |
13 | 12 |
|
14 | | - using (var AES = new AesManaged()) |
| 13 | + using (var aes = new AesManaged()) |
15 | 14 | { |
16 | 15 |
|
17 | | - AES.KeySize = 256; |
18 | | - AES.BlockSize = 128; |
| 16 | + // AESManaged properties |
| 17 | + aes.KeySize = 256; |
| 18 | + aes.BlockSize = 128; |
| 19 | + aes.Padding = PaddingMode.PKCS7; |
| 20 | + aes.Mode = CipherMode.CBC; |
| 21 | + |
| 22 | + |
| 23 | + // Derives a key using PBKDF2 from the password and a salts |
19 | 24 | var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000); |
20 | | - AES.Key = key.GetBytes(AES.KeySize / 8); |
21 | | - AES.IV = key.GetBytes(AES.BlockSize / 8); |
22 | | - AES.Padding = PaddingMode.PKCS7; |
23 | | - AES.Mode = CipherMode.CBC; |
24 | | - |
25 | | - using (var outFile = File.Create(oF)) |
26 | | - using (var cs = new CryptoStream(outFile, AES.CreateEncryptor(), CryptoStreamMode.Write)) |
27 | | - using (var inFile = File.OpenRead(iF)) |
28 | | - using (var br = new BinaryReader(inFile)) |
| 25 | + |
| 26 | + |
| 27 | + // Set actual IV and key |
| 28 | + aes.Key = key.GetBytes(aes.KeySize / 8); |
| 29 | + aes.IV = key.GetBytes(aes.BlockSize / 8); |
| 30 | + |
| 31 | + using (var outFileStream = new FileStream(outFile, FileMode.Create)) |
| 32 | + using (var cs = new CryptoStream(outFileStream, aes.CreateEncryptor(), CryptoStreamMode.Write)) |
| 33 | + using (var inFileStream = new FileStream(inputFile, FileMode.Create)) |
29 | 34 | { |
30 | 35 |
|
31 | 36 | sbyte data; |
32 | | - while ((data = (sbyte)inFile.ReadByte()) != -1) |
| 37 | + while ((data = (sbyte)inFileStream.ReadByte()) != -1) |
33 | 38 | cs.WriteByte((byte)data); |
34 | | - |
35 | 39 | } |
36 | 40 | } |
37 | 41 | } |
38 | 42 |
|
39 | | - public bool DecryptBytes(string iF, string oF, byte[] passwordBytes) |
| 43 | + public bool DecryptBytes(string inputFile, string outFile, byte[] passwordBytes) |
40 | 44 | { |
41 | 45 |
|
42 | | - byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 46 | + var saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; |
43 | 47 |
|
44 | | - using (var AES = new AesManaged()) |
| 48 | + using (var aes = new AesManaged()) |
45 | 49 | { |
46 | 50 |
|
47 | 51 | // AESManaged properties |
48 | | - AES.KeySize = 256; |
49 | | - AES.BlockSize = 128; |
50 | | - AES.Padding = PaddingMode.PKCS7; |
51 | | - AES.Mode = CipherMode.CBC; |
| 52 | + aes.KeySize = 256; |
| 53 | + aes.BlockSize = 128; |
| 54 | + aes.Padding = PaddingMode.PKCS7; |
| 55 | + aes.Mode = CipherMode.CBC; |
52 | 56 |
|
| 57 | + |
| 58 | + // Derives a key using PBKDF2 from the password and a salt |
53 | 59 | var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000); |
54 | 60 |
|
55 | 61 |
|
56 | 62 | // Set actual IV and key |
57 | | - AES.Key = key.GetBytes(AES.KeySize / 8); |
58 | | - AES.IV = key.GetBytes(AES.BlockSize / 8); |
| 63 | + aes.Key = key.GetBytes(aes.KeySize / 8); |
| 64 | + aes.IV = key.GetBytes(aes.BlockSize / 8); |
59 | 65 |
|
60 | 66 | try |
61 | 67 | { |
62 | | - using (var inFile = File.OpenRead(iF)) |
63 | | - using (var cs = new CryptoStream(inFile, AES.CreateDecryptor(), CryptoStreamMode.Read)) |
64 | | - using (var outFile = File.Create(oF)) |
| 68 | + 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)) |
65 | 71 | { |
66 | 72 |
|
67 | 73 | sbyte data; |
68 | | - while ((data = (sbyte)cs.ReadByte()) != -1) |
69 | | - outFile.WriteByte((byte)data); |
70 | | - |
| 74 | + while ((data = (sbyte)inFileStream.ReadByte()) != -1) |
| 75 | + cs.WriteByte((byte)data); |
71 | 76 | } |
72 | 77 | } |
73 | 78 | catch (CryptographicException) |
|
0 commit comments