Skip to content

Commit 00b9620

Browse files
Commit3
1 parent b3709f6 commit 00b9620

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/Backend/Encryptor.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void AES_Encrypt(string iF, string oF, byte[] passwordBytes)
3939

4040
}
4141

42-
public void AES_Decrypt(string iF, string oF, byte[] passwordBytes)
42+
public bool AES_Decrypt(string iF, string oF, byte[] passwordBytes)
4343
{
4444

4545
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
@@ -60,17 +60,24 @@ public void AES_Decrypt(string iF, string oF, byte[] passwordBytes)
6060
AES.Key = key.GetBytes(AES.KeySize / 8);
6161
AES.IV = key.GetBytes(AES.BlockSize / 8);
6262

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))
63+
try
6664
{
65+
using (var inFile = File.OpenRead(iF))
66+
using (var cs = new CryptoStream(inFile, AES.CreateDecryptor(), CryptoStreamMode.Read))
67+
using (var outFile = File.Create(oF))
68+
{
6769

68-
sbyte data;
69-
while ((data = (sbyte)cs.ReadByte()) != -1)
70-
outFile.WriteByte((byte)data);
70+
sbyte data;
71+
while ((data = (sbyte)cs.ReadByte()) != -1)
72+
outFile.WriteByte((byte)data);
7173

74+
}
7275
}
73-
76+
catch (CryptographicException)
77+
{
78+
return false;
79+
}
80+
return true;
7481
}
7582

7683
}

src/UI/MainWindow.xaml.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ private void Encrypt_Click(object sender, RoutedEventArgs e)
7979
string pwd = InpTxtBox.Text;
8080
string ofilePath = FileTxtBox.Text;
8181
AESCryptoManager encryptor = new AESCryptoManager();
82-
encryptor.AES_Encrypt(ofilePath, @"D:\johnk\Documents\pnp.txt", Encoding.UTF8.GetBytes(pwd));
82+
encryptor.AES_Encrypt(ofilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd));
83+
File.Copy(System.IO.Path.GetTempPath() + "tempdata.ini", ofilePath, true);
8384
}
8485

8586
private void Decrypt_Click(object sender, RoutedEventArgs e)
@@ -90,8 +91,17 @@ private void Decrypt_Click(object sender, RoutedEventArgs e)
9091
FileInfo f = new FileInfo(ofilePath);
9192

9293
AESCryptoManager decryptor = new AESCryptoManager();
94+
bool worked = decryptor.AES_Decrypt(ofilePath, System.IO.Path.GetTempPath() + "tempdata.ini", Encoding.UTF8.GetBytes(pwd)); ;
95+
if (worked) { File.Copy(System.IO.Path.GetTempPath() + "tempdata.ini", ofilePath, true); }
9396

94-
decryptor.AES_Decrypt(ofilePath, @"D:\johnk\Documents\btec.txt", Encoding.UTF8.GetBytes(pwd));
97+
if (!worked)
98+
{
99+
MessageBox.Show("Wrong Password");
100+
}
101+
else
102+
{
103+
MessageBox.Show("Successfully Decrypted");
104+
}
95105
}
96106
}
97107
}

0 commit comments

Comments
 (0)