Skip to content

Instantly share code, notes, and snippets.

@kcak11
Last active February 20, 2024 16:35
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kcak11/86f73703eff5bbd2f7bd6b6b3efded34 to your computer and use it in GitHub Desktop.
Save kcak11/86f73703eff5bbd2f7bd6b6b3efded34 to your computer and use it in GitHub Desktop.
AES/CBC/PKCS5PADDING - Java/Javascript (Encryption & Decryption)

AES/CBC/PKCS5PADDING - Java/Javascript (Encryption & Decryption)

package com.ashish.crypto;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
public static void main(String args[]) throws Exception {
byte[] cipherText = encrypt("Testing AES/CBC/PKCS5PADDING stuff from Java and with JavaScript - some random text".getBytes(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456".getBytes()); // 32 length Key
System.out.println(new String(cipherText));
byte[] origText = decrypt(new String(cipherText).getBytes(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456".getBytes());
System.out.println(new String(origText));
}
public static byte[] encrypt(byte[] plainTextData, byte[] secretKey) throws Exception {
try {
String iv = new String(secretKey).substring(0, 16);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
byte[] dataBytes = plainTextData;
int plaintextLength = dataBytes.length;
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(secretKey, "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new String(Base64.getEncoder().encode(encrypted)).getBytes();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static byte[] decrypt(byte[] cipherTextData, byte[] secretKey) throws Exception {
try {
String iv = new String(secretKey).substring(0, 16);
byte[] encrypted = Base64.getDecoder().decode(cipherTextData);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
SecretKeySpec keyspec = new SecretKeySpec(secretKey, "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted);
String originalString = new String(original);
return originalString.getBytes();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
var secretkey = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456'; //Length 32
var key = CryptoJS.enc.Utf8.parse(secretkey);
var iv = CryptoJS.enc.Utf8.parse(secretkey.substring(0, 16));
/*-- Encryption --*/
var cipherText = CryptoJS.AES.encrypt("Testing AES/CBC/PKCS5PADDING stuff from Java and with JavaScript - some random text", key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();
console.log(cipherText);
/*-- Decryption --*/
var decrypted = CryptoJS.AES.decrypt("RQ/SEoGFF9IHmiMNbo/vlPTHuPWCGgDeEK5ZZBZjk/Kh5AIdgmVEeD42gciaK7gDKMP9odpjjZB/PGjebwpYSLzvEONS2jUiDtGPj7C0iNexmK5v5Gw9C8jsvqJdlmVK", key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
console.log(decrypted.toString(CryptoJS.enc.Utf8));
@khalidhabib
Copy link

work like charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment