Skip to content

Commit

Permalink
Fixed CRC-32 error when writing to ZipOutputStream:
Browse files Browse the repository at this point in the history
- Removed cloning of ZipEntry to avoid potential data integrity issues.
- 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 66fb440
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void encrypt(ZipFile inputZip, String outputName, String key) {
var contentId = findContentId(inputZip);
log("ContentId: " + contentId);

var contentEntries = new ArrayList<ContentEntry>();
var contentEntries = new ArrayList<org.allaymc.encryptmypack.EncryptMyPack.ContentEntry>();

Check notice on line 83 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L83

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.ContentEntry' due to existing same package import 'org.allaymc.encryptmypack.*'

// Delete old output
Files.deleteIfExists(Path.of(outputName));
Expand All @@ -107,7 +107,7 @@ public static void encrypt(ZipFile inputZip, String outputName, String key) {
entryKey = encryptFile(inputZip, outputStream, zipEntry);
}
log("File: " + zipEntry.getName() + ", entryKey: " + entryKey);
contentEntries.add(new ContentEntry(zipEntry.getName(), entryKey));
contentEntries.add(new org.allaymc.encryptmypack.EncryptMyPack.ContentEntry(zipEntry.getName(), entryKey));

Check notice on line 110 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L110

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.ContentEntry' due to existing same package import 'org.allaymc.encryptmypack.*'
});

