Skip to content

Instantly share code, notes, and snippets.

@RichardHJensen
Created September 22, 2011 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardHJensen/1234973 to your computer and use it in GitHub Desktop.
Save RichardHJensen/1234973 to your computer and use it in GitHub Desktop.
Class using base64 strings to initialize key and vector
package com.rhjensen.encryption;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class AESEncryptor {
private Cipher encryptor;
private Cipher decryptor;
public AESEncryptor(String sessionKey, String iv) {
byte[] keyBytes;
byte[] vectorBytes;
try {
keyBytes = new BASE64Decoder().decodeBuffer(sessionKey);
vectorBytes = new BASE64Decoder().decodeBuffer(iv);
encryptor = Cipher.getInstance("AES/CBC/PKCS5Padding");
encryptor.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(keyBytes, "AES"),
new IvParameterSpec(vectorBytes));
decryptor = Cipher.getInstance("AES/CBC/PKCS5Padding");
decryptor.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(keyBytes, "AES"),
encryptor.getParameters());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String encrypt(String plainText) throws UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
// get bytes from string, encrypt, encode
byte[] utf8bytes = plainText.getBytes("utf-8");
byte[] ciphertext = encryptor.doFinal(utf8bytes);
return new BASE64Encoder().encode(ciphertext);
}
public String decrypt(String cipherText) throws IOException, IllegalBlockSizeException, BadPaddingException {
// decode, decrypt, use bytes to create string
byte[] encryptedBytes = new BASE64Decoder().decodeBuffer(cipherText);
byte[] plaintext = decryptor.doFinal(encryptedBytes);
return new String(plaintext);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment