Skip to content

Instantly share code, notes, and snippets.

@chanchal-357
Last active October 18, 2021 07:33
Show Gist options
  • Save chanchal-357/296998fc3bc7ade9676cc6b6c11b0c59 to your computer and use it in GitHub Desktop.
Save chanchal-357/296998fc3bc7ade9676cc6b6c11b0c59 to your computer and use it in GitHub Desktop.
AES CBC Encrypt-Decrypt using random IV (java 8)
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
public class CryptoUtilAesCbc {
private static final String FACTORY_INSTANCE = "PBKDF2WithHmacSHA256";
private static final String ALGORITHM = "AES/CBC/PKCS5PADDING";
private static final String ENCRYPTION_TYPE = "AES";
private static final int IV_LENGTH = 16;
private static final String SECRET_KEY = "yourSecretKey";
private static final int ITERATION_COUNT = 65536;
private static final int KEY_LENGTH = 256;
private static byte[] getRandomIv() {
byte[] iv = new byte[IV_LENGTH];
new SecureRandom().nextBytes(iv);
return iv;
}
public static String encrypt(String password, String salt, String message) throws Exception {
byte[] iv = getRandomIv();
SecretKey secret = getSecretKey(password, salt);
Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, secret, iv);
byte[] cipherText = cipher.doFinal(message.getBytes());
byte[] cipherTextWithIv = ByteBuffer.allocate(iv.length + cipherText.length)
.put(iv)
.put(cipherText)
.array();
return Base64.getEncoder().encodeToString(cipherTextWithIv);
}
public static String decrypt(String password, String salt, String encrypted) throws Exception {
byte[] decoded = Base64.getDecoder().decode(encrypted);
byte[] iv = Arrays.copyOfRange(decoded, 0, IV_LENGTH);
byte[] cipherText = Arrays.copyOfRange(decoded, IV_LENGTH, decoded.length);
SecretKey secret = getSecretKey(password, salt);
Cipher cipher = initCipher(Cipher.DECRYPT_MODE, secret, iv);
byte[] original = cipher.doFinal(cipherText);
return new String(original);
}
private static SecretKey getSecretKey(String password, String salt) throws Exception {
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), ITERATION_COUNT, KEY_LENGTH);
SecretKeyFactory factory = SecretKeyFactory.getInstance(FACTORY_INSTANCE);
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), ENCRYPTION_TYPE);
}
private static Cipher initCipher(final int mode, SecretKey secretKey, byte[] iv) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(mode, secretKey, new IvParameterSpec(iv));
return cipher;
}
public static void main(String[] args) throws Exception {
String fSalt = "anySaltYouCanUseOfOn";
String plainText = "M0993000353";
String cipherText = encrypt(SECRET_KEY, fSalt, plainText);
System.out.println("CipherText: " + cipherText);
String DecryptedMessage = decrypt(SECRET_KEY, fSalt, cipherText);
System.out.println("DecryptedMessage: " + DecryptedMessage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment