Skip to content

Commit 4819658

Browse files
committed
Fixes for decryption of empty files.
1 parent 2817b71 commit 4819658

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Add dependency:
2222
<dependency>
2323
<groupId>com.github.tchudyk</groupId>
2424
<artifactId>filetype-aes</artifactId>
25-
<version>1.0.1</version>
25+
<version>1.0.2</version>
2626
</dependency>
2727
```
2828

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>pl.codeset</groupId>
88
<artifactId>filetype-aes</artifactId>
9-
<version>1.0.1</version>
9+
<version>1.0.2</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/pl/codeset/aesfiletype/AesReader.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ void decrypt(OutputStream outputStream) throws IOException {
124124
bytesRead += read.length;
125125
readPayload += read.length;
126126

127-
cipher.update(read, 0, BLOCK_SIZE, decrypted);
128-
hmac.update(read, 0, BLOCK_SIZE);
127+
if (read.length > 0) {
128+
cipher.update(read, 0, BLOCK_SIZE, decrypted);
129+
hmac.update(read, 0, BLOCK_SIZE);
130+
}
129131

130132
int readLength = read.length;
131133
if (readPayload >= expectedPayloadSize) {
@@ -134,7 +136,9 @@ void decrypt(OutputStream outputStream) throws IOException {
134136
readLength = (last > 0 ? last : BLOCK_SIZE);
135137
}
136138

137-
outputStream.write(decrypted, 0, readLength);
139+
if (read.length > 0) {
140+
outputStream.write(decrypted, 0, readLength);
141+
}
138142
} while (readPayload < expectedPayloadSize);
139143

140144
outputStream.write(cipher.doFinal());

src/test/java/pl/codeset/aesfiletype/AesFileTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,21 @@ void encryptDecryptTest(@TempDir Path tempDir) {
2626

2727
assertArrayEquals(inputBytes, bytes);
2828
}
29+
30+
@Test
31+
void encryptDecryptEmptyTest(@TempDir Path tempDir) {
32+
33+
byte[] inputBytes = "".getBytes(StandardCharsets.UTF_8);
34+
AesFileBuilder.fromBytes(inputBytes)
35+
.usePassword("test")
36+
.writeToFile(tempDir.resolve("encrypted.aes"))
37+
.build();
38+
39+
AesFile loadedFile = AesFile.openFromFile(tempDir.resolve("encrypted.aes"))
40+
.usePassword("test");
41+
42+
byte[] bytes = loadedFile.decryptToBytes();
43+
44+
assertArrayEquals(inputBytes, bytes);
45+
}
2946
}

0 commit comments

Comments
 (0)