diff --git a/src/main/java/encryption/EncryptionUtil.java b/src/main/java/encryption/EncryptionUtil.java new file mode 100644 index 0000000..deb7748 --- /dev/null +++ b/src/main/java/encryption/EncryptionUtil.java @@ -0,0 +1,65 @@ +package encryption; + +import javax.crypto.*; +import javax.crypto.spec.IvParameterSpec; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.Base64; +import java.util.Objects; + +public class EncryptionUtil { + EncryptionUtil encryptionUtil; + + EncryptionUtil getInstance() { + synchronized (this) { + return Objects.requireNonNullElseGet(encryptionUtil, EncryptionUtil::new); + } + } + + private EncryptionUtil() { + } + + public static String encrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv) + throws NoSuchPaddingException, NoSuchAlgorithmException, + InvalidAlgorithmParameterException, InvalidKeyException, + BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.ENCRYPT_MODE, key, iv); +// First applying cipher and then Encoding using Base64 Encryption + byte[] cipheredBytes = cipher.doFinal(input.getBytes()); + return Base64.getEncoder().encodeToString(cipheredBytes); + } + + public static String decrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv) + throws NoSuchPaddingException, NoSuchAlgorithmException, + InvalidAlgorithmParameterException, InvalidKeyException, + BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.DECRYPT_MODE, key, iv); +// Opposite of encrypt logic. First decoding from Base64 and then deciphering. + byte[] decodedBytes = Base64.getDecoder().decode(input.getBytes()); + byte[] decipheredBytes = cipher.doFinal(decodedBytes); + return new String(decipheredBytes); + } + + /** + * To Generate Initialization Vector (IV). It is a pseudo-random value + * and has the same size as the block that is encrypted. + **/ + public static IvParameterSpec generateIv() { + byte[] iv = new byte[16]; + new SecureRandom().nextBytes(iv); + return new IvParameterSpec(iv); + } + + + public static SecretKey generateKey(int n) throws NoSuchAlgorithmException { + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + keyGenerator.init(n); + return keyGenerator.generateKey(); + } + + +} diff --git a/src/test/java/encryption/EncryptionUtilTest.java b/src/test/java/encryption/EncryptionUtilTest.java new file mode 100644 index 0000000..cc01804 --- /dev/null +++ b/src/test/java/encryption/EncryptionUtilTest.java @@ -0,0 +1,30 @@ +package encryption; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.IvParameterSpec; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +public class EncryptionUtilTest { + + @Test + void givenString_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException, + IllegalBlockSizeException, InvalidKeyException, BadPaddingException, + InvalidAlgorithmParameterException, NoSuchPaddingException { + String input = "Vamsi"; + SecretKey key = EncryptionUtil.generateKey(256); + IvParameterSpec ivParameterSpec = EncryptionUtil.generateIv(); + String algorithm = "AES/CBC/PKCS5Padding"; + String cipherText = EncryptionUtil.encrypt(algorithm, input, key, ivParameterSpec); + String decipheredText = EncryptionUtil.decrypt(algorithm, cipherText, key, ivParameterSpec); + assertEquals(input, decipheredText); + } +} \ No newline at end of file