Skip to content

Commit baf2bef

Browse files
committed
new sys v2
1 parent 8d40706 commit baf2bef

File tree

4 files changed

+62
-170
lines changed

4 files changed

+62
-170
lines changed

src/Backend/Encryptor.cs

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

95
namespace Encryption_App
106
{
11-
class Encryptor
7+
class AESCryptoManager
128
{
13-
public void SymEncrypt(string Data)
14-
{
9+
public void AES_Encrypt(string iF, string oF, byte[] passwordBytes)
10+
{
1511

16-
}
12+
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
13+
14+
using (var AES = new AesManaged())
15+
{
16+
17+
AES.KeySize = 256;
18+
AES.BlockSize = 128;
19+
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))
29+
{
30+
31+
sbyte data;
32+
while ((data = (sbyte)inFile.ReadByte()) != -1)
33+
cs.WriteByte((byte)data);
34+
35+
}
36+
37+
38+
}
39+
40+
}
1741

18-
public void SymEncrypt(byte[] bArray, byte[] pwdBytes)
19-
{
20-
byte[] encryptedBytes = null;
42+
public void AES_Decrypt(string iF, string oF, byte[] passwordBytes)
43+
{
2144

22-
// Set your salt here, change it to meet your flavor:
23-
// The salt bytes must be at least 8 bytes.
2445
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
2546

26-
using (MemoryStream ms = new MemoryStream())
47+
using (var AES = new AesManaged())
2748
{
28-
using (RijndaelManaged AES = new RijndaelManaged())
29-
{
30-
AES.KeySize = 256;
31-
AES.BlockSize = 128;
3249

33-
var key = new Rfc2898DeriveBytes(pwdBytes, saltBytes, 1000);
34-
AES.Key = key.GetBytes(AES.KeySize / 8);
35-
AES.IV = key.GetBytes(AES.BlockSize / 8);
50+
// AESManaged properties
51+
AES.KeySize = 256;
52+
AES.BlockSize = 128;
53+
AES.Padding = PaddingMode.PKCS7;
54+
AES.Mode = CipherMode.CBC;
55+
56+
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000);
57+
3658

37-
AES.Mode = CipherMode.CBC;
59+
// Set actual IV and key
60+
AES.Key = key.GetBytes(AES.KeySize / 8);
61+
AES.IV = key.GetBytes(AES.BlockSize / 8);
62+
63+
using (var inFile = File.OpenRead(iF))
64+
using (var cs = new CryptoStream(inFile, AES.CreateDecryptor(), CryptoStreamMode.Read))
65+
using (var outFile = File.Create(oF))
66+
{
67+
68+
sbyte data;
69+
while ((data = (sbyte)cs.ReadByte()) != -1)
70+
outFile.WriteByte((byte)data);
3871

39-
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
40-
{
41-
cs.Write(bArray, 0, bArray.Length);
42-
cs.Close();
43-
}
44-
encryptedBytes = ms.ToArray();
4572
}
73+
4674
}
4775

48-
4976
}
50-
5177
}
52-
}
78+
}

src/Encryption App.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@
5959
<Generator>MSBuild:Compile</Generator>
6060
<SubType>Designer</SubType>
6161
</Page>
62+
<Compile Include="Backend\Encryptor.cs" />
6263
<Compile Include="UI\App.xaml.cs">
6364
<DependentUpon>App.xaml</DependentUpon>
6465
<SubType>Code</SubType>
6566
</Compile>
66-
<Compile Include="Backend\Encryptor.cs" />
6767
<Compile Include="UI\MainWindow.xaml.cs">
6868
<DependentUpon>MainWindow.xaml</DependentUpon>
6969
<SubType>Code</SubType>

src/Program.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/UI/MainWindow.xaml.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -78,50 +78,20 @@ private void Encrypt_Click(object sender, RoutedEventArgs e)
7878
{
7979
string pwd = InpTxtBox.Text;
8080
string ofilePath = FileTxtBox.Text;
81-
Encryptor encryptor = new Encryptor();
82-
string filePath = encryptor.SymEncrypt(ofilePath, Encoding.UTF8.GetBytes(pwd));
83-
byte[] encryptedData;
84-
using (var br = new BinaryReader(File.OpenRead(filePath)))
85-
{
86-
encryptedData = br.ReadBytes((int)new FileInfo(filePath).Length);
87-
}
88-
89-
if (encryptedData.Length == 0)
90-
{
91-
MessageBox.Show("Encryption Failed");
92-
}
93-
else
94-
{
95-
using (var bw = new BinaryWriter(File.Create(filePath)))
96-
{
97-
bw.Write(encryptedData);
98-
}
99-
MessageBox.Show("Successfully Encrypted");
100-
}
101-
File.Copy(@"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit", ofilePath, true);
81+
AESCryptoManager encryptor = new AESCryptoManager();
82+
encryptor.AES_Encrypt(ofilePath, @"D:\johnk\Documents\pnp.txt", Encoding.UTF8.GetBytes(pwd));
10283
}
10384

10485
private void Decrypt_Click(object sender, RoutedEventArgs e)
10586
{
10687
string pwd = PwdTxtBox.Text;
10788
string ofilePath = DecryptFileLocBox.Text;
108-
byte[] data;
10989

11090
FileInfo f = new FileInfo(ofilePath);
11191

112-
Encryptor encryptor = new Encryptor();
113-
114-
string filePath = encryptor.SymDecrypt(ofilePath, Encoding.UTF8.GetBytes(pwd));
92+
AESCryptoManager decryptor = new AESCryptoManager();
11593

116-
//if (data.Length == 0)
117-
//{
118-
// MessageBox.Show("Decryption Failed");
119-
//}
120-
//else
121-
//{
122-
// MessageBox.Show("Successfully Decrypted");
123-
//}
124-
File.Copy(@"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit", ofilePath, true);
94+
decryptor.AES_Decrypt(ofilePath, @"D:\johnk\Documents\btec.txt", Encoding.UTF8.GetBytes(pwd));
12595
}
12696
}
12797
}

0 commit comments

Comments
 (0)