Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Java AES 256-bit Encryption/Decryption class

By: a guest on Jun 23rd, 2012  |  syntax: Java  |  size: 2.59 KB  |  views: 5,541  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.security.spec.KeySpec;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.SecretKeyFactory;
  5. import javax.crypto.spec.IvParameterSpec;
  6. import javax.crypto.spec.PBEKeySpec;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import sun.misc.BASE64Decoder;
  9. import sun.misc.BASE64Encoder;
  10.  
  11. public class AESEncrypter {
  12.  
  13.     private static final byte[] SALT = {
  14.         (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
  15.         (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
  16.     };
  17.     private static final int ITERATION_COUNT = 65536;
  18.     private static final int KEY_LENGTH = 256;
  19.     private Cipher ecipher;
  20.     private Cipher dcipher;
  21.    
  22.     AESEncrypter(String passPhrase) throws Exception {
  23.         SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  24.         KeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
  25.         SecretKey tmp = factory.generateSecret(spec);
  26.         SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
  27.  
  28.         ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  29.         ecipher.init(Cipher.ENCRYPT_MODE, secret);
  30.        
  31.         dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  32.         byte[] iv = ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
  33.         dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
  34.     }
  35.  
  36.     public String encrypt(String encrypt) throws Exception {
  37.         byte[] bytes = encrypt.getBytes("UTF8");
  38.         byte[] encrypted = encrypt(bytes);
  39.         return new BASE64Encoder().encode(encrypted);
  40.     }
  41.  
  42.     public byte[] encrypt(byte[] plain) throws Exception {
  43.         return ecipher.doFinal(plain);
  44.     }
  45.  
  46.     public String decrypt(String encrypt) throws Exception {
  47.         byte[] bytes = new BASE64Decoder().decodeBuffer(encrypt);
  48.         byte[] decrypted = decrypt(bytes);
  49.         return new String(decrypted, "UTF8");
  50.     }
  51.  
  52.     public byte[] decrypt(byte[] encrypt) throws Exception {
  53.         return dcipher.doFinal(encrypt);
  54.     }
  55.  
  56.     public static void main(String[] args) throws Exception {
  57.  
  58.         String message = "MESSAGE";
  59.         String password = "PASSWORD";
  60.  
  61.         AESEncrypter encrypter = new AESEncrypter(password);
  62.         String encrypted = encrypter.encrypt(message);
  63.         String decrypted = encrypter.decrypt(encrypted);
  64.  
  65.         System.out.println("Encrypt(\"" + message + "\", \"" + password + "\") = \"" + encrypted + "\"");
  66.         System.out.println("Decrypt(\"" + encrypted + "\", \"" + password + "\") = \"" + decrypted + "\"");
  67.     }
  68. }
clone this paste RAW Paste Data