Skip to content

Commit

Permalink
Removed cloning of ZipEntry to avoid potential data integrity issues.
Browse files Browse the repository at this point in the history
Added an explicit call to outputStream.closeEntry() after writing the encrypted bytes to ensure proper entry closure and correct CRC-32 validation.
  • Loading branch information
Miroshka000 committed Jun 23, 2024
1 parent cf38dd6 commit 0d9b7ec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ tasks.test {

graalvmNative {
binaries.all {
javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(21))
})
resources.autodetect()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ public static void encryptExcludedFile(ZipFile inputZip, ZipOutputStream outputS

@SneakyThrows
public static String encryptFile(ZipFile inputZip, ZipOutputStream outputStream, ZipEntry zipEntry) {
byte[] bytes;
bytes = inputZip.getInputStream(zipEntry).readAllBytes();
byte[] bytes = inputZip.getInputStream(zipEntry).readAllBytes();
// Init encryptor
var key = randomAlphanumeric(KEY_LENGTH);
var secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
Expand All @@ -183,12 +182,13 @@ public static String encryptFile(ZipFile inputZip, ZipOutputStream outputStream,
// Encrypt the file
var encryptedBytes = cipher.doFinal(bytes);
// Write bytes
outputStream.putNextEntry((ZipEntry) zipEntry.clone());
outputStream.putNextEntry(new ZipEntry(zipEntry.getName()));
outputStream.write(encryptedBytes);
outputStream.closeEntry();
outputStream.closeEntry(); // Закрываем entry после записи данных
return key;
}


@SneakyThrows
public static void decrypt(ZipFile inputZip, String outputName, String key) {
Content content = decryptContentsJson(inputZip, "contents.json", key);
Expand Down

0 comments on commit 0d9b7ec

Please sign in to comment.