Skip to content

Commit e78ca7c

Browse files
authored
Merge pull request eugenp#4606 from kiview/bael-1930-encrypt
[BAEL-1930] Encryption and decryption of files using JDK
2 parents 2201a8d + da1db70 commit e78ca7c

3 files changed

Lines changed: 92 additions & 3 deletions

File tree

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.encrypt;
2+
3+
import javax.crypto.*;
4+
import javax.crypto.spec.IvParameterSpec;
5+
import java.io.*;
6+
import java.security.InvalidAlgorithmParameterException;
7+
import java.security.InvalidKeyException;
8+
import java.security.NoSuchAlgorithmException;
9+
10+
class FileEncrypterDecrypter {
11+
12+
private SecretKey secretKey;
13+
private Cipher cipher;
14+
15+
FileEncrypterDecrypter(SecretKey secretKey, String cipher) throws NoSuchPaddingException, NoSuchAlgorithmException {
16+
this.secretKey = secretKey;
17+
this.cipher = Cipher.getInstance(cipher);
18+
}
19+
20+
void encrypt(String content, String fileName) throws InvalidKeyException, IOException {
21+
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
22+
byte[] iv = cipher.getIV();
23+
24+
try (
25+
FileOutputStream fileOut = new FileOutputStream(fileName);
26+
CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher)
27+
) {
28+
fileOut.write(iv);
29+
cipherOut.write(content.getBytes());
30+
}
31+
32+
}
33+
34+
String decrypt(String fileName) throws InvalidAlgorithmParameterException, InvalidKeyException, IOException {
35+
36+
String content;
37+
38+
try (FileInputStream fileIn = new FileInputStream(fileName)) {
39+
byte[] fileIv = new byte[16];
40+
fileIn.read(fileIv);
41+
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv));
42+
43+
try (
44+
CipherInputStream cipherIn = new CipherInputStream(fileIn, cipher);
45+
InputStreamReader inputReader = new InputStreamReader(cipherIn);
46+
BufferedReader reader = new BufferedReader(inputReader)
47+
) {
48+
49+
StringBuilder sb = new StringBuilder();
50+
String line;
51+
while ((line = reader.readLine()) != null) {
52+
sb.append(line);
53+
}
54+
content = sb.toString();
55+
}
56+
57+
}
58+
return content;
59+
}
60+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)