Skip to content

Instantly share code, notes, and snippets.

@ansig
Created November 8, 2014 08:18
Show Gist options
  • Save ansig/e9021b4d02e2b023208f to your computer and use it in GitHub Desktop.
Save ansig/e9021b4d02e2b023208f to your computer and use it in GitHub Desktop.
Encrypting and decrypting a text using a passphrase and AES. The encryption key is generated from a passphrase using PBKDF2.
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
/**
* Encrypting and decrypting a text using a passphrase and AES. The encryption key is generated from a passphrase using PBKDF2
*/
public class AESEncryptorDecryptor {
private static final String STRING_ENCODING = "UTF-8";
private static final String KEY_ALGORITHM = "PBKDF2WithHmacSHA1";
private static final int KEY_SALT_BYTES = 16;
private static final int KEY_ITERATIONS = 65536;
private static final int KEY_BITS = 128;
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
public static void main(String[] args) throws Exception {
String clearText = "Detta är strängen";
String passphrase = "This is the passphrase";
System.out.printf("The cleartext: %s%n", clearText);
System.out.printf("The key text: %s%n", passphrase);
SecretKey aesKey = createAesKey(passphrase);
// Create the cipher instance
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
// Encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
// the iv needs to be saved together with the encrypted bytes
// in order to be able to decrypt the password at a later stage
AlgorithmParameters params = cipher.getParameters();
byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
byte[] encryptedBytes = cipher.doFinal(clearText.getBytes(STRING_ENCODING));
System.out.printf("The encrypted text: %s%n", Base64.encode(encryptedBytes));
// Decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(iv));
byte[] decrypted = cipher.doFinal(encryptedBytes);
System.out.printf("The decrypted text: %s%n", new String(decrypted));
}
private static SecretKey createAesKey(String passphrase) throws NoSuchAlgorithmException, InvalidKeySpecException {
// generate random salt
SecureRandom random = new SecureRandom();
byte[] salt = new byte[KEY_SALT_BYTES];
random.nextBytes(salt);
// hash the password
PBEKeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt, KEY_ITERATIONS, KEY_BITS);
SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
byte[] keyHash = factory.generateSecret(spec).getEncoded();
return new SecretKeySpec(keyHash, "AES");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment