Skip to content

Commit 96adc4b

Browse files
authored
Merge pull request #10 from johnkellyoxford/dev
New system
2 parents f8912ff + 4cd1ae5 commit 96adc4b

File tree

5 files changed

+75
-56
lines changed

5 files changed

+75
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
## Updates
1616
* Add error handling if encryption/decryption fails
1717

18-
"Copied this from John's cause why not" @nightraven3142
18+
"Copied this from John's cause why not" @nightraven3142

src/Backend/Encryptor.cs

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,43 @@ namespace Encryption_App
1010
{
1111
class Encryptor
1212
{
13-
private static readonly string cryptFileEnding;
13+
public void SymEncrypt(string Data)
14+
{
1415

15-
public byte[] SymEncrypt(byte[] data, byte[] pwd)
16-
{
17-
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
16+
}
1817

19-
using (var ms = new MemoryStream())
20-
{
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;
27-
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));
33-
34-
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
35-
{
36-
using (var bw = new BinaryWriter(cs))
37-
{
38-
bw.Write(data, 0, data.Length);
39-
}
40-
}
41-
return ms.ToArray();
42-
}
43-
}
44-
}
18+
public void SymEncrypt(byte[] bArray, byte[] pwdBytes)
19+
{
20+
byte[] encryptedBytes = null;
4521

46-
public byte[] SymDecrypt(byte[] data, byte[] pwd)
47-
{
22+
// Set your salt here, change it to meet your flavor:
23+
// The salt bytes must be at least 8 bytes.
4824
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
4925

50-
using (var ms = new MemoryStream())
26+
using (MemoryStream ms = new MemoryStream())
5127
{
52-
using (AesManaged AES = new AesManaged())
28+
using (RijndaelManaged AES = new RijndaelManaged())
5329
{
5430
AES.KeySize = 256;
5531
AES.BlockSize = 128;
56-
AES.Padding = PaddingMode.PKCS7;
57-
AES.Mode = CipherMode.CBC;
5832

59-
var key = new Rfc2898DeriveBytes(pwd, saltBytes, 1000);
33+
var key = new Rfc2898DeriveBytes(pwdBytes, saltBytes, 1000);
6034
AES.Key = key.GetBytes(AES.KeySize / 8);
6135
AES.IV = key.GetBytes(AES.BlockSize / 8);
6236

63-
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
37+
AES.Mode = CipherMode.CBC;
38+
39+
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
6440
{
65-
cs.Write(data, 0, data.Length);
41+
cs.Write(bArray, 0, bArray.Length);
42+
cs.Close();
6643
}
67-
return ms.ToArray();
44+
encryptedBytes = ms.ToArray();
6845
}
6946
}
47+
48+
7049
}
50+
7151
}
7252
}

src/Backend/tempoutfile.noedit

Whitespace-only changes.

src/UI/MainWindow.xaml.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
56
using System.Security.Cryptography;
@@ -76,35 +77,51 @@ private void Button_Click(object sender, RoutedEventArgs e)
7677
private void Encrypt_Click(object sender, RoutedEventArgs e)
7778
{
7879
string pwd = InpTxtBox.Text;
79-
string filePath = FileTxtBox.Text;
80-
byte[] data = File.ReadAllBytes(filePath);
80+
string ofilePath = FileTxtBox.Text;
8181
Encryptor encryptor = new Encryptor();
82-
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+
}
8388

84-
using (var bw = new BinaryWriter(File.Create(filePath)))
89+
if (encryptedData.Length == 0)
90+
{
91+
MessageBox.Show("Encryption Failed");
92+
}
93+
else
8594
{
86-
bw.Write(encryptedData);
95+
using (var bw = new BinaryWriter(File.Create(filePath)))
96+
{
97+
bw.Write(encryptedData);
98+
}
99+
MessageBox.Show("Successfully Encrypted");
87100
}
101+
File.Copy(@"C:\Users\johnk\source\repos\EncryptionApp\src\Backend\tempoutfile.noedit", ofilePath, true);
88102
}
89103

90104
private void Decrypt_Click(object sender, RoutedEventArgs e)
91105
{
92106
string pwd = PwdTxtBox.Text;
93-
string filePath = DecryptFileLocBox.Text;
107+
string ofilePath = DecryptFileLocBox.Text;
94108
byte[] data;
95109

96-
FileInfo f = new FileInfo(filePath);
97-
98-
data = File.ReadAllBytes(filePath);
110+
FileInfo f = new FileInfo(ofilePath);
99111

100112
Encryptor encryptor = new Encryptor();
101113

102-
data = encryptor.SymDecrypt(data, Encoding.UTF8.GetBytes(pwd));
103-
104-
using (var bw = new BinaryWriter(File.Create(filePath)))
105-
{
106-
bw.Write(data);
107-
}
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);
108125
}
109126
}
110127
}

utils/SpeedEncryptTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Security.Cryptography;
3+
4+
public class Class1
5+
{
6+
public Class1()
7+
{
8+
}
9+
10+
public static void main()
11+
{
12+
var rng = new RNGCryptoServiceProvider();
13+
byte[] salt = new byte[16];
14+
salt = rng.GetBytes(salt);
15+
var watch = System.Diagnostics.Stopwatch.StartNew();
16+
var hasher = new Rfc2898DeriveBytes("HelloWorld12345", salt, 10000);
17+
watch.Stop();
18+
var elapsedMs = watch.ElapsedMilliseconds;
19+
Console.WriteLine(elapsedMs);
20+
21+
}
22+
}

0 commit comments

Comments
 (0)