Skip to content

Commit c0aaed7

Browse files
committed
[BAEL-1930] Encryption and decryption of files using JDK
1 parent 2d6136b commit c0aaed7

3 files changed

Lines changed: 94 additions & 3 deletions

File tree

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.encrypt;
2+
3+
import javax.crypto.*;
4+
import javax.crypto.spec.IvParameterSpec;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.security.InvalidAlgorithmParameterException;
10+
import java.security.InvalidKeyException;
11+
import java.security.NoSuchAlgorithmException;
12+
13+
class FileEncrypterDecrypter {
14+
15+
private SecretKey secretKey;
16+
private Cipher cipher;
17+
18+
FileEncrypterDecrypter(SecretKey secretKey, String cipher) throws NoSuchPaddingException, NoSuchAlgorithmException {
19+
this.secretKey = secretKey;
20+
this.cipher = Cipher.getInstance(cipher);
21+
}
22+
23+
void encrypt(String content, String fileName) throws InvalidKeyException, IOException {
24+
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
25+
byte[] iv = cipher.getIV();
26+
27+
try (
28+
FileOutputStream fileOut = new FileOutputStream(fileName);
29+
CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher)
30+
) {
31+
fileOut.write(iv);
32+
cipherOut.write(content.getBytes());
33+
}
34+
35+
}
36+
37+
String decrypt(String fileName) throws InvalidAlgorithmParameterException, InvalidKeyException, IOException {
38+
39+
String content;
40+
41+
try (FileInputStream fileIn = new FileInputStream(fileName)) {
42+
byte[] fileIv = new byte[16];
43+
fileIn.read(fileIv);
44+
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv));
45+
46+
try (CipherInputStream cipherIn = new CipherInputStream(fileIn, cipher)) {
47+
InputStreamReader inReader = new InputStreamReader(cipherIn);
48+
49+
StringBuilder sb = new StringBuilder();
50+
int c = inReader.read();
51+
while (c != -1) {
52+
sb.append((char) c);
53+
c = inReader.read();
54+
}
55+
content = sb.toString();
56+
}
57+
58+
}
59+
return content;
60+
}
61+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.encrypt;
2+
3+
import org.junit.Test;
4+
5+
import javax.crypto.KeyGenerator;
6+
import javax.crypto.NoSuchPaddingException;
7+
import javax.crypto.SecretKey;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.security.InvalidAlgorithmParameterException;
11+
import java.security.InvalidKeyException;
12+
import java.security.NoSuchAlgorithmException;
13+
14+
import static org.hamcrest.Matchers.is;
15+
import static org.junit.Assert.assertThat;
16+
17+
public class FileEncrypterDecrypterIntegrationTest {
18+
19+
@Test
20+
public void givenStringAndFilename_whenEncryptingIntoFile_andDecryptingFileAgain_thenOriginalStringIsReturned() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, InvalidAlgorithmParameterException {
21+
String originalContent = "foobar";
22+
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
23+
24+
FileEncrypterDecrypter fileEncrypterDecrypter = new FileEncrypterDecrypter(secretKey, "AES/CBC/PKCS5Padding");
25+
fileEncrypterDecrypter.encrypt(originalContent, "baz.enc");
26+
27+
String decryptedContent = fileEncrypterDecrypter.decrypt("baz.enc");
28+
assertThat(decryptedContent, is(originalContent));
29+
30+
new File("baz.enc").delete(); // cleanup
31+
}
32+
33+
}

0 commit comments

Comments
 (0)