1. public class SSL {
  2.  
  3. private final static String HEX = "0123456789ABCDEF";
  4. private final static String ENC = "US-ASCII";
  5.  
  6. private static final String RANDOM_ALGORITHM = "SHA1PRNG";
  7. private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
  8. private static final String SECRET_KEY_ALGORITHM = "AES";
  9.  
  10. private static IvParameterSpec ips;
  11.  
  12. public static void init(byte[] iv) {
  13. if(iv == null) {
  14. iv = new byte[16];
  15.  
  16. Random random = new Random();
  17. random.nextBytes(iv);
  18. }
  19.  
  20. ips = new IvParameterSpec(iv);
  21. }
  22.  
  23. public static byte[] getCertificate() {
  24. return ips.getIV();
  25. }
  26.  
  27. public static String encrypt(Session current, String cleartext) throws Exception {
  28. byte[] rawKey = getRawKey(current.getCurrentSession().getBytes(ENC));
  29. byte[] result = encrypt(rawKey, cleartext.getBytes(ENC));
  30. return toHex(result);
  31. }
  32.  
  33. public static String decrypt(Session current, String encrypted) throws Exception {
  34. byte[] rawKey = getRawKey(current.getCurrentSession().getBytes(ENC));
  35. byte[] enc = toByte(encrypted);
  36. byte[] result = decrypt(rawKey, enc);
  37. return new String(result, ENC);
  38. }
  39.  
  40. private static byte[] getRawKey(byte[] seed) throws Exception {
  41. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  42. SecureRandom sr = SecureRandom.getInstance(RANDOM_ALGORITHM);
  43. sr.setSeed(seed);
  44. kgen.init(128, sr); // 192 and 256 bits may not be available
  45. SecretKey skey = kgen.generateKey();
  46. byte[] raw = skey.getEncoded();
  47. return raw;
  48. }
  49.  
  50.  
  51. private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
  52. SecretKeySpec skeySpec = new SecretKeySpec(raw, SECRET_KEY_ALGORITHM);
  53. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
  54. cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);
  55. byte[] encrypted = cipher.doFinal(clear);
  56. return encrypted;
  57. }
  58.  
  59. private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
  60. SecretKeySpec skeySpec = new SecretKeySpec(raw, SECRET_KEY_ALGORITHM);
  61. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
  62. cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);
  63. byte[] decrypted = cipher.doFinal(encrypted);
  64. return decrypted;
  65. }
  66.  
  67. public static String toHex(String txt) throws UnsupportedEncodingException {
  68. return toHex(txt.getBytes(ENC));
  69. }
  70. public static String fromHex(String hex) throws UnsupportedEncodingException {
  71. return new String(toByte(hex), ENC);
  72. }
  73.  
  74. public static byte[] toByte(String hexString) {
  75. int len = hexString.length()/2;
  76. byte[] result = new byte[len];
  77. for (int i = 0; i < len; i++)
  78. result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
  79. return result;
  80. }
  81.  
  82. public static String toHex(byte[] buf) {
  83. if (buf == null)
  84. return "";
  85. StringBuffer result = new StringBuffer(2*buf.length);
  86. for (int i = 0; i < buf.length; i++) {
  87. appendHex(result, buf[i]);
  88. }
  89. return result.toString();
  90. }
  91.  
  92. private static void appendHex(StringBuffer sb, byte b) {
  93. sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
  94. }
  95. }