From fb74711055a02f187112dd8ab6fd520004c36818 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 31 Oct 2020 17:40:52 +0330 Subject: [PATCH 01/12] add test file --- .../core-java-security-2/src/test/resources/baeldung.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 core-java-modules/core-java-security-2/src/test/resources/baeldung.txt diff --git a/core-java-modules/core-java-security-2/src/test/resources/baeldung.txt b/core-java-modules/core-java-security-2/src/test/resources/baeldung.txt new file mode 100644 index 0000000000..dc6988d2f9 --- /dev/null +++ b/core-java-modules/core-java-security-2/src/test/resources/baeldung.txt @@ -0,0 +1,2 @@ +Hello Baeldung +This is AES file encryption sample \ No newline at end of file From c98d8050fd2fdccf5dd91e6c3a841a8dac8df675 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 31 Oct 2020 17:41:10 +0330 Subject: [PATCH 02/12] update assertj version --- core-java-modules/core-java-security-2/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-security-2/pom.xml b/core-java-modules/core-java-security-2/pom.xml index 890b4147ca..895509410d 100644 --- a/core-java-modules/core-java-security-2/pom.xml +++ b/core-java-modules/core-java-security-2/pom.xml @@ -53,7 +53,7 @@ 1.11 - 3.10.0 + 3.18.0 2.3.1 From ec724579ee7a4714adc777cc37dbbe58c8d82731 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 31 Oct 2020 17:41:37 +0330 Subject: [PATCH 03/12] add aes main source --- .../main/java/com/baeldung/aes/AESUtil.java | 183 ++++++++++++++++++ .../main/java/com/baeldung/aes/Student.java | 29 +++ .../com/baeldung/aes/AESUtilUnitTest.java | 89 +++++++++ 3 files changed, 301 insertions(+) create mode 100644 core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java create mode 100644 core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java create mode 100644 core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java new file mode 100644 index 0000000000..c4568623d0 --- /dev/null +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java @@ -0,0 +1,183 @@ +package com.baeldung.crypto; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.crypto.*; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; +import java.io.*; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.KeySpec; +import java.util.Base64; + +public class AESUtil { + static 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 null; + } + + public static String decrypt(String algorithm, String cipherText, SecretKey key, IvParameterSpec iv) { + try { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.DECRYPT_MODE, key, iv); + byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText)); + return new String(plainText); + } catch (NoSuchAlgorithmException | NoSuchPaddingException + | InvalidKeyException | InvalidAlgorithmParameterException + | IllegalBlockSizeException | BadPaddingException exp) { + logger.error(exp.getMessage()); + } + return null; + } + + public static SecretKey generateKey(int n) throws NoSuchAlgorithmException { + KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); + keyGenerator.init(n); + SecretKey key = keyGenerator.generateKey(); + return key; + } + + public static SecretKey getKeyFromPassword(String password, String salt) + 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"); + return secret; + } + + public static IvParameterSpec generateIv() { + byte[] iv = new byte[16]; + new SecureRandom().nextBytes(iv); + return new IvParameterSpec(iv); + } + + public static void encryptFile(String algorithm, SecretKey key, IvParameterSpec iv + , File inputFile, File outputFile) throws IOException { + try { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.ENCRYPT_MODE, key, iv); + FileInputStream inputStream = new FileInputStream(inputFile); + FileOutputStream outputStream = new FileOutputStream(outputFile); + 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[] outputBytes = cipher.doFinal(); + if (outputBytes != null) { + outputStream.write(outputBytes); + } + 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 { + try { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.DECRYPT_MODE, key, iv); + FileInputStream inputStream = new FileInputStream(encryptedFile); + FileOutputStream 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) { + outputStream.write(output); + } + inputStream.close(); + outputStream.close(); + } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException + | NoSuchPaddingException | BadPaddingException + | IllegalBlockSizeException | InvalidKeyException exp) { + logger.error(exp.getMessage()); + } + } + + public static SealedObject encryptObject(String algorithm, Serializable object, SecretKey key, IvParameterSpec iv) { + try { + Cipher cipher = Cipher.getInstance(algorithm); + 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()); + } + return null; + } + + 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()); + } + return null; + } + + public static String encryptPasswordBased(String plainText, SecretKey key, IvParameterSpec iv) { + 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 null; + } + + public static String decryptPasswordBased(String cipherText, SecretKey key, IvParameterSpec iv) { + 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 null; + } + +} diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java new file mode 100644 index 0000000000..be87d4b06f --- /dev/null +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java @@ -0,0 +1,29 @@ +package com.baeldung.crypto; + +import java.io.Serializable; + +public class Student implements Serializable { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java new file mode 100644 index 0000000000..fd51ce6a62 --- /dev/null +++ b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java @@ -0,0 +1,89 @@ +package com.baeldung.crypto; + +import org.assertj.core.api.WithAssertions; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import javax.crypto.*; +import javax.crypto.spec.IvParameterSpec; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; + +//@SpringBootTest +class AESUtilUnitTest implements WithAssertions { + + @Test + void contextLoads() { + } + + @Test + void givenString_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException { + // given + String input = "baeldung"; + SecretKey key = AESUtil.generateKey(128); + IvParameterSpec ivParameterSpec = AESUtil.generateIv(); + String algorithm = "AES/CBC/PKCS5Padding"; + + // when + String cipherText = AESUtil.encrypt(algorithm, input, key, ivParameterSpec); + String plainText = AESUtil.decrypt(algorithm, cipherText, key, ivParameterSpec); + + // then + Assertions.assertEquals(input, plainText); + } + + @Test + void givenFile_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException, IOException { + // given + SecretKey key = AESUtil.generateKey(128); + String algorithm = "AES/CBC/PKCS5Padding"; + IvParameterSpec ivParameterSpec = AESUtil.generateIv(); + File inputFile = Paths.get("src/test/resources/baeldung.txt").toFile(); + File encryptedFile = new File("classpath:baeldung.encrypted"); + File decryptedFile = new File("document.decrypted"); + + // when + AESUtil.encryptFile(algorithm, key, ivParameterSpec, inputFile, encryptedFile); + AESUtil.decryptFile(algorithm, key, ivParameterSpec, encryptedFile, decryptedFile); + + // then + assertThat(inputFile).hasSameTextualContentAs(decryptedFile); + } + + @Test + void givenObject_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException { + // given + Student student = new Student("Baeldung", 20); + SecretKey key = AESUtil.generateKey(128); + IvParameterSpec ivParameterSpec = AESUtil.generateIv(); + String algorithm = "AES/CBC/PKCS5Padding"; + + // when + SealedObject sealedObject = AESUtil.encryptObject(algorithm, student, key, ivParameterSpec); + Student object = (Student) AESUtil.decryptObject(algorithm, sealedObject, key, ivParameterSpec); + + // then + assertThat(student).isEqualToComparingFieldByField(object); + } + + @Test + void givenPassword_whenEncrypt_thenSuccess() throws InvalidKeySpecException, NoSuchAlgorithmException { + // given + String plainText = "www.baeldung.com"; + String password = "baeldung"; + String salt = "12345678"; + IvParameterSpec ivParameterSpec = AESUtil.generateIv(); + SecretKey key = AESUtil.getKeyFromPassword(password,salt); + + // when + String cipherText = AESUtil.encryptPasswordBased(plainText, key, ivParameterSpec); + String decryptedCipherText = AESUtil.decryptPasswordBased(cipherText, key, ivParameterSpec); + + // then + Assertions.assertEquals(plainText, decryptedCipherText); + } +} + From 6f32d4758d472b9b1cc2ad8dfc31ea7a7c323c7e Mon Sep 17 00:00:00 2001 From: sharifi Date: Sat, 31 Oct 2020 17:40:22 +0330 Subject: [PATCH 04/12] update README file --- core-java-modules/core-java-security-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-security-2/README.md b/core-java-modules/core-java-security-2/README.md index 03a5a94acc..f6bc410900 100644 --- a/core-java-modules/core-java-security-2/README.md +++ b/core-java-modules/core-java-security-2/README.md @@ -12,4 +12,5 @@ This module contains articles about core Java Security - [How to Read PEM File to Get Public and Private Keys](https://www.baeldung.com/java-read-pem-file-keys) - [Listing the Available Cipher Algorithms](https://www.baeldung.com/java-list-cipher-algorithms) - [Get a List of Trusted Certificates in Java](https://www.baeldung.com/java-list-trusted-certificates) +- [Java AES encryption and decryption](https://www.baeldung.com/) - More articles: [[<-- prev]](/core-java-modules/core-java-security) From 108c23795f2bfce0038af8e523b71a4cff844042 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sun, 8 Nov 2020 17:18:20 +0330 Subject: [PATCH 05/12] Revert "update README file" This reverts commit 6f32d475 --- core-java-modules/core-java-security-2/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/core-java-modules/core-java-security-2/README.md b/core-java-modules/core-java-security-2/README.md index f6bc410900..03a5a94acc 100644 --- a/core-java-modules/core-java-security-2/README.md +++ b/core-java-modules/core-java-security-2/README.md @@ -12,5 +12,4 @@ This module contains articles about core Java Security - [How to Read PEM File to Get Public and Private Keys](https://www.baeldung.com/java-read-pem-file-keys) - [Listing the Available Cipher Algorithms](https://www.baeldung.com/java-list-cipher-algorithms) - [Get a List of Trusted Certificates in Java](https://www.baeldung.com/java-list-trusted-certificates) -- [Java AES encryption and decryption](https://www.baeldung.com/) - More articles: [[<-- prev]](/core-java-modules/core-java-security) From 04bb7449970ac84f1d5ea8fb2b53ae44a670b563 Mon Sep 17 00:00:00 2001 From: sharifi Date: Thu, 12 Nov 2020 14:06:53 +0330 Subject: [PATCH 06/12] add equals method --- .../src/main/java/com/baeldung/aes/Student.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java index be87d4b06f..6daceb3377 100644 --- a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java @@ -1,6 +1,7 @@ package com.baeldung.crypto; import java.io.Serializable; +import java.util.Objects; public class Student implements Serializable { private String name; @@ -26,4 +27,14 @@ public class Student implements Serializable { public void setAge(int age) { this.age = age; } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Student student = (Student) o; + return age == student.age && Objects.equals(name, student.name); + } } From b2b8ad97097b1d9b3297f940e1caee5f3c1fccad Mon Sep 17 00:00:00 2001 From: sharifi Date: Thu, 12 Nov 2020 14:08:02 +0330 Subject: [PATCH 07/12] add clean up files improve code quality --- .../java/com/baeldung/aes/AESUtilUnitTest.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java index fd51ce6a62..ed3b5bd650 100644 --- a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java +++ b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java @@ -12,13 +12,8 @@ import java.nio.file.Paths; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; -//@SpringBootTest class AESUtilUnitTest implements WithAssertions { - @Test - void contextLoads() { - } - @Test void givenString_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException { // given @@ -41,7 +36,8 @@ class AESUtilUnitTest implements WithAssertions { SecretKey key = AESUtil.generateKey(128); String algorithm = "AES/CBC/PKCS5Padding"; IvParameterSpec ivParameterSpec = AESUtil.generateIv(); - File inputFile = Paths.get("src/test/resources/baeldung.txt").toFile(); + File inputFile = Paths.get("src/test/resources/baeldung.txt") + .toFile(); File encryptedFile = new File("classpath:baeldung.encrypted"); File decryptedFile = new File("document.decrypted"); @@ -51,6 +47,8 @@ class AESUtilUnitTest implements WithAssertions { // then assertThat(inputFile).hasSameTextualContentAs(decryptedFile); + encryptedFile.delete(); + decryptedFile.delete(); } @Test @@ -66,7 +64,7 @@ class AESUtilUnitTest implements WithAssertions { Student object = (Student) AESUtil.decryptObject(algorithm, sealedObject, key, ivParameterSpec); // then - assertThat(student).isEqualToComparingFieldByField(object); + assertThat(student).isEqualTo(object); } @Test @@ -76,7 +74,7 @@ class AESUtilUnitTest implements WithAssertions { String password = "baeldung"; String salt = "12345678"; IvParameterSpec ivParameterSpec = AESUtil.generateIv(); - SecretKey key = AESUtil.getKeyFromPassword(password,salt); + SecretKey key = AESUtil.getKeyFromPassword(password, salt); // when String cipherText = AESUtil.encryptPasswordBased(plainText, key, ivParameterSpec); @@ -86,4 +84,3 @@ class AESUtilUnitTest implements WithAssertions { Assertions.assertEquals(plainText, decryptedCipherText); } } - From f860d885317a9b9b8c8b20524dfa73c7d8ea6a9d Mon Sep 17 00:00:00 2001 From: sharifi Date: Thu, 12 Nov 2020 14:08:44 +0330 Subject: [PATCH 08/12] edit logger code improve code quality --- .../main/java/com/baeldung/aes/AESUtil.java | 125 ++++++++++-------- 1 file changed, 73 insertions(+), 52 deletions(-) diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java index c4568623d0..e8b75ccc79 100644 --- a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java @@ -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; } From e0896445acdd8952decbd357e9e5e7604a94a9d7 Mon Sep 17 00:00:00 2001 From: sharifi Date: Tue, 17 Nov 2020 09:14:33 +0330 Subject: [PATCH 09/12] correct the package name --- .../src/main/java/com/baeldung/aes/AESUtil.java | 2 +- .../src/main/java/com/baeldung/aes/Student.java | 2 +- .../src/test/java/com/baeldung/aes/AESUtilUnitTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java index e8b75ccc79..361f09f1cb 100644 --- a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java @@ -1,4 +1,4 @@ -package com.baeldung.crypto; +package com.baeldung.aes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java index 6daceb3377..13cd1c5e9d 100644 --- a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/Student.java @@ -1,4 +1,4 @@ -package com.baeldung.crypto; +package com.baeldung.aes; import java.io.Serializable; import java.util.Objects; diff --git a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java index ed3b5bd650..3b583ef0c4 100644 --- a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java +++ b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.crypto; +package com.baeldung.aes; import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.Assertions; From ef87edfab635cc49ed3d084c6ee1934de8da5f03 Mon Sep 17 00:00:00 2001 From: sharifi Date: Tue, 17 Nov 2020 09:30:20 +0330 Subject: [PATCH 10/12] improve import package names --- .../src/test/java/com/baeldung/aes/AESUtilUnitTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java index 3b583ef0c4..78543d0abc 100644 --- a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java +++ b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java @@ -4,7 +4,8 @@ import org.assertj.core.api.WithAssertions; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import javax.crypto.*; +import javax.crypto.SealedObject; +import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import java.io.File; import java.io.IOException; From 12bbd7424914d446fe62812ce969a4cf26faf300 Mon Sep 17 00:00:00 2001 From: sharifi Date: Sun, 29 Nov 2020 11:46:11 +0330 Subject: [PATCH 11/12] remove try/catch block and add throw clause --- .../main/java/com/baeldung/aes/AESUtil.java | 218 +++++++----------- 1 file changed, 85 insertions(+), 133 deletions(-) diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java index 361f09f1cb..2952eef625 100644 --- a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java +++ b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/aes/AESUtil.java @@ -1,8 +1,5 @@ package com.baeldung.aes; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; @@ -28,36 +25,25 @@ import java.security.spec.KeySpec; import java.util.Base64; public class AESUtil { - 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 null; + 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); + byte[] cipherText = cipher.doFinal(input.getBytes()); + return Base64.getEncoder() + .encodeToString(cipherText); } - public static String decrypt(String algorithm, String cipherText, SecretKey key, IvParameterSpec iv) { - try { - Cipher cipher = Cipher.getInstance(algorithm); - cipher.init(Cipher.DECRYPT_MODE, key, iv); - byte[] plainText = cipher.doFinal(Base64.getDecoder() - .decode(cipherText)); - return new String(plainText); - } catch (NoSuchAlgorithmException | NoSuchPaddingException | - InvalidKeyException | InvalidAlgorithmParameterException | - IllegalBlockSizeException | BadPaddingException exp) { - LOGGER.error(exp.getMessage()); - } - return null; + public static String decrypt(String algorithm, String cipherText, SecretKey key, IvParameterSpec iv) + throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, + InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.DECRYPT_MODE, key, iv); + byte[] plainText = cipher.doFinal(Base64.getDecoder() + .decode(cipherText)); + return new String(plainText); } 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, - 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); - inputStream = new FileInputStream(inputFile); - outputStream = new FileOutputStream(outputFile); - 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[] 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(); + File inputFile, File outputFile) throws IOException, NoSuchPaddingException, + NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, + BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.ENCRYPT_MODE, key, iv); + FileInputStream inputStream = new FileInputStream(inputFile); + FileOutputStream outputStream = new FileOutputStream(outputFile); + 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); } - } 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) { - try { - Cipher cipher = Cipher.getInstance(algorithm); - 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()); + public static void decryptFile(String algorithm, SecretKey key, IvParameterSpec iv, + File encryptedFile, File decryptedFile) throws IOException, NoSuchPaddingException, + NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, + BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.DECRYPT_MODE, key, iv); + FileInputStream inputStream = new FileInputStream(encryptedFile); + FileOutputStream 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); + } } - return null; + byte[] output = cipher.doFinal(); + if (output != null) { + outputStream.write(output); + } + inputStream.close(); + outputStream.close(); } - 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()); - } - return null; + public static SealedObject encryptObject(String algorithm, Serializable object, SecretKey key, + IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException, + InvalidAlgorithmParameterException, InvalidKeyException, IOException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.ENCRYPT_MODE, key, iv); + SealedObject sealedObject = new SealedObject(object, cipher); + return sealedObject; } - public static String encryptPasswordBased(String plainText, SecretKey key, IvParameterSpec iv) { - 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 null; + public static Serializable decryptObject(String algorithm, SealedObject sealedObject, SecretKey key, + IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException, + InvalidAlgorithmParameterException, InvalidKeyException, ClassNotFoundException, + BadPaddingException, IllegalBlockSizeException, IOException { + Cipher cipher = Cipher.getInstance(algorithm); + cipher.init(Cipher.DECRYPT_MODE, key, iv); + Serializable unsealObject = (Serializable) sealedObject.getObject(cipher); + return unsealObject; } - public static String decryptPasswordBased(String cipherText, SecretKey key, IvParameterSpec iv) { - 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 null; + public static String encryptPasswordBased(String plainText, SecretKey key, IvParameterSpec iv) + throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, + InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, key, iv); + return Base64.getEncoder() + .encodeToString(cipher.doFinal(plainText.getBytes())); + } + + public static String decryptPasswordBased(String cipherText, SecretKey key, IvParameterSpec iv) + throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, + 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))); } } From c1d36f8a66578848c6095c6380534c6558c434ab Mon Sep 17 00:00:00 2001 From: sharifi Date: Sun, 29 Nov 2020 11:46:34 +0330 Subject: [PATCH 12/12] add throw clause --- .../com/baeldung/aes/AESUtilUnitTest.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java index 78543d0abc..531c20ca79 100644 --- a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java +++ b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/aes/AESUtilUnitTest.java @@ -7,16 +7,23 @@ import org.junit.jupiter.api.Test; import javax.crypto.SealedObject; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; import java.io.File; import java.io.IOException; import java.nio.file.Paths; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; class AESUtilUnitTest implements WithAssertions { @Test - void givenString_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException { + void givenString_whenEncrypt_thenSuccess() + throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, + BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException { // given String input = "baeldung"; SecretKey key = AESUtil.generateKey(128); @@ -32,7 +39,9 @@ class AESUtilUnitTest implements WithAssertions { } @Test - void givenFile_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException, IOException { + void givenFile_whenEncrypt_thenSuccess() + throws NoSuchAlgorithmException, IOException, IllegalBlockSizeException, InvalidKeyException, + BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException { // given SecretKey key = AESUtil.generateKey(128); String algorithm = "AES/CBC/PKCS5Padding"; @@ -53,7 +62,10 @@ class AESUtilUnitTest implements WithAssertions { } @Test - void givenObject_whenEncrypt_thenSuccess() throws NoSuchAlgorithmException { + void givenObject_whenEncrypt_thenSuccess() + throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, + InvalidAlgorithmParameterException, NoSuchPaddingException, IOException, BadPaddingException, + ClassNotFoundException { // given Student student = new Student("Baeldung", 20); SecretKey key = AESUtil.generateKey(128); @@ -69,7 +81,9 @@ class AESUtilUnitTest implements WithAssertions { } @Test - void givenPassword_whenEncrypt_thenSuccess() throws InvalidKeySpecException, NoSuchAlgorithmException { + void givenPassword_whenEncrypt_thenSuccess() + throws InvalidKeySpecException, NoSuchAlgorithmException, IllegalBlockSizeException, + InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException { // given String plainText = "www.baeldung.com"; String password = "baeldung";