thieumao

AES demo

Oct 3rd, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package aesdemo;
  6.  
  7. import java.security.SecureRandom;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.KeyGenerator;
  10. import javax.crypto.SecretKey;
  11. import javax.crypto.spec.SecretKeySpec;
  12.  
  13. /**
  14.  *
  15.  * @author leo
  16.  */
  17. public class AESdemo {
  18.  
  19.     public static void main(String[] args) {
  20.         try {
  21.             String plaintext = "Hello world", key = "test";
  22.             String ciphertext = encrypt(key, plaintext);
  23.             String plaintext2 = decrypt(key, ciphertext);
  24.             System.out.println("Encrypting '" + plaintext
  25.                     + "' yields: (" + ciphertext.length() + ") " + ciphertext);
  26.             System.out.println("Decrypting it yields: " + plaintext2);
  27.         } catch (Exception ex) {
  28.             ex.printStackTrace();
  29.         }
  30.     }
  31.  
  32.     public static String encrypt(String seed, String cleartext) throws Exception {
  33.         byte[] rawKey = getRawKey(seed.getBytes());
  34.         byte[] result = encrypt(rawKey, cleartext.getBytes());
  35.         return toHex(result);
  36.     }
  37.  
  38.     public static String decrypt(String seed, String encrypted) throws Exception {
  39.         byte[] rawKey = getRawKey(seed.getBytes());
  40.         byte[] enc = toByte(encrypted);
  41.         byte[] result = decrypt(rawKey, enc);
  42.         return new String(result);
  43.     }
  44.  
  45.     private static byte[] getRawKey(byte[] seed) throws Exception {
  46.         KeyGenerator kgen = KeyGenerator.getInstance("AES");
  47.         SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  48.         sr.setSeed(seed);
  49.         kgen.init(128, sr); // 192 and 256 bits may not be available
  50.         SecretKey skey = kgen.generateKey();
  51.         byte[] raw = skey.getEncoded();
  52.         return raw;
  53.     }
  54.  
  55.     private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
  56.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  57.         Cipher cipher = Cipher.getInstance("AES");
  58.         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  59.         byte[] encrypted = cipher.doFinal(clear);
  60.         return encrypted;
  61.     }
  62.  
  63.     private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
  64.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  65.         Cipher cipher = Cipher.getInstance("AES");
  66.         cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  67.         byte[] decrypted = cipher.doFinal(encrypted);
  68.         return decrypted;
  69.     }
  70.  
  71.     public static String toHex(String txt) {
  72.         return toHex(txt.getBytes());
  73.     }
  74.  
  75.     public static String fromHex(String hex) {
  76.         return new String(toByte(hex));
  77.     }
  78.  
  79.     public static byte[] toByte(String hexString) {
  80.         int len = hexString.length() / 2;
  81.         byte[] result = new byte[len];
  82.         for (int i = 0; i < len; i++) {
  83.             result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
  84.         }
  85.         return result;
  86.     }
  87.  
  88.     public static String toHex(byte[] buf) {
  89.         if (buf == null) {
  90.             return "";
  91.         }
  92.         StringBuffer result = new StringBuffer(2 * buf.length);
  93.         for (int i = 0; i < buf.length; i++) {
  94.             appendHex(result, buf[i]);
  95.         }
  96.         return result.toString();
  97.     }
  98.     private final static String HEX = "0123456789ABCDEF";
  99.  
  100.     private static void appendHex(StringBuffer sb, byte b) {
  101.         sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment