@@ -16,11 +16,13 @@ namespace FileSecure_v3
16
16
class Program
17
17
{
18
18
//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 )
20
20
{
21
21
// Generate a random Nonce
22
22
byte [ ] nonce = new byte [ 128 / 8 ] ;
23
23
new Random ( ) . NextBytes ( nonce ) ;
24
+ // Create the key
25
+ byte [ ] PasswordToKey = new PasswordDeriveBytes ( Encoding . UTF8 . GetBytes ( Password ) , nonce ) . GetBytes ( 256 / 8 ) ;
24
26
using ( FileStream plainfile = new FileStream ( OpenPath , FileMode . Open ) )
25
27
{
26
28
@@ -44,15 +46,18 @@ static async void Encrypt(byte[] PasswordToKey,string OpenPath, string SavePath)
44
46
}
45
47
}
46
48
}
47
- static async public void Decrypt ( byte [ ] PasswordToKey , string OpenPath , string SavePath )
49
+ static async public void Decrypt ( string Password , string OpenPath , string SavePath )
48
50
{
49
51
using ( FileStream encryptfile = File . OpenRead ( OpenPath ) )
50
52
{
51
- // Setup the Crypto Engine
53
+ // Get the IV/Nonce from the file
52
54
byte [ ] nonce = new byte [ 16 ] ;
53
55
encryptfile . Read ( nonce , 0 , nonce . Length ) ;
56
+ // Create the password
57
+ byte [ ] PasswordToKey = new PasswordDeriveBytes ( Encoding . UTF8 . GetBytes ( Password ) , null ) . GetBytes ( 256 / 8 ) ;
54
58
using ( FileStream plainfile = new FileStream ( SavePath , FileMode . OpenOrCreate ) )
55
59
{
60
+ // Setup the Crypto Engine and start the encryption process
56
61
BufferedAeadBlockCipher buffblockcipher = new BufferedAeadBlockCipher ( new GcmBlockCipher ( new AesEngine ( ) ) ) ;
57
62
CipherStream cryptstream = new CipherStream ( encryptfile , buffblockcipher , buffblockcipher ) ;
58
63
buffblockcipher . Init ( false , new AeadParameters ( new KeyParameter ( PasswordToKey ) , 128 , nonce ) ) ;
@@ -207,14 +212,13 @@ static void Main(string[] args)
207
212
}
208
213
Console . Clear ( ) ;
209
214
// Start the encryption operation
210
- byte [ ] PasswordToKey = new PasswordDeriveBytes ( Encoding . UTF8 . GetBytes ( Password ) , null ) . GetBytes ( 256 / 8 ) ;
211
215
if ( IsEncryption == true )
212
216
{
213
217
// Single File Encryption
214
218
try
215
219
{
216
220
Console . WriteLine ( "Encrypting File, please wait..." ) ;
217
- Encrypt ( PasswordToKey , OpenPath , SavePath ) ;
221
+ Encrypt ( Password , OpenPath , SavePath ) ;
218
222
Console . WriteLine ( "Encryption Process has been Completed, press Enter to exit the application" ) ;
219
223
Console . ReadLine ( ) ;
220
224
} catch ( UnauthorizedAccessException ) {
@@ -236,7 +240,7 @@ static void Main(string[] args)
236
240
try
237
241
{
238
242
Console . WriteLine ( "Decrypting File, please wait..." ) ;
239
- Decrypt ( PasswordToKey , OpenPath , SavePath ) ;
243
+ Decrypt ( Password , OpenPath , SavePath ) ;
240
244
Console . WriteLine ( "Decryption Process has been Completed, press Enter to exit the application" ) ;
241
245
Console . ReadLine ( ) ;
242
246
}
0 commit comments