generateContentsJson("contents.json", outputStream, contentId, key, contentEntries);
Expand All @@ -124,21 +124,21 @@ public static void createDirectoryRoot(ZipEntry zipEntry, ZipOutputStream output
@SneakyThrows
public static void encryptSubPack(ZipFile inputZip, ZipOutputStream zos, String subPackPath, String key, String contentId) {
log("Encrypting sub pack: " + subPackPath);
var subPackContentEntries = new ArrayList<ContentEntry>();
var subPackContentEntries = new ArrayList<org.allaymc.encryptmypack.EncryptMyPack.ContentEntry>();

Check notice on line 127 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L127

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.ContentEntry' due to existing same package import 'org.allaymc.encryptmypack.*'

// Encrypt files
inputZip.stream().forEach(zipEntry -> {
if (zipEntry.isDirectory()) return;
if (!zipEntry.getName().startsWith(subPackPath)) return;
String entryKey = encryptFile(inputZip, zos, zipEntry);
log("Sub pack file: " + zipEntry.getName() + ", entryKey: " + entryKey);
subPackContentEntries.add(new ContentEntry(zipEntry.getName().substring(subPackPath.length()), entryKey));
subPackContentEntries.add(new org.allaymc.encryptmypack.EncryptMyPack.ContentEntry(zipEntry.getName().substring(subPackPath.length()), entryKey));

Check notice on line 135 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L135

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.ContentEntry' due to existing same package import 'org.allaymc.encryptmypack.*'
});

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 {
public static void generateContentsJson(String name, ZipOutputStream outputStream, String contentId, String key, ArrayList<org.allaymc.encryptmypack.EncryptMyPack.ContentEntry> contentEntries) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

Check notice on line 141 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L141

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.ContentEntry' due to existing same package import 'org.allaymc.encryptmypack.*'
outputStream.putNextEntry(new ZipEntry(name));
try (var stream = new ByteArrayOutputStream()) {
stream.write(VERSION);
Expand All @@ -154,7 +154,7 @@ public static void generateContentsJson(String name, ZipOutputStream outputStrea
var cipher = Cipher.getInstance("AES/CFB8/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(key.substring(0, 16).getBytes(StandardCharsets.UTF_8)));
// Write contents.json
var contentJson = GSON.toJson(new Content(contentEntries));
var contentJson = GSON.toJson(new org.allaymc.encryptmypack.EncryptMyPack.Content(contentEntries));

Check notice on line 157 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L157

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.Content' due to existing same package import 'org.allaymc.encryptmypack.*'
paddingTo(stream, 0x100);
stream.write(cipher.doFinal(contentJson.getBytes(StandardCharsets.UTF_8)));
outputStream.write(stream.toByteArray());
Expand All @@ -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,15 +182,16 @@ 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);
org.allaymc.encryptmypack.EncryptMyPack.Content content = decryptContentsJson(inputZip, "contents.json", key);

Check notice on line 194 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L194

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.Content' due to existing same package import 'org.allaymc.encryptmypack.*'

// Delete old output
Files.deleteIfExists(Path.of(outputName));
Expand Down Expand Up @@ -219,7 +219,7 @@ public static void decrypt(ZipFile inputZip, String outputName, String key) {
}

// Handle sub packs (if exist)
inputZip.stream().filter(EncryptMyPack::isSubPackRoot).forEach(zipEntry -> decryptSubPack(inputZip, outputStream, zipEntry.getName(), key));
inputZip.stream().filter(org.allaymc.encryptmypack.EncryptMyPack::isSubPackRoot).forEach(zipEntry -> decryptSubPack(inputZip, outputStream, zipEntry.getName(), key));

Check notice on line 222 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L222

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack' due to existing same package import 'org.allaymc.encryptmypack.*'

outputStream.close();
log("Decrypted " + inputZip.getName() + " with key " + key + " successfully");
Expand All @@ -228,7 +228,7 @@ public static void decrypt(ZipFile inputZip, String outputName, String key) {
@SneakyThrows
public static void decryptSubPack(ZipFile inputZip, ZipOutputStream zos, String subPackPath, String key) {
log("Decrypting sub pack: " + subPackPath);
Content content = decryptContentsJson(inputZip, subPackPath + "contents.json", key);
org.allaymc.encryptmypack.EncryptMyPack.Content content = decryptContentsJson(inputZip, subPackPath + "contents.json", key);

Check notice on line 231 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L231

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.Content' due to existing same package import 'org.allaymc.encryptmypack.*'

for (var contentEntry : content.content) {
var entryPath = subPackPath + contentEntry.path;
Expand Down Expand Up @@ -260,15 +260,15 @@ public static void decryptFile(ZipOutputStream zos, byte[] bytes, String entryKe
}

@SneakyThrows
private static Content decryptContentsJson(ZipFile inputZip, String subPackPath, String key) {
private static org.allaymc.encryptmypack.EncryptMyPack.Content decryptContentsJson(ZipFile inputZip, String subPackPath, String key) {

Check notice on line 263 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L263

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.Content' due to existing same package import 'org.allaymc.encryptmypack.*'
try (var stream = inputZip.getInputStream(inputZip.getEntry(subPackPath))) {
stream.skip(0x100);
var bytes = stream.readAllBytes();
var secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
var cipher = Cipher.getInstance("AES/CFB8/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(key.substring(0, 16).getBytes(StandardCharsets.UTF_8)));
var decryptedBytes = cipher.doFinal(bytes);
Content content = GSON.fromJson(new String(decryptedBytes), Content.class);
org.allaymc.encryptmypack.EncryptMyPack.Content content = GSON.fromJson(new String(decryptedBytes), org.allaymc.encryptmypack.EncryptMyPack.Content.class);

Check notice on line 271 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L271

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.Content' due to existing same package import 'org.allaymc.encryptmypack.*'
log("Decrypted content json: " + content);
return content;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ public static void paddingTo(ByteArrayOutputStream stream, int pos) {
public static String findContentId(ZipFile zip) {
var manifestEntry = zip.getEntry("manifest.json");
if (manifestEntry == null) throw new IllegalArgumentException("manifest file not exists");
Manifest manifest = GSON.fromJson(new JsonReader(new InputStreamReader(zip.getInputStream(manifestEntry), StandardCharsets.UTF_8)), Manifest.class);
org.allaymc.encryptmypack.EncryptMyPack.Manifest manifest = GSON.fromJson(new JsonReader(new InputStreamReader(zip.getInputStream(manifestEntry), StandardCharsets.UTF_8)), org.allaymc.encryptmypack.EncryptMyPack.Manifest.class);

Check notice on line 306 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L306

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.Manifest' due to existing same package import 'org.allaymc.encryptmypack.*'
return manifest.header.uuid;
}

Expand All @@ -323,15 +323,15 @@ public static void err(String msg) {
System.err.println(msg);
}

public record Content(List<ContentEntry> content) {
public record Content(List<org.allaymc.encryptmypack.EncryptMyPack.ContentEntry> content) {

Check notice on line 326 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L326

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.ContentEntry' due to existing same package import 'org.allaymc.encryptmypack.*'
}

public record ContentEntry(String path, String key) {
}

public static class Manifest {

public Header header;
public org.allaymc.encryptmypack.EncryptMyPack.Manifest.Header header;

Check notice on line 334 in src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/main/java/org/allaymc/encryptmypack/EncryptMyPack.java#L334

Unnecessary use of fully qualified name 'org.allaymc.encryptmypack.EncryptMyPack.Manifest.Header' due to existing same package import 'org.allaymc.encryptmypack.*'

public static class Header {
private String uuid;
Expand Down

0 comments on commit 66fb440

Please sign in to comment.