Skip to content

Instantly share code, notes, and snippets.

@inti25
Created July 18, 2019 05:16
Show Gist options
  • Save inti25/eb5dcebb8ede965d5bd84408b6e457a0 to your computer and use it in GitHub Desktop.
Save inti25/eb5dcebb8ede965d5bd84408b6e457a0 to your computer and use it in GitHub Desktop.
AES Encrypt and Decrypt Javascripts JAVA
// 1. Javascripts
var iv = '00000000000000000000000000000000';
var salt = '99990000000000000000000000000099';
var AesUtil = function() {
this.keySize = 128 / 32;
this.iterationCount = 1000;
};
AesUtil.prototype.generateKey = function(salt, passPhrase) {
var key = CryptoJS.PBKDF2(
passPhrase,
CryptoJS.enc.Hex.parse(salt),
{keySize: this.keySize, iterations: this.iterationCount});
return key;
};
AesUtil.prototype.encrypt = function(passPhrase, plainText) {
var key = this.generateKey(salt, passPhrase);
var encrypted = CryptoJS.AES.encrypt(
plainText,
key,
{iv: CryptoJS.enc.Hex.parse(iv)});
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
};
AesUtil.prototype.decrypt = function(passPhrase, cipherText) {
var key = this.generateKey(salt, passPhrase);
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(cipherText),
});
var decrypted = CryptoJS.AES.decrypt(
cipherParams,
key,
{iv: CryptoJS.enc.Hex.parse(iv)});
return decrypted.toString(CryptoJS.enc.Utf8);
};
// usage
var aesUtil = new AesUtil();
var hashKey = Sha256.hash('' + key);
var rawData = aesUtil.decrypt(hashKey, encryptData);
// 2. Java
public class AesUtil {
private final int keySize = 128;
private final int iterationCount = 1000;
private final Cipher cipher;
public AesUtil() throws NoSuchPaddingException, NoSuchAlgorithmException {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
}
public String encrypt(String salt, String iv, String passphrase, String plaintext) throws Exception {
SecretKey key = generateKey(salt, passphrase);
byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, plaintext.getBytes("UTF-8"));
return base64(encrypted);
}
public String decrypt(String salt, String iv, String passphrase, String ciphertext) throws Exception {
SecretKey key = generateKey(salt, passphrase);
byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, base64(ciphertext));
return new String(decrypted, "UTF-8");
}
private byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) throws Exception {
cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));
return cipher.doFinal(bytes);
}
private SecretKey generateKey(String salt, String passphrase) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);
SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
return key;
}
public static String random(int length) {
byte[] salt = new byte[length];
new SecureRandom().nextBytes(salt);
return hex(salt);
}
public static String base64(byte[] bytes) {
return Base64.encodeBase64String(bytes);
}
public static byte[] base64(String str) {
return Base64.decodeBase64(str);
}
public static String hex(byte[] bytes) {
return Hex.encodeHexString(bytes);
}
public static byte[] hex(String str) {
try {
return Hex.decodeHex(str.toCharArray());
} catch (DecoderException e) {
throw new IllegalStateException(e);
}
}
}
// usage
private static String iv = "00000000000000000000000000000000";
private static String salt = "99990000000000000000000000000099";
public String encrypt(String key, String rawData) {
try {
String hashKey = DigestUtils.sha256Hex(key);
AesUtil aesUtil = new AesUtil();
String plaintext = aesUtil.encrypt(salt, iv, hashKey, rawData);
return plaintext;
} catch (Exception e) {
// handle exeption
}
}
public String decrypt(String key, String encryptData) {
try {
String hashKey = DigestUtils.sha256Hex(key);
AesUtil aesUtil = new AesUtil();
String plaintext = aesUtil.decrypt(salt, iv, hashKey, encryptData);
return plaintext;
} catch (Exception e) {
// handle exeption
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment