Skip to content

Commit 0d415a7

Browse files
committed
2 parents 202006c + e597a80 commit 0d415a7

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/Backend/AESCryptoManager.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ public void AES_Encrypt(string iF, string oF, byte[] passwordBytes)
1616

1717
AES.KeySize = 256;
1818
AES.BlockSize = 128;
19-
AES.Padding = PaddingMode.PKCS7;
20-
AES.Mode = CipherMode.CBC;
21-
2219
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 100000);
2320
AES.Key = key.GetBytes(AES.KeySize / 8);
2421
AES.IV = key.GetBytes(AES.BlockSize / 8);
22+
AES.Padding = PaddingMode.PKCS7;
23+
AES.Mode = CipherMode.CBC;
2524

2625
using (var outFile = File.Create(oF))
2726
using (var cs = new CryptoStream(outFile, AES.CreateEncryptor(), CryptoStreamMode.Write))
@@ -40,7 +39,7 @@ public void AES_Encrypt(string iF, string oF, byte[] passwordBytes)
4039

4140
}
4241

43-
public void AES_Decrypt(string iF, string oF, byte[] passwordBytes)
42+
public bool AES_Decrypt(string iF, string oF, byte[] passwordBytes)
4443
{
4544

4645
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
@@ -61,7 +60,6 @@ public void AES_Decrypt(string iF, string oF, byte[] passwordBytes)
6160
AES.Key = key.GetBytes(AES.KeySize / 8);
6261
AES.IV = key.GetBytes(AES.BlockSize / 8);
6362

64-
6563
try
6664
{
6765
using (var inFile = File.OpenRead(iF))
@@ -77,10 +75,13 @@ public void AES_Decrypt(string iF, string oF, byte[] passwordBytes)
7775
}
7876
catch (CryptographicException)
7977
{
80-
78+
return false;
8179
}
80+
return true;
8281
}
8382

8483
}
84+
85+
}
8586
}
8687
}

src/EncryptionApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<Generator>MSBuild:Compile</Generator>
6060
<SubType>Designer</SubType>
6161
</Page>
62+
<Compile Include="Backend\AESCryptoManager.cs" />
6263
<Compile Include="Backend\Encryptor.cs" />
6364
<Compile Include="UI\App.xaml.cs">
6465
<DependentUpon>App.xaml</DependentUpon>

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)