Skip to content

Commit 4145fcf

Browse files
committed
broken code
plz review
1 parent 19d6267 commit 4145fcf

File tree

3 files changed

+64
-79
lines changed

3 files changed

+64
-79
lines changed

src/Backend/Encryptor.cs

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,75 +12,61 @@ class Encryptor
1212
{
1313
private static readonly string cryptFileEnding;
1414

15-
public byte[] SymEncrypt(byte[] data, byte[] pwd)
15+
public string SymEncrypt(string filePath, byte[] pwd)
1616
{
1717
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
1818

19-
using (var ms = new MemoryStream())
19+
20+
using (var AES = new AesManaged())
2021
{
21-
using (var AES = new AesManaged())
22-
{
23-
AES.KeySize = 256;
24-
AES.BlockSize = 128;
25-
AES.Padding = PaddingMode.PKCS7;
26-
AES.Mode = CipherMode.CBC;
22+
AES.KeySize = 256;
23+
AES.BlockSize = 128;
24+
AES.Padding = PaddingMode.PKCS7;
25+
AES.Mode = CipherMode.CBC;
2726

28-
var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000);
29-
AES.Key = key.GetBytes(AES.KeySize / 8);
30-
AES.IV = key.GetBytes(AES.BlockSize / 8);
31-
Console.WriteLine(Encoding.UTF8.GetString(AES.Key));
32-
Console.WriteLine(Encoding.UTF8.GetString(AES.IV));
27+
var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000);
28+
AES.Key = key.GetBytes(AES.KeySize / 8);
29+
AES.IV = key.GetBytes(AES.BlockSize / 8);
3330

34-
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
35-
{
36-
using (var bw = new BinaryWriter(cs))
37-
{
38-
try
39-
{
40-
bw.Write(data, 0, data.Length);
41-
}
42-
catch(Exception ex)
43-
{
44-
return null;
45-
}
46-
}
47-
}
48-
return ms.ToArray();
31+
using (var inFile = File.Create(filePath))
32+
using (var outFile = File.Create(@"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit"))
33+
using (var cs = new CryptoStream(outFile, AES.CreateEncryptor(), CryptoStreamMode.Write))
34+
//using (var bw = new BinaryWriter(cs))
35+
{
36+
sbyte rdata;
37+
while ((rdata = (sbyte)inFile.ReadByte()) != -1)
38+
cs.WriteByte((byte)rdata);
4939
}
40+
return @"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit";
5041
}
5142
}
5243

53-
public byte[] SymDecrypt(byte[] data, byte[] pwd)
44+
public string SymDecrypt(string filePath, byte[] pwd)
5445
{
5546
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
5647

57-
using (var ms = new MemoryStream())
48+
using (AesManaged AES = new AesManaged())
5849
{
59-
using (AesManaged AES = new AesManaged())
60-
{
61-
AES.KeySize = 256;
62-
AES.BlockSize = 128;
63-
AES.Padding = PaddingMode.PKCS7;
64-
AES.Mode = CipherMode.CBC;
50+
AES.KeySize = 256;
51+
AES.BlockSize = 128;
52+
AES.Padding = PaddingMode.PKCS7;
53+
AES.Mode = CipherMode.CBC;
6554

66-
var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000);
67-
AES.Key = key.GetBytes(AES.KeySize / 8);
68-
AES.IV = key.GetBytes(AES.BlockSize / 8);
55+
var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000);
56+
AES.Key = key.GetBytes(AES.KeySize / 8);
57+
AES.IV = key.GetBytes(AES.BlockSize / 8);
6958

70-
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
71-
{
72-
try
73-
{
74-
cs.Write(data, 0, data.Length);
75-
}
76-
catch(Exception ex)
77-
{
78-
return null;
79-
}
80-
}
81-
return ms.ToArray();
59+
using (var inFile = File.OpenRead(filePath))
60+
using (var outFile = File.Create(@"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit"))
61+
using (var cs = new CryptoStream(outFile, AES.CreateDecryptor(), CryptoStreamMode.Write))
62+
//using (var bw = new BinaryWriter(cs))
63+
{
64+
sbyte rdata;
65+
while ((rdata = (sbyte)inFile.ReadByte()) != -1)
66+
cs.WriteByte((byte)rdata);
8267
}
68+
return @"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit";
8369
}
8470
}
8571
}
86-
}
72+
}

src/Backend/tempoutfile.noedit

Whitespace-only changes.

src/UI/MainWindow.xaml.cs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,52 +77,51 @@ private void Button_Click(object sender, RoutedEventArgs e)
7777
private void Encrypt_Click(object sender, RoutedEventArgs e)
7878
{
7979
string pwd = InpTxtBox.Text;
80-
string filePath = FileTxtBox.Text;
81-
byte[] data = File.ReadAllBytes(filePath);
80+
string ofilePath = FileTxtBox.Text;
8281
Encryptor encryptor = new Encryptor();
83-
byte[] encryptedData = encryptor.SymEncrypt(data, Encoding.UTF8.GetBytes(pwd));
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+
}
8488

8589
if (encryptedData.Length == 0)
8690
{
87-
MessageBox.Show("Encryption Failed");
88-
}
89-
else
90-
{
91+
MessageBox.Show("Encryption Failed");
92+
}
93+
else
94+
{
9195
using (var bw = new BinaryWriter(File.Create(filePath)))
9296
{
9397
bw.Write(encryptedData);
94-
}
98+
}
9599
MessageBox.Show("Successfully Encrypted");
96100
}
101+
File.Copy(@"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit", ofilePath, true);
97102
}
98103

99104
private void Decrypt_Click(object sender, RoutedEventArgs e)
100105
{
101106
string pwd = PwdTxtBox.Text;
102-
string filePath = DecryptFileLocBox.Text;
107+
string ofilePath = DecryptFileLocBox.Text;
103108
byte[] data;
104109

105-
FileInfo f = new FileInfo(filePath);
106-
107-
data = File.ReadAllBytes(filePath);
110+
FileInfo f = new FileInfo(ofilePath);
108111

109112
Encryptor encryptor = new Encryptor();
110113

111-
data = encryptor.SymDecrypt(data, Encoding.UTF8.GetBytes(pwd));
112-
Debug.Write("Stuff");
113-
114-
if (data.Length == 0)
115-
{
116-
MessageBox.Show("Decryption Failed");
117-
}
118-
else
119-
{
120-
using (var bw = new BinaryWriter(File.Create(filePath)))
121-
{
122-
bw.Write(data);
123-
}
124-
MessageBox.Show("Successfully Decrypted");
125-
}
114+
string filePath = encryptor.SymDecrypt(ofilePath, Encoding.UTF8.GetBytes(pwd));
115+
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);
126125
}
127126
}
128127
}

0 commit comments

Comments
 (0)