Skip to content

Commit 3de0e42

Browse files
committed
Add salt for password derive method
1 parent 8b97dcc commit 3de0e42

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

FileSecure-v3/Program.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ namespace FileSecure_v3
1616
class Program
1717
{
1818
//static int CycleSize = 1024 * 1024 * 100; // Increasing this value can reduce the amount of cycle that is required to encrypt an file but also require higher memory consumption
19-
static async void Encrypt(byte[] PasswordToKey,string OpenPath, string SavePath)
19+
static async void Encrypt(string Password,string OpenPath, string SavePath)
2020
{
2121
// Generate a random Nonce
2222
byte[] nonce = new byte[128 / 8];
2323
new Random().NextBytes(nonce);
24+
// Create the key
25+
byte[] PasswordToKey = new PasswordDeriveBytes(Encoding.UTF8.GetBytes(Password), nonce).GetBytes(256 / 8);
2426
using (FileStream plainfile = new FileStream(OpenPath, FileMode.Open))
2527
{
2628

@@ -44,15 +46,18 @@ static async void Encrypt(byte[] PasswordToKey,string OpenPath, string SavePath)
4446
}
4547
}
4648
}
47-
static async public void Decrypt(byte[] PasswordToKey, string OpenPath, string SavePath)
49+
static async public void Decrypt(string Password, string OpenPath, string SavePath)
4850
{
4951
using (FileStream encryptfile = File.OpenRead(OpenPath))
5052
{
51-
// Setup the Crypto Engine
53+
// Get the IV/Nonce from the file
5254
byte[] nonce = new byte[16];
5355
encryptfile.Read(nonce, 0, nonce.Length);
56+
// Create the password
57+
byte[] PasswordToKey = new PasswordDeriveBytes(Encoding.UTF8.GetBytes(Password), null).GetBytes(256 / 8);
5458
using (FileStream plainfile = new FileStream(SavePath, FileMode.OpenOrCreate))
5559
{
60+
// Setup the Crypto Engine and start the encryption process
5661
BufferedAeadBlockCipher buffblockcipher = new BufferedAeadBlockCipher(new GcmBlockCipher(new AesEngine()));
5762
CipherStream cryptstream = new CipherStream(encryptfile, buffblockcipher, buffblockcipher);
5863
buffblockcipher.Init(false, new AeadParameters(new KeyParameter(PasswordToKey), 128, nonce));
@@ -207,14 +212,13 @@ static void Main(string[] args)
207212
}
208213
Console.Clear();
209214
// Start the encryption operation
210-
byte[] PasswordToKey = new PasswordDeriveBytes(Encoding.UTF8.GetBytes(Password),null).GetBytes(256 / 8);
211215
if(IsEncryption == true)
212216
{
213217
// Single File Encryption
214218
try
215219
{
216220
Console.WriteLine("Encrypting File, please wait...");
217-
Encrypt(PasswordToKey, OpenPath, SavePath);
221+
Encrypt(Password, OpenPath, SavePath);
218222
Console.WriteLine("Encryption Process has been Completed, press Enter to exit the application");
219223
Console.ReadLine();
220224
} catch(UnauthorizedAccessException) {
@@ -236,7 +240,7 @@ static void Main(string[] args)
236240
try
237241
{
238242
Console.WriteLine("Decrypting File, please wait...");
239-
Decrypt(PasswordToKey, OpenPath, SavePath);
243+
Decrypt(Password, OpenPath, SavePath);
240244
Console.WriteLine("Decryption Process has been Completed, press Enter to exit the application");
241245
Console.ReadLine();
242246
}

0 commit comments

Comments
 (0)