edit logger code

improve code quality
This commit is contained in:
sharifi 2020-11-12 14:08:44 +03:30
parent 1b22786001
commit b0d7dd8359
1 changed files with 73 additions and 52 deletions

View File

@ -3,11 +3,22 @@ package com.baeldung.crypto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.*;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.BadPaddingException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SealedObject;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@ -17,18 +28,19 @@ import java.security.spec.KeySpec;
import java.util.Base64;
public class AESUtil {
static Logger logger = LoggerFactory.getLogger(AESUtil.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AESUtil.class);
public static String encrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv) {
try {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] cipherText = cipher.doFinal(input.getBytes());
return Base64.getEncoder().encodeToString(cipherText);
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IllegalBlockSizeException | BadPaddingException exp) {
logger.error(exp.getMessage());
return Base64.getEncoder()
.encodeToString(cipherText);
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
InvalidKeyException | InvalidAlgorithmParameterException |
IllegalBlockSizeException | BadPaddingException exp) {
LOGGER.error(exp.getMessage());
}
return null;
}
@ -37,12 +49,13 @@ public class AESUtil {
try {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
byte[] plainText = cipher.doFinal(Base64.getDecoder()
.decode(cipherText));
return new String(plainText);
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IllegalBlockSizeException | BadPaddingException exp) {
logger.error(exp.getMessage());
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
InvalidKeyException | InvalidAlgorithmParameterException |
IllegalBlockSizeException | BadPaddingException exp) {
LOGGER.error(exp.getMessage());
}
return null;
}
@ -55,10 +68,11 @@ public class AESUtil {
}
public static SecretKey getKeyFromPassword(String password, String salt)
throws NoSuchAlgorithmException, InvalidKeySpecException {
throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 65536, 256);
SecretKey secret = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
SecretKey secret = new SecretKeySpec(factory.generateSecret(spec)
.getEncoded(), "AES");
return secret;
}
@ -68,13 +82,15 @@ public class AESUtil {
return new IvParameterSpec(iv);
}
public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec iv
, File inputFile, File outputFile) throws IOException {
public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
File inputFile, File outputFile) throws IOException {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream outputStream = new FileOutputStream(outputFile);
inputStream = new FileInputStream(inputFile);
outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[64];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
@ -87,22 +103,25 @@ public class AESUtil {
if (outputBytes != null) {
outputStream.write(outputBytes);
}
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException |
NoSuchPaddingException | BadPaddingException |
IllegalBlockSizeException | InvalidKeyException exp) {
LOGGER.error(exp.getMessage());
} finally {
inputStream.close();
outputStream.close();
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException
| NoSuchPaddingException | BadPaddingException
| IllegalBlockSizeException | InvalidKeyException exp) {
logger.error(exp.getMessage());
}
}
public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec iv
, File encryptedFile, File decryptedFile) throws IOException {
public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
File encryptedFile, File decryptedFile) throws IOException {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
FileInputStream inputStream = new FileInputStream(encryptedFile);
FileOutputStream outputStream = new FileOutputStream(decryptedFile);
inputStream = new FileInputStream(encryptedFile);
outputStream = new FileOutputStream(decryptedFile);
byte[] buffer = new byte[64];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
@ -115,12 +134,13 @@ public class AESUtil {
if (output != null) {
outputStream.write(output);
}
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException |
NoSuchPaddingException | BadPaddingException |
IllegalBlockSizeException | InvalidKeyException exp) {
LOGGER.error(exp.getMessage());
} finally {
inputStream.close();
outputStream.close();
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException
| NoSuchPaddingException | BadPaddingException
| IllegalBlockSizeException | InvalidKeyException exp) {
logger.error(exp.getMessage());
}
}
@ -130,26 +150,25 @@ public class AESUtil {
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
SealedObject sealedObject = new SealedObject(object, cipher);
return sealedObject;
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IOException | IllegalBlockSizeException exp) {
logger.error(exp.getMessage());
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
InvalidKeyException | InvalidAlgorithmParameterException |
IOException | IllegalBlockSizeException exp) {
LOGGER.error(exp.getMessage());
}
return null;
}
public static Serializable decryptObject(String algorithm, SealedObject sealedObject
, SecretKey key, IvParameterSpec iv) {
public static Serializable decryptObject(String algorithm, SealedObject sealedObject,
SecretKey key, IvParameterSpec iv) {
try {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
Serializable unsealObject = (Serializable) sealedObject.getObject(cipher);
return unsealObject;
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IOException | ClassNotFoundException | IllegalBlockSizeException
| BadPaddingException exp) {
logger.error(exp.getMessage());
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
InvalidKeyException | InvalidAlgorithmParameterException |
IOException | ClassNotFoundException | IllegalBlockSizeException | BadPaddingException exp) {
LOGGER.error(exp.getMessage());
}
return null;
}
@ -158,11 +177,12 @@ public class AESUtil {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
return Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes()));
} catch (NoSuchAlgorithmException | BadPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| NoSuchPaddingException | IllegalBlockSizeException exp) {
logger.error(exp.getMessage());
return Base64.getEncoder()
.encodeToString(cipher.doFinal(plainText.getBytes()));
} catch (NoSuchAlgorithmException | BadPaddingException |
InvalidKeyException | InvalidAlgorithmParameterException |
NoSuchPaddingException | IllegalBlockSizeException exp) {
LOGGER.error(exp.getMessage());
}
return null;
}
@ -171,11 +191,12 @@ public class AESUtil {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
return new String(cipher.doFinal(Base64.getDecoder().decode(cipherText)));
} catch (NoSuchAlgorithmException | BadPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| NoSuchPaddingException | IllegalBlockSizeException exp) {
logger.error(exp.getMessage());
return new String(cipher.doFinal(Base64.getDecoder()
.decode(cipherText)));
} catch (NoSuchAlgorithmException | BadPaddingException |
InvalidKeyException | InvalidAlgorithmParameterException |
NoSuchPaddingException | IllegalBlockSizeException exp) {
LOGGER.error(exp.getMessage());
}
return null;
}