Skip to content

Commit

Permalink
Encryption Algorithm Implementation using Cipher from javax.crypto pa…
Browse files Browse the repository at this point in the history
…ckage
  • Loading branch information
vamsitallapudi committed Jul 7, 2021
1 parent 39039b6 commit 443c4f7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/main/java/encryption/EncryptionUtil.java
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();
}


}
30 changes: 30 additions & 0 deletions src/test/java/encryption/EncryptionUtilTest.java
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);
}
}

0 comments on commit 443c4f7

Please sign in to comment.