Skip to content

Instantly share code, notes, and snippets.

@tw-Frey
Last active February 6, 2020 20:43
Show Gist options
  • Select an option

  • Save tw-Frey/faae8e7d26dc1d0b1380fd965b6ff0fc to your computer and use it in GitHub Desktop.

Select an option

Save tw-Frey/faae8e7d26dc1d0b1380fd965b6ff0fc to your computer and use it in GitHub Desktop.
(use PBEKeySpec) AES256 Password Encryption/Decryption Java
package com.wardana.sukma;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AESAlgorithm {
private static final String TOKEN = "passwd";
private String salt;
private int pwdIterations = 65536;
private int keySize = 256;
private byte[] ivBytes;
private String keyAlgorithm = "AES";
private String encryptAlgorithm = "AES/CBC/PKCS5Padding";
private String secretKeyFactoryAlgorithm = "PBKDF2WithHmacSHA1";
public AESAlgorithm(){
this.salt = getSalt();
}
private String getSalt(){
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[20];
random.nextBytes(bytes);
String text = new String(bytes);
return text;
}
/**
*
* @param plainText
* @return encrypted text
* @throws Exception
*/
public String encyrpt(String plainText) throws Exception{
//generate key
byte[] saltBytes = salt.getBytes("UTF-8");
SecretKeyFactory skf = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm);
PBEKeySpec spec = new PBEKeySpec(TOKEN.toCharArray(), saltBytes, this.pwdIterations, this.keySize);
SecretKey secretKey = skf.generateSecret(spec);
SecretKeySpec key = new SecretKeySpec(secretKey.getEncoded(), keyAlgorithm);
//AES initialization
Cipher cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);
//generate IV
this.ivBytes = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
byte[] encryptedText = cipher.doFinal(plainText.getBytes("UTF-8"));
return new Base64().encodeAsString(encryptedText);
}
/**
*
* @param encryptText
* @return decrypted text
* @throws Exception
*/
public String decrypt(String encryptText) throws Exception {
byte[] saltBytes = salt.getBytes("UTF-8");
byte[] encryptTextBytes = new Base64().decode(encryptText);
SecretKeyFactory skf = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm);
PBEKeySpec spec = new PBEKeySpec(TOKEN.toCharArray(), saltBytes, this.pwdIterations, this.keySize);
SecretKey secretKey = skf.generateSecret(spec);
SecretKeySpec key = new SecretKeySpec(secretKey.getEncoded(), keyAlgorithm);
//decrypt the message
Cipher cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivBytes));
byte[] decyrptTextBytes = null;
try {
decyrptTextBytes = cipher.doFinal(encryptTextBytes);
} catch (IllegalBlockSizeException e) {
// TODO: handle exception
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
String text = new String(decyrptTextBytes);
return text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment