remove try/catch block and add throw clause
This commit is contained in:
parent
c57644617d
commit
5274cf403f
|
@ -1,8 +1,5 @@
|
||||||
package com.baeldung.aes;
|
package com.baeldung.aes;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import javax.crypto.Cipher;
|
import javax.crypto.Cipher;
|
||||||
import javax.crypto.IllegalBlockSizeException;
|
import javax.crypto.IllegalBlockSizeException;
|
||||||
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
@ -28,36 +25,25 @@ import java.security.spec.KeySpec;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
public class AESUtil {
|
public class AESUtil {
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(AESUtil.class);
|
|
||||||
|
|
||||||
public static String encrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv) {
|
public static String encrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv)
|
||||||
try {
|
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
byte[] cipherText = cipher.doFinal(input.getBytes());
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
return Base64.getEncoder()
|
byte[] cipherText = cipher.doFinal(input.getBytes());
|
||||||
.encodeToString(cipherText);
|
return Base64.getEncoder()
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
.encodeToString(cipherText);
|
||||||
InvalidKeyException | InvalidAlgorithmParameterException |
|
|
||||||
IllegalBlockSizeException | BadPaddingException exp) {
|
|
||||||
LOGGER.error(exp.getMessage());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String decrypt(String algorithm, String cipherText, SecretKey key, IvParameterSpec iv) {
|
public static String decrypt(String algorithm, String cipherText, SecretKey key, IvParameterSpec iv)
|
||||||
try {
|
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
byte[] plainText = cipher.doFinal(Base64.getDecoder()
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||||
.decode(cipherText));
|
byte[] plainText = cipher.doFinal(Base64.getDecoder()
|
||||||
return new String(plainText);
|
.decode(cipherText));
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
return new String(plainText);
|
||||||
InvalidKeyException | InvalidAlgorithmParameterException |
|
|
||||||
IllegalBlockSizeException | BadPaddingException exp) {
|
|
||||||
LOGGER.error(exp.getMessage());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
|
public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
|
||||||
|
@ -83,122 +69,88 @@ public class AESUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
|
public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
|
||||||
File inputFile, File outputFile) throws IOException {
|
File inputFile, File outputFile) throws IOException, NoSuchPaddingException,
|
||||||
FileInputStream inputStream = null;
|
NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException,
|
||||||
FileOutputStream outputStream = null;
|
BadPaddingException, IllegalBlockSizeException {
|
||||||
try {
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
FileInputStream inputStream = new FileInputStream(inputFile);
|
||||||
inputStream = new FileInputStream(inputFile);
|
FileOutputStream outputStream = new FileOutputStream(outputFile);
|
||||||
outputStream = new FileOutputStream(outputFile);
|
byte[] buffer = new byte[64];
|
||||||
byte[] buffer = new byte[64];
|
int bytesRead;
|
||||||
int bytesRead;
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
byte[] output = cipher.update(buffer, 0, bytesRead);
|
||||||
byte[] output = cipher.update(buffer, 0, bytesRead);
|
|
||||||
if (output != null) {
|
|
||||||
outputStream.write(output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
byte[] outputBytes = cipher.doFinal();
|
|
||||||
if (outputBytes != null) {
|
|
||||||
outputStream.write(outputBytes);
|
|
||||||
}
|
|
||||||
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException |
|
|
||||||
NoSuchPaddingException | BadPaddingException |
|
|
||||||
IllegalBlockSizeException | InvalidKeyException exp) {
|
|
||||||
LOGGER.error(exp.getMessage());
|
|
||||||
} finally {
|
|
||||||
inputStream.close();
|
|
||||||
outputStream.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
inputStream = new FileInputStream(encryptedFile);
|
|
||||||
outputStream = new FileOutputStream(decryptedFile);
|
|
||||||
byte[] buffer = new byte[64];
|
|
||||||
int bytesRead;
|
|
||||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
||||||
byte[] output = cipher.update(buffer, 0, bytesRead);
|
|
||||||
if (output != null) {
|
|
||||||
outputStream.write(output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
byte[] output = cipher.doFinal();
|
|
||||||
if (output != null) {
|
if (output != null) {
|
||||||
outputStream.write(output);
|
outputStream.write(output);
|
||||||
}
|
}
|
||||||
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException |
|
|
||||||
NoSuchPaddingException | BadPaddingException |
|
|
||||||
IllegalBlockSizeException | InvalidKeyException exp) {
|
|
||||||
LOGGER.error(exp.getMessage());
|
|
||||||
} finally {
|
|
||||||
inputStream.close();
|
|
||||||
outputStream.close();
|
|
||||||
}
|
}
|
||||||
|
byte[] outputBytes = cipher.doFinal();
|
||||||
|
if (outputBytes != null) {
|
||||||
|
outputStream.write(outputBytes);
|
||||||
|
}
|
||||||
|
inputStream.close();
|
||||||
|
outputStream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SealedObject encryptObject(String algorithm, Serializable object, SecretKey key, IvParameterSpec iv) {
|
public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
|
||||||
try {
|
File encryptedFile, File decryptedFile) throws IOException, NoSuchPaddingException,
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException,
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
BadPaddingException, IllegalBlockSizeException {
|
||||||
SealedObject sealedObject = new SealedObject(object, cipher);
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
return sealedObject;
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
FileInputStream inputStream = new FileInputStream(encryptedFile);
|
||||||
InvalidKeyException | InvalidAlgorithmParameterException |
|
FileOutputStream outputStream = new FileOutputStream(decryptedFile);
|
||||||
IOException | IllegalBlockSizeException exp) {
|
byte[] buffer = new byte[64];
|
||||||
LOGGER.error(exp.getMessage());
|
int bytesRead;
|
||||||
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
|
byte[] output = cipher.update(buffer, 0, bytesRead);
|
||||||
|
if (output != null) {
|
||||||
|
outputStream.write(output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
byte[] output = cipher.doFinal();
|
||||||
|
if (output != null) {
|
||||||
|
outputStream.write(output);
|
||||||
|
}
|
||||||
|
inputStream.close();
|
||||||
|
outputStream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Serializable decryptObject(String algorithm, SealedObject sealedObject,
|
public static SealedObject encryptObject(String algorithm, Serializable object, SecretKey key,
|
||||||
SecretKey key, IvParameterSpec iv) {
|
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
|
||||||
try {
|
InvalidAlgorithmParameterException, InvalidKeyException, IOException, IllegalBlockSizeException {
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
Serializable unsealObject = (Serializable) sealedObject.getObject(cipher);
|
SealedObject sealedObject = new SealedObject(object, cipher);
|
||||||
return unsealObject;
|
return sealedObject;
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
|
||||||
InvalidKeyException | InvalidAlgorithmParameterException |
|
|
||||||
IOException | ClassNotFoundException | IllegalBlockSizeException | BadPaddingException exp) {
|
|
||||||
LOGGER.error(exp.getMessage());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String encryptPasswordBased(String plainText, SecretKey key, IvParameterSpec iv) {
|
public static Serializable decryptObject(String algorithm, SealedObject sealedObject, SecretKey key,
|
||||||
try {
|
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
InvalidAlgorithmParameterException, InvalidKeyException, ClassNotFoundException,
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
BadPaddingException, IllegalBlockSizeException, IOException {
|
||||||
return Base64.getEncoder()
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
.encodeToString(cipher.doFinal(plainText.getBytes()));
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||||
} catch (NoSuchAlgorithmException | BadPaddingException |
|
Serializable unsealObject = (Serializable) sealedObject.getObject(cipher);
|
||||||
InvalidKeyException | InvalidAlgorithmParameterException |
|
return unsealObject;
|
||||||
NoSuchPaddingException | IllegalBlockSizeException exp) {
|
|
||||||
LOGGER.error(exp.getMessage());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String decryptPasswordBased(String cipherText, SecretKey key, IvParameterSpec iv) {
|
public static String encryptPasswordBased(String plainText, SecretKey key, IvParameterSpec iv)
|
||||||
try {
|
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||||
return new String(cipher.doFinal(Base64.getDecoder()
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
.decode(cipherText)));
|
return Base64.getEncoder()
|
||||||
} catch (NoSuchAlgorithmException | BadPaddingException |
|
.encodeToString(cipher.doFinal(plainText.getBytes()));
|
||||||
InvalidKeyException | InvalidAlgorithmParameterException |
|
}
|
||||||
NoSuchPaddingException | IllegalBlockSizeException exp) {
|
|
||||||
LOGGER.error(exp.getMessage());
|
public static String decryptPasswordBased(String cipherText, SecretKey key, IvParameterSpec iv)
|
||||||
}
|
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
|
||||||
return null;
|
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||||
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||||
|
return new String(cipher.doFinal(Base64.getDecoder()
|
||||||
|
.decode(cipherText)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue