Use BufferedReader when decrypting

This commit is contained in:
Kevin Wittek 2018-07-04 13:50:06 +02:00
parent c0aaed7474
commit 4615e1653b
1 changed files with 9 additions and 10 deletions

View File

@ -2,10 +2,7 @@ package com.baeldung.encrypt;
import javax.crypto.*; import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import java.io.FileInputStream; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -43,14 +40,16 @@ class FileEncrypterDecrypter {
fileIn.read(fileIv); fileIn.read(fileIv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv)); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(fileIv));
try (CipherInputStream cipherIn = new CipherInputStream(fileIn, cipher)) { try (
InputStreamReader inReader = new InputStreamReader(cipherIn); CipherInputStream cipherIn = new CipherInputStream(fileIn, cipher);
InputStreamReader inputReader = new InputStreamReader(cipherIn);
BufferedReader reader = new BufferedReader(inputReader)
) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int c = inReader.read(); String line;
while (c != -1) { while ((line = reader.readLine()) != null) {
sb.append((char) c); sb.append(line);
c = inReader.read();
} }
content = sb.toString(); content = sb.toString();
} }