From 7a8e6e264f30fab2c3e4ffff9dd8256e611f54df Mon Sep 17 00:00:00 2001 From: Bhabani Prasad Patel Date: Sat, 29 May 2021 08:46:26 +0530 Subject: [PATCH] Removing this code as been moved to new module. --- .../ConversionClassUtil.java | 53 ------------------- .../ConversionClassUtilUnitTest.java | 44 --------------- 2 files changed, 97 deletions(-) delete mode 100644 core-java-modules/core-java-security-2/src/main/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtil.java delete mode 100644 core-java-modules/core-java-security-2/src/test/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtilUnitTest.java diff --git a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtil.java b/core-java-modules/core-java-security-2/src/main/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtil.java deleted file mode 100644 index 5ff1b7cb9d..0000000000 --- a/core-java-modules/core-java-security-2/src/main/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtil.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.baeldung.secretkeyandstringconversion; - -import java.security.NoSuchAlgorithmException; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.KeySpec; -import java.util.Base64; - -import javax.crypto.KeyGenerator; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.SecretKeySpec; - -public class ConversionClassUtil { - - /* Generating Secret key */ - - // Generating Secret Key using KeyGenerator class with 256 - public static SecretKey generateKey(int n) throws NoSuchAlgorithmException { - KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); - keyGenerator.init(n); - SecretKey originalKey = keyGenerator.generateKey(); - return originalKey; - } - - // Generating Secret Key using password and salt - 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 originalKey = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); - return originalKey; - } - - /* Converting Secret key into String */ - public static String convertSecretKeyToString(SecretKey secretKey) throws NoSuchAlgorithmException { - // Converting the Secret Key into byte array - byte[] rawData = secretKey.getEncoded(); - // Getting String - Base64 encoded version of the Secret Key - String encodedKey = Base64.getEncoder().encodeToString(rawData); - return encodedKey; - } - - /* Converting String into Secret key into */ - public static SecretKey convertStringToSecretKeyto(String encodedKey) { - // Decoding the Base64 encoded string into byte array - byte[] decodedKey = Base64.getDecoder().decode(encodedKey); - // Rebuilding the Secret Key using SecretKeySpec Class - SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); - return originalKey; - } - -} diff --git a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtilUnitTest.java b/core-java-modules/core-java-security-2/src/test/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtilUnitTest.java deleted file mode 100644 index 29c8ba9fd0..0000000000 --- a/core-java-modules/core-java-security-2/src/test/java/com/baeldung/secretkeyandstringconversion/ConversionClassUtilUnitTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.secretkeyandstringconversion; - -import java.security.NoSuchAlgorithmException; -import java.security.spec.InvalidKeySpecException; - -import javax.crypto.SecretKey; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class ConversionClassUtilUnitTest { - - @Test - void givenPasswordAndSalt_whenCreateSecreKeyCheckConversion_thenSuccess() - throws NoSuchAlgorithmException, InvalidKeySpecException { - // given - String password = "Baeldung@2021"; - String salt = "@$#baelDunG@#^$*"; - - // when - SecretKey encodedKey = ConversionClassUtil.getKeyFromPassword(password, salt); - String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey); - SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString); - - // then - Assertions.assertEquals(encodedKey, decodeKey); - } - - @Test - void givenSize_whenCreateSecreKeyCheckConversion_thenSuccess() - throws NoSuchAlgorithmException, InvalidKeySpecException { - // given - int size = 256; - - // when - SecretKey encodedKey = ConversionClassUtil.generateKey(size); - String encodedString = ConversionClassUtil.convertSecretKeyToString(encodedKey); - SecretKey decodeKey = ConversionClassUtil.convertStringToSecretKeyto(encodedString); - - // then - Assertions.assertEquals(encodedKey, decodeKey); - } - -}