Source Code Cross Referenced for UnixCrypt.java in  » Sevlet-Container » jetty-modules » org » mortbay » jetty » security » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Sevlet Container » jetty modules » org.mortbay.jetty.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)UnixCrypt.java	0.9 96/11/25
003:         *
004:         * Copyright (c) 1996 Aki Yoshida. All rights reserved.
005:         *
006:         * Permission to use, copy, modify and distribute this software
007:         * for non-commercial or commercial purposes and without fee is
008:         * hereby granted provided that this copyright notice appears in
009:         * all copies.
010:         */
011:
012:        /**
013:         * Unix crypt(3C) utility
014:         *
015:         * @version 	0.9, 11/25/96
016:         * @author 	Aki Yoshida
017:         */
018:
019:        /**
020:         * modified April 2001
021:         * by Iris Van den Broeke, Daniel Deville
022:         */package org.mortbay.jetty.security;
023:
024:        /* ------------------------------------------------------------ */
025:        /** Unix Crypt.
026:         * Implements the one way cryptography used by Unix systems for
027:         * simple password protection.
028:         * @version $Id: UnixCrypt.java,v 1.1 2005/10/05 14:09:14 janb Exp $
029:         * @author Greg Wilkins (gregw)
030:         */
031:        public class UnixCrypt extends Object {
032:
033:            /* (mostly) Standard DES Tables from Tom Truscott */
034:            private static final byte[] IP = { /* initial permutation */
035:            58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62,
036:                    54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
037:                    57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11,
038:                    3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23,
039:                    15, 7 };
040:
041:            /* The final permutation is the inverse of IP - no table is necessary */
042:            private static final byte[] ExpandTr = { /* expansion operation */
043:            32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13,
044:                    14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24,
045:                    25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 };
046:
047:            private static final byte[] PC1 = { /* permuted choice table 1 */
048:            57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59,
049:                    51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
050:
051:                    63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14,
052:                    6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 };
053:
054:            private static final byte[] Rotates = { /* PC1 rotation schedule */
055:            1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
056:
057:            private static final byte[] PC2 = { /* permuted choice table 2 */
058:            9, 18, 14, 17, 11, 24, 1, 5, 22, 25, 3, 28, 15, 6, 21, 10, 35, 38,
059:                    23, 19, 12, 4, 26, 8, 43, 54, 16, 7, 27, 20, 13, 2,
060:
061:                    0, 0, 41, 52, 31, 37, 47, 55, 0, 0, 30, 40, 51, 45, 33, 48,
062:                    0, 0, 44, 49, 39, 56, 34, 53, 0, 0, 46, 42, 50, 36, 29, 32 };
063:
064:            private static final byte[][] S = { /* 48->32 bit substitution tables */
065:                    /* S[1]			*/
066:                    { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0,
067:                            15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
068:                            4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5,
069:                            0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0,
070:                            6, 13 },
071:                    /* S[2]			*/
072:                    { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3,
073:                            13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
074:                            0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2,
075:                            15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5,
076:                            14, 9 },
077:                    /* S[3]			*/
078:                    { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13,
079:                            7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
080:                            13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14,
081:                            7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5,
082:                            2, 12 },
083:                    /* S[4]			*/
084:                    { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13,
085:                            8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
086:                            10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8,
087:                            4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7,
088:                            2, 14 },
089:                    /* S[5]			*/
090:                    { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14,
091:                            11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
092:                            4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0,
093:                            14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4,
094:                            5, 3 },
095:                    /* S[6]			*/
096:                    { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10,
097:                            15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
098:                            9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11,
099:                            6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0,
100:                            8, 13 },
101:                    /* S[7]			*/
102:                    { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13,
103:                            0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
104:                            1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9,
105:                            2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2,
106:                            3, 12 },
107:                    /* S[8]			*/
108:                    { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1,
109:                            15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
110:                            7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5,
111:                            8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5,
112:                            6, 11 } };
113:
114:            private static final byte[] P32Tr = { /* 32-bit permutation function */
115:            16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8,
116:                    24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 };
117:
118:            private static final byte[] CIFP = { /* compressed/interleaved permutation */
119:            1, 2, 3, 4, 17, 18, 19, 20, 5, 6, 7, 8, 21, 22, 23, 24, 9, 10, 11,
120:                    12, 25, 26, 27, 28, 13, 14, 15, 16, 29, 30, 31, 32,
121:
122:                    33, 34, 35, 36, 49, 50, 51, 52, 37, 38, 39, 40, 53, 54, 55,
123:                    56, 41, 42, 43, 44, 57, 58, 59, 60, 45, 46, 47, 48, 61, 62,
124:                    63, 64 };
125:
126:            private static final byte[] ITOA64 = { /* 0..63 => ascii-64 */
127:            (byte) '.', (byte) '/', (byte) '0', (byte) '1', (byte) '2',
128:                    (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
129:                    (byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C',
130:                    (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
131:                    (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M',
132:                    (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R',
133:                    (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W',
134:                    (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b',
135:                    (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g',
136:                    (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l',
137:                    (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q',
138:                    (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
139:                    (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z' };
140:
141:            /* =====  Tables that are initialized at run time  ==================== */
142:
143:            private static byte[] A64TOI = new byte[128]; /* ascii-64 => 0..63 */
144:
145:            /* Initial key schedule permutation */
146:            private static long[][] PC1ROT = new long[16][16];
147:
148:            /* Subsequent key schedule rotation permutations */
149:            private static long[][][] PC2ROT = new long[2][16][16];
150:
151:            /* Initial permutation/expansion table */
152:            private static long[][] IE3264 = new long[8][16];
153:
154:            /* Table that combines the S, P, and E operations.  */
155:            private static long[][] SPE = new long[8][64];
156:
157:            /* compressed/interleaved => final permutation table */
158:            private static long[][] CF6464 = new long[16][16];
159:
160:            /* ==================================== */
161:
162:            static {
163:                byte[] perm = new byte[64];
164:                byte[] temp = new byte[64];
165:
166:                // inverse table.
167:                for (int i = 0; i < 64; i++)
168:                    A64TOI[ITOA64[i]] = (byte) i;
169:
170:                // PC1ROT - bit reverse, then PC1, then Rotate, then PC2
171:                for (int i = 0; i < 64; i++)
172:                    perm[i] = (byte) 0;
173:                ;
174:                for (int i = 0; i < 64; i++) {
175:                    int k;
176:                    if ((k = (int) PC2[i]) == 0)
177:                        continue;
178:                    k += Rotates[0] - 1;
179:                    if ((k % 28) < Rotates[0])
180:                        k -= 28;
181:                    k = (int) PC1[k];
182:                    if (k > 0) {
183:                        k--;
184:                        k = (k | 0x07) - (k & 0x07);
185:                        k++;
186:                    }
187:                    perm[i] = (byte) k;
188:                }
189:                init_perm(PC1ROT, perm, 8);
190:
191:                // PC2ROT - PC2 inverse, then Rotate, then PC2
192:                for (int j = 0; j < 2; j++) {
193:                    int k;
194:                    for (int i = 0; i < 64; i++)
195:                        perm[i] = temp[i] = 0;
196:                    for (int i = 0; i < 64; i++) {
197:                        if ((k = (int) PC2[i]) == 0)
198:                            continue;
199:                        temp[k - 1] = (byte) (i + 1);
200:                    }
201:                    for (int i = 0; i < 64; i++) {
202:                        if ((k = (int) PC2[i]) == 0)
203:                            continue;
204:                        k += j;
205:                        if ((k % 28) <= j)
206:                            k -= 28;
207:                        perm[i] = temp[k];
208:                    }
209:
210:                    init_perm(PC2ROT[j], perm, 8);
211:                }
212:
213:                // Bit reverse, intial permupation, expantion
214:                for (int i = 0; i < 8; i++) {
215:                    for (int j = 0; j < 8; j++) {
216:                        int k = (j < 2) ? 0 : IP[ExpandTr[i * 6 + j - 2] - 1];
217:                        if (k > 32)
218:                            k -= 32;
219:                        else if (k > 0)
220:                            k--;
221:                        if (k > 0) {
222:                            k--;
223:                            k = (k | 0x07) - (k & 0x07);
224:                            k++;
225:                        }
226:                        perm[i * 8 + j] = (byte) k;
227:                    }
228:                }
229:
230:                init_perm(IE3264, perm, 8);
231:
232:                // Compression, final permutation, bit reverse
233:                for (int i = 0; i < 64; i++) {
234:                    int k = IP[CIFP[i] - 1];
235:                    if (k > 0) {
236:                        k--;
237:                        k = (k | 0x07) - (k & 0x07);
238:                        k++;
239:                    }
240:                    perm[k - 1] = (byte) (i + 1);
241:                }
242:
243:                init_perm(CF6464, perm, 8);
244:
245:                // SPE table
246:                for (int i = 0; i < 48; i++)
247:                    perm[i] = P32Tr[ExpandTr[i] - 1];
248:                for (int t = 0; t < 8; t++) {
249:                    for (int j = 0; j < 64; j++) {
250:                        int k = (((j >> 0) & 0x01) << 5)
251:                                | (((j >> 1) & 0x01) << 3)
252:                                | (((j >> 2) & 0x01) << 2)
253:                                | (((j >> 3) & 0x01) << 1)
254:                                | (((j >> 4) & 0x01) << 0)
255:                                | (((j >> 5) & 0x01) << 4);
256:                        k = S[t][k];
257:                        k = (((k >> 3) & 0x01) << 0) | (((k >> 2) & 0x01) << 1)
258:                                | (((k >> 1) & 0x01) << 2)
259:                                | (((k >> 0) & 0x01) << 3);
260:                        for (int i = 0; i < 32; i++)
261:                            temp[i] = 0;
262:                        for (int i = 0; i < 4; i++)
263:                            temp[4 * t + i] = (byte) ((k >> i) & 0x01);
264:                        long kk = 0;
265:                        for (int i = 24; --i >= 0;)
266:                            kk = ((kk << 1) | ((long) temp[perm[i] - 1]) << 32 | ((long) temp[perm[i + 24] - 1]));
267:
268:                        SPE[t][j] = to_six_bit(kk);
269:                    }
270:                }
271:            }
272:
273:            /**
274:             * You can't call the constructer.
275:             */
276:            private UnixCrypt() {
277:            }
278:
279:            /**
280:             * Returns the transposed and split code of a 24-bit code
281:             * into a 4-byte code, each having 6 bits.
282:             */
283:            private static int to_six_bit(int num) {
284:                return (((num << 26) & 0xfc000000) | ((num << 12) & 0xfc0000)
285:                        | ((num >> 2) & 0xfc00) | ((num >> 16) & 0xfc));
286:            }
287:
288:            /**
289:             * Returns the transposed and split code of two 24-bit code 
290:             * into two 4-byte code, each having 6 bits.
291:             */
292:            private static long to_six_bit(long num) {
293:                return (((num << 26) & 0xfc000000fc000000L)
294:                        | ((num << 12) & 0xfc000000fc0000L)
295:                        | ((num >> 2) & 0xfc000000fc00L) | ((num >> 16) & 0xfc000000fcL));
296:            }
297:
298:            /**
299:             * Returns the permutation of the given 64-bit code with
300:             * the specified permutataion table.
301:             */
302:            private static long perm6464(long c, long[][] p) {
303:                long out = 0L;
304:                for (int i = 8; --i >= 0;) {
305:                    int t = (int) (0x00ff & c);
306:                    c >>= 8;
307:                    long tp = p[i << 1][t & 0x0f];
308:                    out |= tp;
309:                    tp = p[(i << 1) + 1][t >> 4];
310:                    out |= tp;
311:                }
312:                return out;
313:            }
314:
315:            /**
316:             * Returns the permutation of the given 32-bit code with
317:             * the specified permutataion table.
318:             */
319:            private static long perm3264(int c, long[][] p) {
320:                long out = 0L;
321:                for (int i = 4; --i >= 0;) {
322:                    int t = (0x00ff & c);
323:                    c >>= 8;
324:                    long tp = p[i << 1][t & 0x0f];
325:                    out |= tp;
326:                    tp = p[(i << 1) + 1][t >> 4];
327:                    out |= tp;
328:                }
329:                return out;
330:            }
331:
332:            /**
333:             * Returns the key schedule for the given key.
334:             */
335:            private static long[] des_setkey(long keyword) {
336:                long K = perm6464(keyword, PC1ROT);
337:                long[] KS = new long[16];
338:                KS[0] = K & ~0x0303030300000000L;
339:
340:                for (int i = 1; i < 16; i++) {
341:                    KS[i] = K;
342:                    K = perm6464(K, PC2ROT[Rotates[i] - 1]);
343:
344:                    KS[i] = K & ~0x0303030300000000L;
345:                }
346:                return KS;
347:            }
348:
349:            /**
350:             * Returns the DES encrypted code of the given word with the specified 
351:             * environment.
352:             */
353:            private static long des_cipher(long in, int salt, int num_iter,
354:                    long[] KS) {
355:                salt = to_six_bit(salt);
356:                long L = in;
357:                long R = L;
358:                L &= 0x5555555555555555L;
359:                R = (R & 0xaaaaaaaa00000000L)
360:                        | ((R >> 1) & 0x0000000055555555L);
361:                L = ((((L << 1) | (L << 32)) & 0xffffffff00000000L) | ((R | (R >> 32)) & 0x00000000ffffffffL));
362:
363:                L = perm3264((int) (L >> 32), IE3264);
364:                R = perm3264((int) (L & 0xffffffff), IE3264);
365:
366:                while (--num_iter >= 0) {
367:                    for (int loop_count = 0; loop_count < 8; loop_count++) {
368:                        long kp;
369:                        long B;
370:                        long k;
371:
372:                        kp = KS[(loop_count << 1)];
373:                        k = ((R >> 32) ^ R) & salt & 0xffffffffL;
374:                        k |= (k << 32);
375:                        B = (k ^ R ^ kp);
376:
377:                        L ^= (SPE[0][(int) ((B >> 58) & 0x3f)]
378:                                ^ SPE[1][(int) ((B >> 50) & 0x3f)]
379:                                ^ SPE[2][(int) ((B >> 42) & 0x3f)]
380:                                ^ SPE[3][(int) ((B >> 34) & 0x3f)]
381:                                ^ SPE[4][(int) ((B >> 26) & 0x3f)]
382:                                ^ SPE[5][(int) ((B >> 18) & 0x3f)]
383:                                ^ SPE[6][(int) ((B >> 10) & 0x3f)] ^ SPE[7][(int) ((B >> 2) & 0x3f)]);
384:
385:                        kp = KS[(loop_count << 1) + 1];
386:                        k = ((L >> 32) ^ L) & salt & 0xffffffffL;
387:                        k |= (k << 32);
388:                        B = (k ^ L ^ kp);
389:
390:                        R ^= (SPE[0][(int) ((B >> 58) & 0x3f)]
391:                                ^ SPE[1][(int) ((B >> 50) & 0x3f)]
392:                                ^ SPE[2][(int) ((B >> 42) & 0x3f)]
393:                                ^ SPE[3][(int) ((B >> 34) & 0x3f)]
394:                                ^ SPE[4][(int) ((B >> 26) & 0x3f)]
395:                                ^ SPE[5][(int) ((B >> 18) & 0x3f)]
396:                                ^ SPE[6][(int) ((B >> 10) & 0x3f)] ^ SPE[7][(int) ((B >> 2) & 0x3f)]);
397:                    }
398:                    // swap L and R
399:                    L ^= R;
400:                    R ^= L;
401:                    L ^= R;
402:                }
403:                L = ((((L >> 35) & 0x0f0f0f0fL) | (((L & 0xffffffff) << 1) & 0xf0f0f0f0L)) << 32 | (((R >> 35) & 0x0f0f0f0fL) | (((R & 0xffffffff) << 1) & 0xf0f0f0f0L)));
404:
405:                L = perm6464(L, CF6464);
406:
407:                return L;
408:            }
409:
410:            /**
411:             * Initializes the given permutation table with the mapping table.
412:             */
413:            private static void init_perm(long[][] perm, byte[] p, int chars_out) {
414:                for (int k = 0; k < chars_out * 8; k++) {
415:
416:                    int l = p[k] - 1;
417:                    if (l < 0)
418:                        continue;
419:                    int i = l >> 2;
420:                    l = 1 << (l & 0x03);
421:                    for (int j = 0; j < 16; j++) {
422:                        int s = ((k & 0x07) + ((7 - (k >> 3)) << 3));
423:                        if ((j & l) != 0x00)
424:                            perm[i][j] |= (1L << s);
425:                    }
426:                }
427:            }
428:
429:            /**
430:             * Encrypts String into crypt (Unix) code.
431:             * @param key the key to be encrypted
432:             * @param setting the salt to be used
433:             * @return the encrypted String
434:             */
435:            public static String crypt(String key, String setting) {
436:                long constdatablock = 0L; /* encryption constant */
437:                byte[] cryptresult = new byte[13]; /* encrypted result */
438:                long keyword = 0L;
439:                /* invalid parameters! */
440:                if (key == null || setting == null)
441:                    return "*"; // will NOT match under ANY circumstances!
442:
443:                int keylen = key.length();
444:
445:                for (int i = 0; i < 8; i++) {
446:                    keyword = (keyword << 8)
447:                            | ((i < keylen) ? 2 * key.charAt(i) : 0);
448:                }
449:
450:                long[] KS = des_setkey(keyword);
451:
452:                int salt = 0;
453:                for (int i = 2; --i >= 0;) {
454:                    char c = (i < setting.length()) ? setting.charAt(i) : '.';
455:                    cryptresult[i] = (byte) c;
456:                    salt = (salt << 6) | (0x00ff & A64TOI[c]);
457:                }
458:
459:                long rsltblock = des_cipher(constdatablock, salt, 25, KS);
460:
461:                cryptresult[12] = ITOA64[(((int) rsltblock) << 2) & 0x3f];
462:                rsltblock >>= 4;
463:                for (int i = 12; --i >= 2;) {
464:                    cryptresult[i] = ITOA64[((int) rsltblock) & 0x3f];
465:                    rsltblock >>= 6;
466:                }
467:
468:                return new String(cryptresult, 0x00, 0, 13);
469:            }
470:
471:            public static void main(String[] arg) {
472:                if (arg.length != 2) {
473:                    System.err
474:                            .println("Usage - java org.mortbay.util.UnixCrypt <key> <salt>");
475:                    System.exit(1);
476:                }
477:
478:                System.err.println("Crypt=" + crypt(arg[0], arg[1]));
479:            }
480:
481:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.