Skip to content

Commit 28a040e

Browse files
authored
Fixing invalid file encryption (#11)
* Fixed setting AES key + tests * bump version
1 parent 4f00a08 commit 28a040e

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
build:
1212
runs-on: ubuntu-latest
1313
env:
14-
VERSION_TAG: JPKv0_0_9
15-
VERSION: '0.0.9'
14+
VERSION_TAG: JPKv0_0_10
15+
VERSION: '0.0.10'
1616

1717
steps:
1818
- name: Checkout

JpkHelper/Commads/MakeManifestCommand.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public async Task Execute()
7171
var compressed = await Compress(file);
7272
var parts = compressed.Chunk(CompressedFileSizeLimit).Select((chunk, index) =>
7373
{
74-
var result = Encrypt(aesKey, iv, chunk);
74+
var result = EncryptionHelper.Encrypt(aesKey, iv, chunk);
7575
var partName = $"{fileName}.zip.{index + 1:D3}.aes";
7676
using var destination = File.Create(Path.Combine(OutputPath, partName));
7777
destination.Write(result);
@@ -80,7 +80,9 @@ public async Task Execute()
8080
result.Length,
8181
HashHelpers.CalculateMD5(new MemoryStream(result))
8282
);
83-
});
83+
}).ToArray();
84+
foreach (var part in parts)
85+
AssertPartCanBeDecrypted(part, aesKey, iv);
8486
var compressedAndZippedFile = new CompressedFileInfo(
8587
fileName,
8688
new FileInfo(file).Length,
@@ -98,7 +100,7 @@ public async Task Execute()
98100
switch (AESKeyBehaviour)
99101
{
100102
case AESKeyBehaviour.ToFile:
101-
var path = Path.Combine(OutputPath, "aes_key.base64.txt");
103+
var path = Path.Combine(OutputPath, $"{fileName}.aeskey.base64.txt");
102104
Console.WriteLine(string.Format(Strings.SavingBase64AesKey, path));
103105
await File.WriteAllTextAsync(path, Convert.ToBase64String(aesKey));
104106
break;
@@ -119,6 +121,15 @@ public async Task Execute()
119121
}
120122
}
121123

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+
122133
private string PickFileName(bool multipleFiles, string file)
123134
{
124135
if (!multipleFiles)
@@ -329,25 +340,15 @@ private byte[] CalculateSha256Hash(string file)
329340
return hash.ComputeHash(f);
330341
}
331342

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-
342343
private static async Task<byte[]> Compress(string filePath)
343344
{
344345
var entryName = Path.GetFileName(filePath);
345346
using var buffer = new MemoryStream();
346347
using (var archive = new ZipArchive(buffer, ZipArchiveMode.Create, true))
347348
{
348349
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);
351352
await sourceStream.CopyToAsync(entryStream);
352353
entryStream.Close();
353354
}

JpkHelper/EncryptionHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ namespace JpkHelper;
44

55
public static class EncryptionHelper
66
{
7+
public const int BlockSize = 128;
78
public static string ToHex(byte[] bytes) => BitConverter.ToString(bytes).Replace("-", "").ToLower();
89

910
public static byte[] Encrypt(byte[] aesKey, byte[] iv, byte[] plaintext)
1011
{
1112
using var aes = Aes.Create();
1213
aes.KeySize = 256;
13-
aes.BlockSize = 128;
14+
aes.BlockSize = BlockSize;
1415
aes.Mode = CipherMode.CBC;
1516
aes.Key = aesKey;
1617
aes.IV = iv;
17-
return aes.CreateEncryptor().TransformFinalBlock(plaintext, 0, plaintext.Length);
18+
return aes.EncryptCbc(plaintext,iv);
1819

1920
}
2021
}

Tests/MakeManifestTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO.Compression;
12
using System.Security.Cryptography;
23
using System.Text;
34
using System.Xml.Linq;
@@ -14,7 +15,8 @@ public async Task MakingManifestOfTwoFilesWorks()
1415
{
1516
FilePaths = ["MakeManifestTestFiles/ITP_1.xml", "MakeManifestTestFiles/ITP_2.xml"],
1617
OutputPath = "./two-files",
17-
EnvironmentType = EnvironmentType.Test
18+
EnvironmentType = EnvironmentType.Test,
19+
AESKeyBehaviour = AESKeyBehaviour.ToFile
1820
};
1921
await command.Execute();
2022
Assert.True(File.Exists($"./two-files/ITP_1-{MakeManifestCommand.ManifestFileName}"));
@@ -62,5 +64,17 @@ private static void AssertFileCorectnes(string manifest, string file, string enc
6264
.First(d => d.Name.LocalName == "ContentLength" && d.Parent.Name.LocalName == "Document")
6365
.Value;
6466
Assert.Equal(new FileInfo(file).Length, long.Parse(originalFileLength));
67+
var iv = Convert.FromBase64String(m.Descendants().First(d => d.Name.LocalName == "IV").Value);
68+
var aesKey = Convert.FromBase64String(File.ReadAllText($"two-files/{Path.GetFileName(file)}.aeskey.base64.txt"));
69+
var encryptedFiles = File.ReadAllBytes(encryptedFile);
70+
using var aes = Aes.Create();
71+
aes.BlockSize = 128;
72+
aes.Key = aesKey;
73+
var unencryptedArchiveStream = new MemoryStream(aes.DecryptCbc(encryptedFiles, iv, PaddingMode.PKCS7));
74+
using var archive = new ZipArchive(unencryptedArchiveStream, ZipArchiveMode.Read);
75+
var unencryptedStream = archive.Entries.First().Open();
76+
var unencrypted = new MemoryStream();
77+
unencryptedStream.CopyTo(unencrypted);
78+
Assert.Equal(File.ReadAllBytes(file), unencrypted.ToArray());
6579
}
6680
}

0 commit comments

Comments
 (0)