parent
1b22786001
commit
b0d7dd8359
|
@ -3,11 +3,22 @@ package com.baeldung.crypto;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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.IvParameterSpec;
|
||||||
import javax.crypto.spec.PBEKeySpec;
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
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.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
@ -17,18 +28,19 @@ import java.security.spec.KeySpec;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
public class AESUtil {
|
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) {
|
public static String encrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv) {
|
||||||
try {
|
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);
|
||||||
byte[] cipherText = cipher.doFinal(input.getBytes());
|
byte[] cipherText = cipher.doFinal(input.getBytes());
|
||||||
return Base64.getEncoder().encodeToString(cipherText);
|
return Base64.getEncoder()
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException
|
.encodeToString(cipherText);
|
||||||
| InvalidKeyException | InvalidAlgorithmParameterException
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
||||||
| IllegalBlockSizeException | BadPaddingException exp) {
|
InvalidKeyException | InvalidAlgorithmParameterException |
|
||||||
logger.error(exp.getMessage());
|
IllegalBlockSizeException | BadPaddingException exp) {
|
||||||
|
LOGGER.error(exp.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -37,12 +49,13 @@ public class AESUtil {
|
||||||
try {
|
try {
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
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);
|
return new String(plainText);
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
||||||
| InvalidKeyException | InvalidAlgorithmParameterException
|
InvalidKeyException | InvalidAlgorithmParameterException |
|
||||||
| IllegalBlockSizeException | BadPaddingException exp) {
|
IllegalBlockSizeException | BadPaddingException exp) {
|
||||||
logger.error(exp.getMessage());
|
LOGGER.error(exp.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -58,7 +71,8 @@ public class AESUtil {
|
||||||
throws NoSuchAlgorithmException, InvalidKeySpecException {
|
throws NoSuchAlgorithmException, InvalidKeySpecException {
|
||||||
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
|
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
|
||||||
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 65536, 256);
|
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;
|
return secret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,13 +82,15 @@ public class AESUtil {
|
||||||
return new IvParameterSpec(iv);
|
return new IvParameterSpec(iv);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
FileInputStream inputStream = null;
|
||||||
|
FileOutputStream outputStream = null;
|
||||||
try {
|
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) {
|
||||||
|
@ -87,22 +103,25 @@ public class AESUtil {
|
||||||
if (outputBytes != null) {
|
if (outputBytes != null) {
|
||||||
outputStream.write(outputBytes);
|
outputStream.write(outputBytes);
|
||||||
}
|
}
|
||||||
|
} catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException |
|
||||||
|
NoSuchPaddingException | BadPaddingException |
|
||||||
|
IllegalBlockSizeException | InvalidKeyException exp) {
|
||||||
|
LOGGER.error(exp.getMessage());
|
||||||
|
} finally {
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
outputStream.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
|
public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec iv,
|
||||||
, File encryptedFile, File decryptedFile) throws IOException {
|
File encryptedFile, File decryptedFile) throws IOException {
|
||||||
|
FileInputStream inputStream = null;
|
||||||
|
FileOutputStream outputStream = null;
|
||||||
try {
|
try {
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||||
FileInputStream inputStream = new FileInputStream(encryptedFile);
|
inputStream = new FileInputStream(encryptedFile);
|
||||||
FileOutputStream outputStream = new FileOutputStream(decryptedFile);
|
outputStream = new FileOutputStream(decryptedFile);
|
||||||
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) {
|
||||||
|
@ -115,12 +134,13 @@ public class AESUtil {
|
||||||
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();
|
inputStream.close();
|
||||||
outputStream.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);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
SealedObject sealedObject = new SealedObject(object, cipher);
|
SealedObject sealedObject = new SealedObject(object, cipher);
|
||||||
return sealedObject;
|
return sealedObject;
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
||||||
| InvalidKeyException | InvalidAlgorithmParameterException
|
InvalidKeyException | InvalidAlgorithmParameterException |
|
||||||
| IOException | IllegalBlockSizeException exp) {
|
IOException | IllegalBlockSizeException exp) {
|
||||||
logger.error(exp.getMessage());
|
LOGGER.error(exp.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Serializable decryptObject(String algorithm, SealedObject sealedObject
|
public static Serializable decryptObject(String algorithm, SealedObject sealedObject,
|
||||||
, SecretKey key, IvParameterSpec iv) {
|
SecretKey key, IvParameterSpec iv) {
|
||||||
try {
|
try {
|
||||||
Cipher cipher = Cipher.getInstance(algorithm);
|
Cipher cipher = Cipher.getInstance(algorithm);
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||||
Serializable unsealObject = (Serializable) sealedObject.getObject(cipher);
|
Serializable unsealObject = (Serializable) sealedObject.getObject(cipher);
|
||||||
return unsealObject;
|
return unsealObject;
|
||||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException
|
} catch (NoSuchAlgorithmException | NoSuchPaddingException |
|
||||||
| InvalidKeyException | InvalidAlgorithmParameterException
|
InvalidKeyException | InvalidAlgorithmParameterException |
|
||||||
| IOException | ClassNotFoundException | IllegalBlockSizeException
|
IOException | ClassNotFoundException | IllegalBlockSizeException | BadPaddingException exp) {
|
||||||
| BadPaddingException exp) {
|
LOGGER.error(exp.getMessage());
|
||||||
logger.error(exp.getMessage());
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -158,11 +177,12 @@ public class AESUtil {
|
||||||
try {
|
try {
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
|
||||||
return Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes()));
|
return Base64.getEncoder()
|
||||||
} catch (NoSuchAlgorithmException | BadPaddingException
|
.encodeToString(cipher.doFinal(plainText.getBytes()));
|
||||||
| InvalidKeyException | InvalidAlgorithmParameterException
|
} catch (NoSuchAlgorithmException | BadPaddingException |
|
||||||
| NoSuchPaddingException | IllegalBlockSizeException exp) {
|
InvalidKeyException | InvalidAlgorithmParameterException |
|
||||||
logger.error(exp.getMessage());
|
NoSuchPaddingException | IllegalBlockSizeException exp) {
|
||||||
|
LOGGER.error(exp.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -171,11 +191,12 @@ public class AESUtil {
|
||||||
try {
|
try {
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
|
||||||
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
cipher.init(Cipher.DECRYPT_MODE, key, iv);
|
||||||
return new String(cipher.doFinal(Base64.getDecoder().decode(cipherText)));
|
return new String(cipher.doFinal(Base64.getDecoder()
|
||||||
} catch (NoSuchAlgorithmException | BadPaddingException
|
.decode(cipherText)));
|
||||||
| InvalidKeyException | InvalidAlgorithmParameterException
|
} catch (NoSuchAlgorithmException | BadPaddingException |
|
||||||
| NoSuchPaddingException | IllegalBlockSizeException exp) {
|
InvalidKeyException | InvalidAlgorithmParameterException |
|
||||||
logger.error(exp.getMessage());
|
NoSuchPaddingException | IllegalBlockSizeException exp) {
|
||||||
|
LOGGER.error(exp.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue