-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encryption Algorithm Implementation using Cipher from javax.crypto pa…
…ckage
- Loading branch information
1 parent
39039b6
commit 443c4f7
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |