fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.security.SecureRandom;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.SecretKey;
  7.  
  8. class Main
  9. {
  10.  
  11. static final int ENCRYPT = -1;
  12. static final int DECRYPT = -2;
  13.  
  14. static byte[] encodeData;
  15. static String charSet =
  16. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  17.  
  18. static {
  19. encodeData = new byte[64];
  20. for (int i = 0; i<64; i++) {
  21. byte c = (byte) charSet.charAt(i);
  22. encodeData[i] = c;
  23. }
  24. }
  25.  
  26. public static void main (String[] args) throws java.lang.Exception
  27. {
  28. String ct = aes128("123", "hello", ENCRYPT );
  29. System.out.println(ct);
  30.  
  31. // should produce a different result
  32. ct = aes128("123", "hello", ENCRYPT );
  33. System.out.println(ct);
  34.  
  35. // attempting to decrypt
  36. System.out.println(aes128("123", ct, DECRYPT));
  37.  
  38. }
  39.  
  40. public static String aes128(String key, String data, final int direction) {
  41. SecureRandom rand = new SecureRandom(key.getBytes());
  42. byte[] randBytes = new byte[16];
  43. rand.nextBytes(randBytes);
  44. SecretKey encKey = new SecretKeySpec(randBytes, "AES");
  45.  
  46. Cipher cipher = null;
  47. try {
  48. cipher = Cipher.getInstance("AES");
  49. cipher.init((direction == ENCRYPT ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE), encKey);
  50. } catch (Exception e) {
  51. return null;
  52. }
  53.  
  54. try {
  55. if (direction == ENCRYPT) {
  56. byte[] encVal = cipher.doFinal(data.getBytes());
  57. return encode(encVal);
  58. } else {
  59. byte[] dataBytes = decode(data);
  60. byte[] encVal = cipher.doFinal(dataBytes);
  61. return new String(encVal);
  62. }
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. return null;
  66. }
  67. }
  68.  
  69.  
  70. public static String
  71. encode(String s) {
  72. return encode(s.getBytes());
  73. }
  74.  
  75. /**
  76.   * base-64 encode a byte array
  77.   * @param src The byte array to encode
  78.   * @returns The base64 encoded result
  79.   */
  80.  
  81. public static String
  82. encode(byte[] src) {
  83. return encode(src, 0, src.length);
  84. }
  85.  
  86. /**
  87.   * base-64 encode a byte array
  88.   * @param src The byte array to encode
  89.   * @param start The starting index
  90.   * @param len The number of bytes
  91.   * @returns The base64 encoded result
  92.   */
  93.  
  94. public static String
  95. encode(byte[] src, int start, int length) {
  96. byte[] dst = new byte[(length+2)/3 * 4 + length/72];
  97. int x = 0;
  98. int dstIndex = 0;
  99. int state = 0; // which char in pattern
  100. int old = 0; // previous byte
  101. int len = 0; // length decoded so far
  102. int max = length + start;
  103. for (int srcIndex = start; srcIndex<max; srcIndex++) {
  104. x = src[srcIndex];
  105. switch (++state) {
  106. case 1:
  107. dst[dstIndex++] = encodeData[(x>>2) & 0x3f];
  108. break;
  109. case 2:
  110. dst[dstIndex++] = encodeData[((old<<4)&0x30)
  111. | ((x>>4)&0xf)];
  112. break;
  113. case 3:
  114. dst[dstIndex++] = encodeData[((old<<2)&0x3C)
  115. | ((x>>6)&0x3)];
  116. dst[dstIndex++] = encodeData[x&0x3F];
  117. state = 0;
  118. break;
  119. }
  120. old = x;
  121. if (++len >= 72) {
  122. dst[dstIndex++] = (byte) '\n';
  123. len = 0;
  124. }
  125. }
  126.  
  127. /*
  128. * now clean up the end bytes
  129. */
  130.  
  131. switch (state) {
  132. case 1: dst[dstIndex++] = encodeData[(old<<4) & 0x30];
  133. dst[dstIndex++] = (byte) '=';
  134. dst[dstIndex++] = (byte) '=';
  135. break;
  136. case 2: dst[dstIndex++] = encodeData[(old<<2) & 0x3c];
  137. dst[dstIndex++] = (byte) '=';
  138. break;
  139. }
  140. return new String(dst);
  141. }
  142.  
  143. /**
  144.   * A Base64 decoder. This implementation is slow, and
  145.   * doesn't handle wrapped lines.
  146.   * The output is undefined if there are errors in the input.
  147.   * @param s a Base64 encoded string
  148.   * @returns The byte array eith the decoded result
  149.   */
  150.  
  151. public static byte[]
  152. decode(String s) {
  153. int end = 0; // end state
  154. if (s.endsWith("=")) {
  155. end++;
  156. }
  157. if (s.endsWith("==")) {
  158. end++;
  159. }
  160. int len = (s.length() + 3)/4 * 3 - end;
  161. byte[] result = new byte[len];
  162. int dst = 0;
  163. try {
  164. for(int src = 0; src< s.length(); src++) {
  165. int code = charSet.indexOf(s.charAt(src));
  166. if (code == -1) {
  167. break;
  168. }
  169. switch (src%4) {
  170. case 0:
  171. result[dst] = (byte) (code<<2);
  172. break;
  173. case 1:
  174. result[dst++] |= (byte) ((code>>4) & 0x3);
  175. result[dst] = (byte) (code<<4);
  176. break;
  177. case 2:
  178. result[dst++] |= (byte) ((code>>2) & 0xf);
  179. result[dst] = (byte) (code<<6);
  180. break;
  181. case 3:
  182. result[dst++] |= (byte) (code & 0x3f);
  183. break;
  184. }
  185. }
  186. return result;
  187. }
  188.  
  189. }
Success #stdin #stdout 0.33s 216960KB
stdin
Standard input is empty
stdout
zeMpipdoIfpMdEGpo/smPQ==
a4MHuUP/gr3SYqcp0JRlwA==
null