[BAEL-1930] Encryption and decryption of files using JDK
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.encrypt;
|
||||
|
||||
import javax.crypto.*;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
class FileEncrypterDecrypter {
|
||||
|
||||
private SecretKey secretKey;
|
||||
private Cipher cipher;
|
||||
|
||||
FileEncrypterDecrypter(SecretKey secretKey, String cipher) throws NoSuchPaddingException, NoSuchAlgorithmException {
|
||||
this.secretKey = secretKey;
|
||||
this.cipher = Cipher.getInstance(cipher);
|
||||
}
|
||||
|
||||
void encrypt(String content, String fileName) throws InvalidKeyException, IOException {
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||
byte[] iv = cipher.getIV();
|
||||
|
||||
try (
|
||||
FileOutputStream fileOut = new FileOutputStream(fileName);
|
||||
CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher)
|
||||
) {
|
||||
fileOut.write(iv);
|
||||
cipherOut.write(content.getBytes());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
String decrypt(String fileName) throws InvalidAlgorithmParameterException, InvalidKeyException, IOException {
|
||||
|
||||
String content;
|
||||
|
||||
try (FileInputStream fileIn = new FileInputStream(fileName)) {
|
||||
byte[] fileIv = new byte[16];
|
||||
fileIn.read(fileIv);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv));
|
||||
|
||||
try (CipherInputStream cipherIn = new CipherInputStream(fileIn, cipher)) {
|
||||
InputStreamReader inReader = new InputStreamReader(cipherIn);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int c = inReader.read();
|
||||
while (c != -1) {
|
||||
sb.append((char) c);
|
||||
c = inReader.read();
|
||||
}
|
||||
content = sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
return content;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user