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