Advertisement
Guest User

AESALGO

a guest
Jun 13th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.security.SecureRandom;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.KeyGenerator;
  4. import javax.crypto.SecretKey;
  5. import javax.crypto.spec.SecretKeySpec;
  6.  
  7. /**
  8.  * Usage:
  9.  *
  10.  * <pre>
  11.  * String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
  12.  * ...
  13.  * String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)
  14.  * </pre>
  15.  *
  16.  * @author ferenc.hechler
  17.  */
  18. public class AES_Algo {
  19.  
  20.     public static String encrypt(String seed, String cleartext)
  21.             throws Exception {
  22.         byte[] rawKey = getRawKey(seed.getBytes());
  23.         byte[] result = encrypt(rawKey, cleartext.getBytes());
  24.         return toHex(result);
  25.     }
  26.  
  27.     public static String decrypt(String seed, String encrypted)
  28.             throws Exception {
  29.         byte[] rawKey = getRawKey(seed.getBytes());
  30.         byte[] enc = toByte(encrypted);
  31.         byte[] result = decrypt(rawKey, enc);
  32.         return new String(result);
  33.     }
  34.  
  35.     private static byte[] getRawKey(byte[] seed) throws Exception {
  36.         KeyGenerator kgen = KeyGenerator.getInstance("AES");
  37.         SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  38.         sr.setSeed(seed);
  39.         kgen.init(128, sr); // 192 and 256 bits may not be available
  40.         SecretKey skey = kgen.generateKey();
  41.         byte[] raw = skey.getEncoded();
  42.         return raw;
  43.     }
  44.  
  45.     private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
  46.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  47.         Cipher cipher = Cipher.getInstance("AES");
  48.         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  49.         byte[] encrypted = cipher.doFinal(clear);
  50.         return encrypted;
  51.     }
  52.  
  53.     private static byte[] decrypt(byte[] raw, byte[] encrypted)
  54.             throws Exception {
  55.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  56.         Cipher cipher = Cipher.getInstance("AES");
  57.         cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  58.         byte[] decrypted = cipher.doFinal(encrypted);
  59.         return decrypted;
  60.     }
  61.  
  62.     public static String toHex(String txt) {
  63.         return toHex(txt.getBytes());
  64.     }
  65.  
  66.     public static String fromHex(String hex) {
  67.         return new String(toByte(hex));
  68.     }
  69.  
  70.     public static byte[] toByte(String hexString) {
  71.         int len = hexString.length() / 2;
  72.         byte[] result = new byte[len];
  73.         for (int i = 0; i < len; i++)
  74.             result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
  75.                     16).byteValue();
  76.         return result;
  77.     }
  78.  
  79.     public static String toHex(byte[] buf) {
  80.         if (buf == null)
  81.             return "";
  82.         StringBuffer result = new StringBuffer(2 * buf.length);
  83.         for (int i = 0; i < buf.length; i++) {
  84.             appendHex(result, buf[i]);
  85.         }
  86.         return result.toString();
  87.     }
  88.  
  89.     private final static String HEX = "0123456789ABCDEF";
  90.  
  91.     private static void appendHex(StringBuffer sb, byte b) {
  92.         sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement