Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

### Fixed CRC-32 error when writing to ZipOutputStream #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 14 additions & 20 deletions src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@
import com.google.gson.stream.JsonReader;
import lombok.SneakyThrows;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -138,7 +131,8 @@ public static void encryptSubPack(ZipFile inputZip, ZipOutputStream zos, String
generateContentsJson(subPackPath + "contents.json", zos, contentId, key, subPackContentEntries);
}

public static void generateContentsJson(String name, ZipOutputStream outputStream, String contentId, String key, ArrayList<ContentEntry> contentEntries) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
@SneakyThrows
public static void generateContentsJson(String name, ZipOutputStream outputStream, String contentId, String key, ArrayList<ContentEntry> contentEntries) {
outputStream.putNextEntry(new ZipEntry(name));
try (var stream = new ByteArrayOutputStream()) {
stream.write(VERSION);
Expand Down Expand Up @@ -173,8 +167,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 +176,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 All @@ -197,23 +191,23 @@ public static void decrypt(ZipFile inputZip, String outputName, String key) {
Files.deleteIfExists(Path.of(outputName));
var outputStream = new ZipOutputStream(new FileOutputStream(outputName));
// Decrypt files
for (var contentEntry : content.content) {
var entryPath = contentEntry.path;
for (var contentEntry : content.content()) {
var entryPath = contentEntry.path();
var zipEntry = inputZip.getEntry(entryPath);
if (zipEntry == null) {
err("Zip entry not exists: " + entryPath);
continue;
}
outputStream.putNextEntry((ZipEntry) zipEntry.clone());
var bytes = inputZip.getInputStream(zipEntry).readAllBytes();
if (contentEntry.key == null) {
if (contentEntry.key() == null) {
// manifest.json, pack_icon.png, bug_pack_icon.png etc...
// Just copy it to output folder
log("Copying file: " + entryPath);
outputStream.write(bytes);
} else {
log("Decrypting file: " + entryPath);
decryptFile(outputStream, bytes, contentEntry.key);
decryptFile(outputStream, bytes, contentEntry.key());
}
outputStream.closeEntry();
}
Expand All @@ -230,8 +224,8 @@ public static void decryptSubPack(ZipFile inputZip, ZipOutputStream zos, String
log("Decrypting sub pack: " + subPackPath);
Content content = decryptContentsJson(inputZip, subPackPath + "contents.json", key);

for (var contentEntry : content.content) {
var entryPath = subPackPath + contentEntry.path;
for (var contentEntry : content.content()) {
var entryPath = subPackPath + contentEntry.path();
var zipEntry = inputZip.getEntry(entryPath);
if (zipEntry == null) {
err("Zip entry not exists: " + entryPath);
Expand All @@ -240,7 +234,7 @@ public static void decryptSubPack(ZipFile inputZip, ZipOutputStream zos, String
zos.putNextEntry((ZipEntry) zipEntry.clone());
var bytes = inputZip.getInputStream(zipEntry).readAllBytes();
log("Decrypting sub pack file: " + entryPath);
decryptFile(zos, bytes, contentEntry.key);
decryptFile(zos, bytes, contentEntry.key());
zos.closeEntry();
}
}
Expand Down Expand Up @@ -337,4 +331,4 @@ public static class Header {
private String uuid;
}
}
}
}
Loading