@@ -71,7 +71,7 @@ public async Task Execute()
71
71
var compressed = await Compress ( file ) ;
72
72
var parts = compressed . Chunk ( CompressedFileSizeLimit ) . Select ( ( chunk , index ) =>
73
73
{
74
- var result = Encrypt ( aesKey , iv , chunk ) ;
74
+ var result = EncryptionHelper . Encrypt ( aesKey , iv , chunk ) ;
75
75
var partName = $ "{ fileName } .zip.{ index + 1 : D3} .aes";
76
76
using var destination = File . Create ( Path . Combine ( OutputPath , partName ) ) ;
77
77
destination . Write ( result ) ;
@@ -80,7 +80,9 @@ public async Task Execute()
80
80
result . Length ,
81
81
HashHelpers . CalculateMD5 ( new MemoryStream ( result ) )
82
82
) ;
83
- } ) ;
83
+ } ) . ToArray ( ) ;
84
+ foreach ( var part in parts )
85
+ AssertPartCanBeDecrypted ( part , aesKey , iv ) ;
84
86
var compressedAndZippedFile = new CompressedFileInfo (
85
87
fileName ,
86
88
new FileInfo ( file ) . Length ,
@@ -98,7 +100,7 @@ public async Task Execute()
98
100
switch ( AESKeyBehaviour )
99
101
{
100
102
case AESKeyBehaviour . ToFile :
101
- var path = Path . Combine ( OutputPath , "aes_key .base64.txt") ;
103
+ var path = Path . Combine ( OutputPath , $ " { fileName } .aeskey .base64.txt") ;
102
104
Console . WriteLine ( string . Format ( Strings . SavingBase64AesKey , path ) ) ;
103
105
await File . WriteAllTextAsync ( path , Convert . ToBase64String ( aesKey ) ) ;
104
106
break ;
@@ -119,6 +121,15 @@ public async Task Execute()
119
121
}
120
122
}
121
123
124
+ private void AssertPartCanBeDecrypted ( CompressedFilePartInfo part , byte [ ] aesKey , byte [ ] iv )
125
+ {
126
+ var bytes = File . ReadAllBytes ( Path . Combine ( OutputPath , part . Name ) ) ;
127
+ using var aes = Aes . Create ( ) ;
128
+ aes . BlockSize = EncryptionHelper . BlockSize ;
129
+ aes . Key = aesKey ;
130
+ aes . DecryptCbc ( bytes , iv , PaddingMode . PKCS7 ) ;
131
+ }
132
+
122
133
private string PickFileName ( bool multipleFiles , string file )
123
134
{
124
135
if ( ! multipleFiles )
@@ -329,25 +340,15 @@ private byte[] CalculateSha256Hash(string file)
329
340
return hash . ComputeHash ( f ) ;
330
341
}
331
342
332
- private static byte [ ] Encrypt ( byte [ ] aesKey , byte [ ] iv , byte [ ] compressed )
333
- {
334
- using var aes = Aes . Create ( ) ;
335
- aes . Key = aesKey ;
336
- aes . KeySize = 256 ;
337
- aes . BlockSize = 128 ;
338
- aes . Mode = CipherMode . CBC ;
339
- return aes . EncryptCbc ( compressed . AsSpan ( ) , iv , PaddingMode . PKCS7 ) ;
340
- }
341
-
342
343
private static async Task < byte [ ] > Compress ( string filePath )
343
344
{
344
345
var entryName = Path . GetFileName ( filePath ) ;
345
346
using var buffer = new MemoryStream ( ) ;
346
347
using ( var archive = new ZipArchive ( buffer , ZipArchiveMode . Create , true ) )
347
348
{
348
349
var entry = archive . CreateEntry ( entryName ) ;
349
- using var entryStream = entry . Open ( ) ;
350
- using var sourceStream = File . OpenRead ( filePath ) ;
350
+ await using var entryStream = entry . Open ( ) ;
351
+ await using var sourceStream = File . OpenRead ( filePath ) ;
351
352
await sourceStream . CopyToAsync ( entryStream ) ;
352
353
entryStream . Close ( ) ;
353
354
}
0 commit comments