Skip to content

Commit 3e100ce

Browse files
committed
2 parents 4145fcf + 8ccf7b9 commit 3e100ce

File tree

1 file changed

+95
-8
lines changed

1 file changed

+95
-8
lines changed

src/Backend/Encryptor.cs

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System;
62
using System.IO;
73
using System.Security.Cryptography;
84

9-
namespace Encryption_App
5+
namespace Aes
106
{
11-
class Encryptor
7+
class AesEncryption
128
{
13-
private static readonly string cryptFileEnding;
9+
public static void Main()
10+
{
11+
try
12+
{
13+
// Create a new instance of the AesManaged
14+
// class. This generates a new key and initialization
15+
// vector (IV).
16+
using (AesManaged myAes = new AesManaged())
17+
{
1418

19+
AES_Encrypt("file.txt", "file.crypt", new byte[10]);
20+
AES_Decrypt("file.crypt", "file.dcryptd", new byte[10]);
21+
}
22+
23+
<<<<<<< HEAD
1524
public string SymEncrypt(string filePath, byte[] pwd)
25+
=======
26+
}
27+
catch (Exception e)
28+
{
29+
Console.WriteLine("Error: {0}", e.Message);
30+
}
31+
Console.Read();
32+
}
33+
private static void AES_Encrypt(string iF, string oF, byte[] passwordBytes)
34+
>>>>>>> 8ccf7b977d851e5f3aac9270e28944d88dc868b9
1635
{
36+
1737
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
1838

39+
<<<<<<< HEAD
1940

2041
using (var AES = new AesManaged())
2142
{
@@ -36,15 +57,53 @@ public string SymEncrypt(string filePath, byte[] pwd)
3657
sbyte rdata;
3758
while ((rdata = (sbyte)inFile.ReadByte()) != -1)
3859
cs.WriteByte((byte)rdata);
60+
=======
61+
using (var outFile = File.Create(oF))
62+
{
63+
64+
using (var AES = new RijndaelManaged())
65+
{
66+
67+
AES.KeySize = 256;
68+
AES.BlockSize = 128;
69+
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000);
70+
AES.Key = key.GetBytes(AES.KeySize / 8);
71+
AES.IV = key.GetBytes(AES.BlockSize / 8);
72+
AES.Padding = PaddingMode.PKCS7;
73+
AES.Mode = CipherMode.CBC;
74+
75+
using (var cs = new CryptoStream(outFile, AES.CreateEncryptor(), CryptoStreamMode.Write))
76+
{
77+
78+
using (var inFile = File.OpenRead(iF))
79+
{
80+
81+
using (var br = new BinaryReader(inFile))
82+
{
83+
84+
sbyte data;
85+
while ((data = (sbyte)inFile.ReadByte()) != -1)
86+
cs.WriteByte((byte)data);
87+
88+
}
89+
}
90+
}
91+
>>>>>>> 8ccf7b977d851e5f3aac9270e28944d88dc868b9
3992
}
4093
return @"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit";
4194
}
4295
}
4396

97+
<<<<<<< HEAD
4498
public string SymDecrypt(string filePath, byte[] pwd)
99+
=======
100+
private static void AES_Decrypt(string iF, string oF, byte[] passwordBytes)
101+
>>>>>>> 8ccf7b977d851e5f3aac9270e28944d88dc868b9
45102
{
103+
46104
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
47105

106+
<<<<<<< HEAD
48107
using (AesManaged AES = new AesManaged())
49108
{
50109
AES.KeySize = 256;
@@ -64,6 +123,34 @@ public string SymDecrypt(string filePath, byte[] pwd)
64123
sbyte rdata;
65124
while ((rdata = (sbyte)inFile.ReadByte()) != -1)
66125
cs.WriteByte((byte)rdata);
126+
=======
127+
using (var inFile = File.OpenRead(iF))
128+
{
129+
130+
using (var AES = new RijndaelManaged())
131+
{
132+
133+
AES.KeySize = 256;
134+
AES.BlockSize = 128;
135+
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000);
136+
AES.Key = key.GetBytes(AES.KeySize / 8);
137+
AES.IV = key.GetBytes(AES.BlockSize / 8);
138+
AES.Padding = PaddingMode.PKCS7;
139+
AES.Mode = CipherMode.CBC;
140+
141+
using (var cs = new CryptoStream(inFile, AES.CreateDecryptor(), CryptoStreamMode.Read))
142+
{
143+
144+
using (var outFile = File.Create(oF))
145+
{
146+
147+
sbyte data;
148+
while ((data = (sbyte)cs.ReadByte()) != -1)
149+
outFile.WriteByte((byte)data);
150+
151+
}
152+
}
153+
>>>>>>> 8ccf7b977d851e5f3aac9270e28944d88dc868b9
67154
}
68155
return @"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit";
69156
}

0 commit comments

Comments
 (0)