Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 26th, 2013  |  syntax: None  |  size: 2.30 KB  |  views: 48  |  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.io.ObjectInputStream.GetField;
  2. import java.security.MessageDigest;
  3. import java.security.SecureRandom;
  4. import java.security.spec.AlgorithmParameterSpec;
  5.  
  6. import javax.crypto.Cipher;
  7. import javax.crypto.spec.IvParameterSpec;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import javax.xml.bind.DatatypeConverter;
  10.  
  11. public class AESCrypt {
  12.  
  13.         private final Cipher cipher;
  14.         private final SecretKeySpec key;
  15.         private String encryptedText, decryptedText;
  16.         //private AlgorithmParameterSpec ivspec;
  17.  
  18.         public AESCrypt(String password) throws Exception {
  19.                 // hash password with SHA-256 and crop the output to 128-bit for key
  20.                 MessageDigest digest = MessageDigest.getInstance("SHA-256");
  21.                 digest.update(password.getBytes("UTF-8"));
  22.                 byte[] keyBytes = new byte[16];
  23.                 System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
  24.  
  25.                 cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  26.                 key = new SecretKeySpec(keyBytes, "AES");
  27.                 //byte[] iv = new byte[cipher.getBlockSize()];
  28.                 //ivspec = new IvParameterSpec(iv);
  29.                 getIV();
  30.         }
  31.        
  32.         public AlgorithmParameterSpec getIV() {
  33.                 AlgorithmParameterSpec ivspec;
  34.                 byte[] iv = new byte[cipher.getBlockSize()];
  35.                 new SecureRandom().nextBytes(iv);
  36.                 ivspec = new IvParameterSpec(iv);
  37.                 return ivspec;
  38.                 }
  39.  
  40.         public String encrypt(String plainText) throws Exception {
  41.                
  42.                 cipher.init(Cipher.ENCRYPT_MODE, key, getIV());
  43.                 byte[] encrypted = cipher.doFinal(plainText.getBytes());
  44.                 encryptedText = DatatypeConverter.printBase64Binary(encrypted);
  45.                 return encryptedText;
  46.         }
  47.  
  48.         public String decrypt(String cryptedText) throws Exception {
  49.                 cipher.init(Cipher.DECRYPT_MODE, key, getIV());
  50.                 byte[] bytes = DatatypeConverter.parseBase64Binary(cryptedText);
  51.                 byte[] decrypted = cipher.doFinal(bytes);
  52.                 decryptedText = new String(decrypted, "UTF-8");
  53.                 return decryptedText;
  54.         }      
  55.  
  56.         public static void main(String[] args) throws Exception {
  57.  
  58.                 System.out.print("....AES....\n");
  59.  
  60.                 String message = "DOBOJ";
  61.                 String password = "PASSWORD";
  62.  
  63.                 System.out.println("MSG:" + message);
  64.  
  65.                 AESCrypt aes = new AESCrypt(password);
  66.                 String encryptedText = aes.encrypt(message).toString();
  67.                 System.out.println("ENCRYPTED MSG: " + encryptedText);
  68.                 String decryptedText = aes.decrypt(encryptedText);
  69.                 System.out.print("DECRYPTED MSG: " + decryptedText);
  70.         }
  71.  
  72. }
clone this paste RAW Paste Data