-
-
Save muhammedfurkan/6f92f94cb1e6d6662dc4024bb178d2a3 to your computer and use it in GitHub Desktop.
AES/CBC/PKCS5PADDING - Java/Javascript (Encryption & Decryption)
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
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; | |
} | |
} | |
} |
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
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment