메뉴 건너뛰기

조회 수 7735 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

2. JSP 테스트 페이지 만들기

Android WebView에서 우리가 만든 JSP 파일을 로드해서 사용할텐데 

로드된 페이지에서 전송할 파라미터 암호화를 안한다면 컨텐츠의 노출 위험이 있다.

그래서 암복호화 클래스를 만들어서 서버와 안드로이드에 로드하는 과정을 추가한다. 

( Web에서 파라미터 암호화 -> Android에서 파라미터 복호화)


암복호화 클래스

 


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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package com.test.decodeparam;
import java.util.Date;
 
public class Decrypt {
    public static void main(String[] args) {
        Decrypt dec = new Decrypt();
        // TODO Auto-generated method stub
        String Text = "TEST";
        System.out.println(Text);
        String strEncText = dec.AESEncrypt(Text, "Key");
        System.out.println(strEncText);
        String strDecText = dec.AESDecrypt(strEncText, "Key");
        System.out.println(strDecText);
    }
 
    String g_strBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 
    byte g_byArrSbox[] = {0x63,0x7c,0x77,0x7b,-14,0x6b,0x6f,-59,0x30,0x01,0x67,0x2b,-2,-41,-85,0x76,
                          -54,-126,-55,0x7d,-6,0x59,0x47,-16,-83,-44,-94,-81,-100,-92,0x72,-64,
                          -73,-3,-109,0x26,0x36,0x3f,-9,-52,0x34,-91,-27,-15,0x71,-40,0x31,0x15,
                          0x04,-57,0x23,-61,0x18,-106,0x05,-102,0x07,0x12,-128,-30,-21,0x27,-78,0x75,
                          0x09,-125,0x2c,0x1a,0x1b,0x6e,0x5a,-96,0x52,0x3b,-42,-77,0x29,-29,0x2f,-124,
                          0x53,-47,0x00,-19,0x20,-4,-79,0x5b,0x6a,-53,-66,0x39,0x4a,0x4c,0x58,-49,
                          -48,-17,-86,-5,0x43,0x4d,0x33,-123,0x45,-7,0x02,0x7f,0x50,0x3c,-97,-88,
                          0x51,-93,0x40,-113,-110,-99,0x38,-11,-68,-74,-38,0x21,0x10,-1,-13,-46,
                          -51,0x0c,0x13,-20,0x5f,-105,0x44,0x17,-60,-89,0x7e,0x3d,0x64,0x5d,0x19,0x73,
                          0x60,-127,0x4f,-36,0x22,0x2a,-112,-120,0x46,-18,-72,0x14,-34,0x5e,0x0b,-37,
                          -32,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,-62,-45,-84,0x62,-111,-107,-28,0x79,
                          -25,-56,0x37,0x6d,-115,-43,0x4e,-87,0x6c,0x56,-12,-22,0x65,0x7a,-82,0x08,
                          -70,0x78,0x25,0x2e,0x1c,-90,-76,-58,-24,-35,0x74,0x1f,0x4b,-67,-117,-118,
                          0x70,0x3e,-75,0x66,0x48,0x03,-10,0x0e,0x61,0x35,0x57,-71,-122,-63,0x1d,-98,
                          -31,-8,-104,0x11,0x69,-39,-114,-108,-101,0x1e,-121,-23,-50,0x55,0x28,-33,
                          -116,-95,-119,0x0d,-65,-26,0x42,0x68,0x41,-103,0x2d,0x0f,-80,0x54,-69,0x16};
 
    byte g_byArrRcon[][] = {{0x000x000x000x00},
                            {0x010x000x000x00},
                            {0x020x000x000x00},
                            {0x040x000x000x00},
                            {0x080x000x000x00},
                            {0x100x000x000x00},
                            {0x200x000x000x00},
                            {0x400x000x000x00},
                            {-1280x000x000x00},
                            {0x1b0x000x000x00},
                            {0x360x000x000x00}};
 
    public byte[] RotWord(byte[] byArrInput)
    {
        byte byArrRet[] = new byte[4];
        byArrRet[0= byArrInput[1];
        byArrRet[1= byArrInput[2];
        byArrRet[2= byArrInput[3];
        byArrRet[3= byArrInput[0];
 
        return byArrRet;
    }
 
    public void SubWord(byte[] byArr)
    {
        for (int nLoop = 0; nLoop < 4; nLoop++)
        {
            if (byArr[nLoop] < 0)
                byArr[nLoop] = g_byArrSbox[byArr[nLoop] + 256];
            else
                byArr[nLoop] = g_byArrSbox[byArr[nLoop]];
        }
    }
 
    public ByteArrayRet KeyExpansion(ByteArrayRet barInput)
    {
        ByteArrayRet ret = new ByteArrayRet();
        ret.nLength = 176;
 
        int nBlockSie = 4;
        int nKeyLength = 4;
        int nRound = 10;
        int nLoop;
        int nLoop2;
 
        // 앞 16 바이트 베끼기
        for (nLoop = 0; nLoop < 16; nLoop++)
            ret.byArray[nLoop] = barInput.byArray[nLoop];
 
        byte byArrTemp[] = new byte[4];
        byte byArrTemp2[] = new byte[4];
        for (nLoop = nKeyLength; nLoop < 44; nLoop++)
        {
            for (nLoop2 = 0; nLoop2 < 4; nLoop2++)
                byArrTemp[nLoop2] = ret.byArray[(nLoop - 1* 4 + nLoop2];
 
            if ((nLoop % nKeyLength) == 0)
            {
                byArrTemp2 = RotWord(byArrTemp);
                SubWord(byArrTemp2);
 
                for (nLoop2 = 0; nLoop2 < 4; nLoop2++)
                {
                    Integer nConvert = byArrTemp2[nLoop2] ^ g_byArrRcon[nLoop / nKeyLength][nLoop2];
                    byArrTemp[nLoop2] = nConvert.byteValue();
                }
            }
 
            for (nLoop2 = 0; nLoop2 < 4; nLoop2++)
            {
                Integer nConvert = ret.byArray[(nLoop - nKeyLength) * 4 + nLoop2] ^ byArrTemp[nLoop2];
                ret.byArray[nLoop * 4 + nLoop2] = nConvert.byteValue();
            }
        }
 
        return ret;
    }
 
    public ByteArrayRet EncodeUTF8(String strInput)
    {
        ByteArrayRet ret = new ByteArrayRet();
 
        int nOneChar;
        for (int nLoop = 0; nLoop < strInput.length(); nLoop++)
        {
            nOneChar = (int)strInput.charAt(nLoop);
 
            if (nOneChar < 0x80)
                ret.byArray[ret.nLength++= (byte)nOneChar;
            else if (nOneChar < 0x800)
            {
                ret.byArray[ret.nLength++= (byte)(0xc0 | nOneChar >> 6);
                ret.byArray[ret.nLength++= (byte)(0x80 | nOneChar & 0x3f);
            }
            else
            {
                ret.byArray[ret.nLength++= (byte)(0xe0 | nOneChar >> 12);
                ret.byArray[ret.nLength++= (byte)(0x80 | nOneChar >> 6 & 0x3f);
                ret.byArray[ret.nLength++= (byte)(0x80 | nOneChar & 0x3f);
            }
        }
 
        return ret;
    }
 
    public ByteArrayRet DecodeUTF8(ByteArrayRet barArg)
    {
        ByteArrayRet ret = new ByteArrayRet();
        int nDivide;
        for (int nLoop = 0; nLoop < barArg.nLength; nLoop++)
        {
            //if (barArg.byArray[nLoop] < 0x80)
            if (barArg.byArray[nLoop] >= 0)
            {
                ret.byArray[ret.nLength++= barArg.byArray[nLoop];
                ret.byArray[ret.nLength++= 0;
            }
            else
            {
                //if ((0xc0 <= barArg.byArray[nLoop]) && (barArg.byArray[nLoop] <= 0xdf) && (0x80 <= barArg.byArray[nLoop + 1]) && (barArg.byArray[nLoop + 1] <= 0xbf))
                if ((-64 <= barArg.byArray[nLoop]) && (barArg.byArray[nLoop] <= -33) && (0 > barArg.byArray[nLoop + 1]) && (barArg.byArray[nLoop + 1>= -65))
                {
                    nDivide = ((barArg.byArray[nLoop++] & 0x1f<< 6| (barArg.byArray[nLoop] & 0x3f);
                    ret.byArray[ret.nLength++= (byte)nDivide;
                    nDivide = nDivide >> 8;
                    ret.byArray[ret.nLength++= (byte)nDivide;
                }
                else
                {
                    //if ((0xe0 <= barArg.byArray[nLoop]) && (barArg.byArray[nLoop] <= 0xef) && (0x80 <= barArg.byArray[nLoop + 1]) && (barArg.byArray[nLoop + 1] <= 0xbf) && (0x80 <= barArg.byArray[nLoop + 2]) && (barArg.byArray[nLoop + 2] <= 0xbf))
                    if ((-32 <= barArg.byArray[nLoop]) && (barArg.byArray[nLoop] <= -17) && (-65 >= barArg.byArray[nLoop + 1]) && (-65 >= barArg.byArray[nLoop + 2]))
                    {
                        nDivide = ((barArg.byArray[nLoop++] & 0xf<< 12| ((barArg.byArray[nLoop++] & 0x3f<< 6| (barArg.byArray[nLoop] & 0x3f);
                        ret.byArray[ret.nLength++= (byte)nDivide;
                        nDivide = nDivide >> 8;
                        ret.byArray[ret.nLength++= (byte)nDivide;
                    }
                }
            }
 
            // 이거 넣으면 어떨까?
            if ((ret.byArray[ret.nLength - 2== 0) && ((ret.nLength % 2== 0))
                return ret;
        }
 
        return ret;
    }
 
    public class ByteArrayRet
    {
        byte byArray[];
        int nLength;
 
        public ByteArrayRet()
        {
            byArray = new byte[1024];
            nLength = 0;
        }
 
        public String toString2()
        {
            String strRetVal = "";
            String strTemp;
 
            //strRetVal = this.byArray.toString();
 
            for (int nTemp = 0; nTemp < nLength; nTemp++)
            {
                if ((nTemp % 8== 0)
                    strRetVal += "<br>";
 
                strTemp = Integer.toHexString(byArray[nTemp]);
                if (strTemp.length() > 2)
                    strRetVal += strTemp.substring(6+ ", ";
                else if (strTemp.length() == 1)
                    strRetVal += "0" + strTemp + ", ";
                else
                    strRetVal += strTemp + ", ";
            }
 
            return strRetVal;
        }
    }
 
    public void AddRoundKey(ByteArrayRet barArg, ByteArrayRet barKey, int nRound, int nArg)
    {
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < nArg; j++){
                barArg.byArray[i * 4 + j] ^= barKey.byArray[(nRound * 4 + j) * 4 + i];
            }
    }
 
    public void SubBytes(ByteArrayRet barArg, int nArg)
    {
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < nArg; j++){
                if (barArg.byArray[i * 4 + j] < 0)
                    barArg.byArray[i * 4 + j] = g_byArrSbox[barArg.byArray[i * 4 + j] + 256];
                else
                    barArg.byArray[i * 4 + j] = g_byArrSbox[barArg.byArray[i * 4 + j]];
            }
    }
 
    public void ShiftRows(ByteArrayRet barArg, int nArg)
    {
        byte byTemp[] = new byte[4];
        for (int i = 1; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
                byTemp[j] = barArg.byArray[i * 4 + (i + j) % nArg];
 
            for (int j = 0; j < 4; j++)
                barArg.byArray[i * 4 + j] = byTemp[j];
        }
    }
 
    public void MixColumns(ByteArrayRet barArg)
    {
        byte byTemp1[] = new byte[4];
        byte byTemp2[] = new byte[4];
        Integer nTemp;
 
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                byTemp1[j] = barArg.byArray[j * 4 + i];
                nTemp = (( barArg.byArray[j * 4 + i] & 0x80!= 0) ? barArg.byArray[j * 4 + i] << 1 ^ 0x11b : barArg.byArray[j * 4 + i] << 1;
                byTemp2[j] = nTemp.byteValue();
            }
 
            nTemp = byTemp2[0] ^ byTemp1[1] ^ byTemp2[1] ^ byTemp1[2] ^ byTemp1[3];
            barArg.byArray[0 * 4 + i] = nTemp.byteValue();
            nTemp = byTemp1[0] ^ byTemp2[1] ^ byTemp1[2] ^ byTemp2[2] ^ byTemp1[3];
            barArg.byArray[1 * 4 + i] = nTemp.byteValue();
            nTemp = byTemp1[0] ^ byTemp1[1] ^ byTemp2[2] ^ byTemp1[3] ^ byTemp2[3];
            barArg.byArray[2 * 4 + i] = nTemp.byteValue();
            nTemp = byTemp1[0] ^ byTemp2[0] ^ byTemp1[1] ^ byTemp1[2] ^ byTemp2[3];
            barArg.byArray[3 * 4 + i] = nTemp.byteValue();
        }
    }
    
    String EncodeBase64(ByteArrayRet barArg, int nLength)
    {
        String strRet = new String();
        
        byte ch1, ch2, ch3;
        byte nEnc1, nEnc2, nEnc3, nEnc4;
        int nLoop = 0;
        
        do
        {
            ch1 = barArg.byArray[nLoop++];
            ch2 = (nLoop++ < nLength) ? barArg.byArray[nLoop - 1] : 0;
            ch3 = (nLoop++ < nLength) ? barArg.byArray[nLoop - 1] : 0;
            
            nEnc1 = (byte)((ch1 >> 2) & 0x3f);
            nEnc2 = (byte)(((ch1 & 3<< 4| ((ch2 >> 4) & 0x0f));
            nEnc3 = (byte)(((ch2 & 15<< 2| ((ch3 >> 6) & 0x03));
            nEnc4 = (byte)(ch3 & 63);
            
            switch (nLoop - nLength)
            {
            case 1:
                nEnc4 = (byte)64;
                break;
                
            case 2:
                nEnc3 = nEnc4 = (byte)64;
                break;
            }
            
            Character ch = new Character(g_strBase64.charAt(nEnc1));
            strRet += ch.toString();
            ch = new Character(g_strBase64.charAt(nEnc2));
            strRet += ch.toString();
            ch = new Character(g_strBase64.charAt(nEnc3));
            strRet += ch.toString();
            ch = new Character(g_strBase64.charAt(nEnc4));
            strRet += ch.toString();
        }
        while (nLoop < nLength);
        
        return strRet;
    }
    
    public ByteArrayRet DecodeBase64(String strCipherText)
    {
        ByteArrayRet ret = new ByteArrayRet();
        ret.nLength = 0;
 
        int nEnc1, nEnc2, nEnc3, nEnc4;
        int ch1, ch2, ch3;
        int nLoop = 0;
 
        do
        {
            nEnc1 = g_strBase64.indexOf(strCipherText.charAt(nLoop++));
            nEnc2 = g_strBase64.indexOf(strCipherText.charAt(nLoop++));
            nEnc3 = g_strBase64.indexOf(strCipherText.charAt(nLoop++));
            nEnc4 = g_strBase64.indexOf(strCipherText.charAt(nLoop++));
 
            ch1 = (nEnc1 << 2| (nEnc2 >> 4);
            ch2 = ((nEnc2 & 15<< 4 ) | (nEnc3 >> 2);
            ch3 = ((nEnc3 & 3<< 6| nEnc4;
 
            ret.byArray[ret.nLength++= (byte)ch1;
            if (nEnc3 != 64)
                ret.byArray[ret.nLength++= (byte)ch2;
            if (nEnc4 != 64)
                ret.byArray[ret.nLength++= (byte)ch3;
        }
        while (nLoop < strCipherText.length());
 
        return ret;
    }
 
    public ByteArrayRet Cipher(ByteArrayRet barKey, ByteArrayRet barKE)
    {
        ByteArrayRet ret = new ByteArrayRet(); 
        ret.nLength = 16;
 
        int nBlockSize = 4;
        int nRound = 10;
        int nLoop = 0;
 
        ByteArrayRet barState = new ByteArrayRet();
        barState.nLength = 16;
        for (nLoop = 0; nLoop < 16; nLoop++)
            barState.byArray[(nLoop % 4* 4 + (int)Math.floor(nLoop / 4)] = barKey.byArray[nLoop];
        
        
        AddRoundKey(barState, barKE, 0, nBlockSize);
 
        for (nLoop = 1; nLoop < nRound; nLoop++)
        {
            SubBytes(barState, nBlockSize);
            ShiftRows(barState, nBlockSize);
            MixColumns(barState);
            AddRoundKey(barState, barKE, nLoop, nBlockSize);
        }
 
        SubBytes(barState, nBlockSize);
        ShiftRows(barState, nBlockSize);
        AddRoundKey(barState, barKE, nRound, nBlockSize);
 
        for (nLoop = 0; nLoop < 4 * nBlockSize; nLoop++)
            ret.byArray[nLoop] = barState.byArray[(nLoop % 4* 4 + (int)Math.floor(nLoop / 4)];
        
        return ret;
    }
 
    public String AESEncrypt(String strPlainText, String strPassword)
    {
        // strPlainText를 UTF8로
        ByteArrayRet barUTF8Text = EncodeUTF8(strPlainText);
 
        // strPassword를 UTF8로
        ByteArrayRet barUTF8PW = EncodeUTF8(strPassword);
 
        // use AES itself to encrypt password to get cipher key (using plain password as source for key expansion) - gives us well encrypted key
        ByteArrayRet barKey = new ByteArrayRet();
        barKey.nLength = 16;
        barKey.byArray = new byte[16];
        int nLoop;
        for (nLoop = 0; nLoop < 16; nLoop++){
            barKey.byArray[nLoop] = /*isNaN(barUTF8PW.byArray[nLoop]) ? 0 : */barUTF8PW.byArray[nLoop];
        }
        
        // 키 만들기
        ByteArrayRet barKE = KeyExpansion(barKey);
 
        // 암호화
        ByteArrayRet barCipher = Cipher(barKey, barKE);
        
        // 시간 알아오기
        Date dNow = new Date();
        long lCurrentTime = dNow.getTime();
        long lTime = lCurrentTime / 1000;
        byte bySec = (byte)(lTime & 0xFF);
        
        /*
        /////////////////////////////////////////////////////////
        lTime = 1239583474;
        bySec = 34;
        /////////////////////////////////////////////////////////
        */
 
        // initialise counter block (NIST SP800-38A §B.2): millisecond time-stamp for nonce in 1st 8 bytes, block counter in 2nd 8 bytes
        ByteArrayRet barCounterBlock = new ByteArrayRet();
        barCounterBlock.nLength = 16;
        barCounterBlock.byArray = new byte[16];
        
        // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes
        for (nLoop = 0; nLoop < 4; nLoop++)
        {
            barCounterBlock.byArray[nLoop] = (byte)((lTime >> nLoop * 8) & 0xFF);
            barCounterBlock.byArray[nLoop + 4= (byte)(bySec & 0xFF);
        }
        
        // and convert it to a string to go on the front of the ciphertext
        int nBlockCount = (int)Math.ceil((double)barUTF8Text.nLength / 16);
        
        ByteArrayRet barCipherText = new ByteArrayRet();
        barCipherText.nLength = nBlockCount * 16 + 8;
        barCipherText.byArray = new byte[barCipherText.nLength];
        barCipherText.byArray[0= barCounterBlock.byArray[0];
        barCipherText.byArray[1= barCounterBlock.byArray[1];
        barCipherText.byArray[2= barCounterBlock.byArray[2];
        barCipherText.byArray[3= barCounterBlock.byArray[3];
        barCipherText.byArray[4= barCounterBlock.byArray[4];
        barCipherText.byArray[5= barCounterBlock.byArray[5];
        barCipherText.byArray[6= barCounterBlock.byArray[6];
        barCipherText.byArray[7= barCounterBlock.byArray[7];
        
        // generate key schedule - an expansion of the key into distinct Key Rounds for each round
        ByteArrayRet barKeySchedule = KeyExpansion(barCipher);
        
        int nBlockSize = 16;
        int nBlockLength = 0;
        int nLoop2 = 0;
        ByteArrayRet barCipherCntr = new ByteArrayRet();
        
        for (nLoop = 0; nLoop < nBlockCount; nLoop++)
        {
            barCounterBlock.byArray[15= (byte)nLoop;
            
            barCipherCntr = Cipher(barCounterBlock, barKeySchedule);
            
            nBlockLength = (nLoop < nBlockCount - 1) ? nBlockSize : (barUTF8Text.nLength - 1) % nBlockSize + 1;
            for (nLoop2 = 0; nLoop2 < nBlockLength; nLoop2++)
                barCipherText.byArray[nLoop * 16 + nLoop2 + 8= (byte)(barCipherCntr.byArray[nLoop2] ^ barUTF8Text.byArray[nLoop * nBlockSize + nLoop2]);
            
        }
        
        return EncodeBase64(barCipherText, (nLoop - 1* 16 + nLoop2 + 8);
        
        //return strRet;
    }
 
    public String AESDecrypt(String strCipherText, String strPassword)
    {
        // strCipherText를 Base64 풀기
        ByteArrayRet barBase64Dec = DecodeBase64(strCipherText);
        
        // strPassword를 UTF8로
        ByteArrayRet barUTF8PW = EncodeUTF8(strPassword);
        
 
        // use AES to encrypt password (mirroring encrypt routine)
        ByteArrayRet barKey = new ByteArrayRet();// byArray
        barKey.nLength = 16;
        barKey.byArray = new byte[16];
        for (int nLoop = 0; nLoop < 16; nLoop++)
            barKey.byArray[nLoop] = barUTF8PW.byArray[nLoop];
        
        // 키 만들기
        ByteArrayRet barKE = KeyExpansion(barKey);// byKey
 
        // 암호화
        ByteArrayRet barCipher = Cipher(barKey, barKE);// byCipher
 
        // recover nonce from 1st 8 bytes of ciphertext
        ByteArrayRet barCounterBlock = new ByteArrayRet();// byCounterBlock
        barCounterBlock.nLength = 16;
        for (int nLoop = 0; nLoop < 8; nLoop++)
            barCounterBlock.byArray[nLoop] = barBase64Dec.byArray[nLoop];
 
        // generate key schedule
        ByteArrayRet barKeySchedule = KeyExpansion(barCipher);
        
        int nBlockSize = 16;
        int nBlocks = (int)Math.ceil((double)(barBase64Dec.nLength - 8/ nBlockSize);
 
        ByteArrayRet barUTF8Text = new ByteArrayRet();
        //barUTF8Text.nLength = 1024;
        barUTF8Text.nLength = barBase64Dec.nLength;
 
        for (Integer nLoop = 0; nLoop < nBlocks; nLoop++)
        {
            barCounterBlock.byArray[15= nLoop.byteValue();
            ByteArrayRet barCipherCntr = Cipher(barCounterBlock, barKeySchedule);
 
            for (int nLoop2 = 0; nLoop2 < 16; nLoop2++)
            {
                if ((16 * nLoop + nLoop2 + 8>= barBase64Dec.nLength)
                    break;
 
                Integer nConvert = barCipherCntr.byArray[nLoop2] ^ barBase64Dec.byArray[16 * nLoop + nLoop2 + 8];
                barUTF8Text.byArray[16 * nLoop + nLoop2] = nConvert.byteValue();
            }
        }
 
        // new String (barUTF8Text.byArray) 라고 하지 않고 아래와 같이 하는 것은, 뒤에 붙은 0들 때문에 글자로 바꾸다가 페이지가 죽기 때문이다.
        int nResultLength = 0;
        for (int nLoop = 0; nLoop < barUTF8Text.nLength; nLoop++)
        {
            if (barUTF8Text.byArray[nLoop] == 0)
                break;
 
            nResultLength++;
        }
        
        byte byResult[] = new byte[nResultLength];
        for (int nLoop = 0; nLoop < nResultLength; nLoop++)
            byResult[nLoop] = barUTF8Text.byArray[nLoop];
 
        String strRet = new String (byResult);
 
        return strRet;
    }
 
}
 
cs
 


* AES 알고리즘 사용

더 강한 알려진 알고리즘을 사용해도 된다. 외부에서 필요한 것은

String strEncText = dec.AESEncrypt(Text, "Key");

String strEncText = dec.AESDecrypt(Text, "Key");


톰캣과 안드로이드에서 가져다 쓸수있게 만들어준다.


Java Class 파일을

1. 톰캣설치경로\webapps\웹프로젝트경로\ 에 복사 ( 위치는 상관없음. 최종적으로 컴파일한 후에 class 파일의 위치가 중요함 )

2. 안드로이드 프로젝트 src에 복사 or Java class 파일을 Jar 파일로 만들어서 안드로이드 프로젝트에 import


웹과 안드로이드 프로젝트 환경에서 가져다 쓸수있도록 한다.


웹에서는 단순하게 cmd를 사용했다.

javac -d . Decrypt.java  해서 컴파일하면 

톰캣설치경로\webapps\웹프로젝트경로\com\test\decodeparam\Decrypt.class 가 생성된다. ( 패키지명대로 디렉터리 생성)


java -cp . com.test.decodeparam.Decrypt 해서 실행 확인해본다.

public static void main(String[] args) {
Decrypt dec = new Decrypt();
// TODO Auto-generated method stub
String Text = "TEST";
System.out.println(Text);
String strEncText = dec.AESEncrypt(Text, "Key");
System.out.println(strEncText);
String strDecText = dec.AESDecrypt(strEncText, "Key");
System.out.println(strDecText)
}

의 결과는

TEST //원문
FrshVxYWFhbjM88g //암호화문
TEST //복호화문

암복호화가 정상적으로 이루어지는 클래스임을 파악했다.

 

 톰캣설치경로\webapps\웹프로젝트경로\에


WEB-INF\classes\ 를 만들고 좀 전에 만든 com 이하 폴더들을 복사해 놓는다.

JSP 안에서 <%@page import="com.test.decodeparam.Decrypt"%> 를 하면

WEB-INF\classes\com\test\decodeparam\Decrypt.class 에서 클래스 파일을 가져와서

Decrypt 클래스를 JSP 상에서 사용할 수 있다.


List of Articles
번호 제목 날짜 조회 수
257 화면 회전에 따른 애니메이션 효과 구현하기 2015.07.16 8055
256 화면 해상도에 관계없는 레이아웃(Layout) 만들기 file 2015.07.16 8640
255 화면 전환해도 데이터 유지 예제 2015.07.26 9204
254 하이브리드앱 기본 - WebView로 웹페이지 띄우기 file 2020.12.14 1025
253 하이브리드 앱에서의 세션관리(로그인 상태 유지) 2018.12.27 4983
252 푸시 서비스(GCM)에 대해 알아보자 file 2015.07.01 7000
251 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (3) file 2015.07.16 6267
250 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (2) file 2015.07.16 7292
249 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (1) file 2015.07.16 6726
248 폰갭(PhoneGap) 플러그인 사용하기 2015.06.29 7332
247 폰갭(PhoneGap) 플러그인 만들기 2015.06.29 8379
246 폰갭(PhoneGap) 에서 페이지들간의 이동 2015.06.29 8446
245 폰갭(PhoneGap) & jQuery Mobile 로 안드로이드 어플 개발 file 2015.06.29 7839
244 폰갭 비콘 디텍팅 안될 때 (기본적인건 다 되있어야됨) 2015.07.26 6528
243 패키지명을 한꺼번에 변경하기 (Refactor) file 2020.12.14 294
242 특정 폴더에서 오래된 파일 삭제하기 2015.07.16 6767
241 트리뷰(TreeView) 컨트롤 file 2014.10.16 6719
240 탭 뷰에 탭 추가하기, 아이콘 넣기 file 2015.07.16 9358
239 클래스나눠서 xml 파싱과 FTP를이용하여 안드로이드에서 활용하기 2014.08.28 6180
238 카카오톡 분석하기 (2) - 카카오톡 암호화 함수 찾기 file 2016.05.26 9598
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved