Source Code Cross Referenced for Crypt.java in  » 6.0-JDK-Modules-com.sun » security » com » sun » security » auth » module » 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 » 6.0 JDK Modules com.sun » security » com.sun.security.auth.module 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Portions Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:
026:        /*	Copyright (c) 1988 AT&T	*/
027:        /*	  All Rights Reserved  	*/
028:
029:        /**
030:         * Implements the UNIX crypt(3) function, based on a direct port of the
031:         * libc crypt function.
032:         *
033:         * <p>
034:         * From the crypt man page:
035:         * <p>
036:         * crypt() is the password encryption routine, based on the NBS
037:         * Data  Encryption  Standard,  with variations intended (among
038:         * other things) to frustrate use of  hardware  implementations
039:         * of the DES for key search.
040:         * <p>
041:         * The first argument to crypt() is  normally  a  user's  typed
042:         * password.   The  second  is a 2-character string chosen from
043:         * the set [a-zA-Z0-9./].  the  salt string is used to perturb 
044:         * the DES algorithm in one
045:         * of 4096 different ways, after which the password is used  as
046:         * the  key  to  encrypt  repeatedly  a  constant  string.  The
047:         * returned value points to the encrypted password, in the same
048:         * alphabet as the salt.  The first two characters are the salt
049:         * itself.
050:         * 
051:         * @version 1.5, 01/11/00
052:         * @author Roland Schemers
053:         */package com.sun.security.auth.module;
054:
055:        class Crypt {
056:
057:            /* EXPORT DELETE START */
058:
059:            private static final byte[] IP = { 58, 50, 42, 34, 26, 18, 10, 2,
060:                    60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14,
061:                    6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17,
062:                    9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29,
063:                    21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7, };
064:
065:            private static final byte[] FP = { 40, 8, 48, 16, 56, 24, 64, 32,
066:                    39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62,
067:                    30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20,
068:                    60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50,
069:                    18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25, };
070:
071:            private static final byte[] PC1_C = { 57, 49, 41, 33, 25, 17, 9, 1,
072:                    58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11,
073:                    3, 60, 52, 44, 36, };
074:
075:            private static final byte[] PC1_D = { 63, 55, 47, 39, 31, 23, 15,
076:                    7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,
077:                    13, 5, 28, 20, 12, 4, };
078:
079:            private static final byte[] shifts = { 1, 1, 2, 2, 2, 2, 2, 2, 1,
080:                    2, 2, 2, 2, 2, 2, 1, };
081:
082:            private static final byte[] PC2_C = { 14, 17, 11, 24, 1, 5, 3, 28,
083:                    15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, };
084:
085:            private static final byte[] PC2_D = { 41, 52, 31, 37, 47, 55, 30,
086:                    40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36,
087:                    29, 32, };
088:
089:            private byte[] C = new byte[28];
090:            private byte[] D = new byte[28];
091:
092:            private byte[] KS;
093:
094:            private byte[] E = new byte[48];
095:
096:            private static final byte[] e2 = { 32, 1, 2, 3, 4, 5, 4, 5, 6, 7,
097:                    8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17,
098:                    18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28,
099:                    29, 28, 29, 30, 31, 32, 1, };
100:
101:            private void setkey(byte[] key) {
102:                int i, j, k;
103:                byte t;
104:
105:                if (KS == null) {
106:                    KS = new byte[16 * 48];
107:                }
108:
109:                for (i = 0; i < 28; i++) {
110:                    C[i] = key[PC1_C[i] - 1];
111:                    D[i] = key[PC1_D[i] - 1];
112:                }
113:                for (i = 0; i < 16; i++) {
114:                    for (k = 0; k < shifts[i]; k++) {
115:                        t = C[0];
116:                        for (j = 0; j < 28 - 1; j++)
117:                            C[j] = C[j + 1];
118:                        C[27] = t;
119:                        t = D[0];
120:                        for (j = 0; j < 28 - 1; j++)
121:                            D[j] = D[j + 1];
122:                        D[27] = t;
123:                    }
124:                    for (j = 0; j < 24; j++) {
125:                        int index = i * 48;
126:
127:                        KS[index + j] = C[PC2_C[j] - 1];
128:                        KS[index + j + 24] = D[PC2_D[j] - 28 - 1];
129:                    }
130:                }
131:                for (i = 0; i < 48; i++)
132:                    E[i] = e2[i];
133:            }
134:
135:            private static final byte[][] S = {
136:                    { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0,
137:                            15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
138:                            4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5,
139:                            0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0,
140:                            6, 13 },
141:
142:                    { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3,
143:                            13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
144:                            0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2,
145:                            15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5,
146:                            14, 9 },
147:
148:                    { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13,
149:                            7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
150:                            13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14,
151:                            7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5,
152:                            2, 12 },
153:
154:                    { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13,
155:                            8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
156:                            10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8,
157:                            4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7,
158:                            2, 14 },
159:
160:                    { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14,
161:                            11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
162:                            4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0,
163:                            14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4,
164:                            5, 3 },
165:
166:                    { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10,
167:                            15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
168:                            9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11,
169:                            6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0,
170:                            8, 13 },
171:
172:                    { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13,
173:                            0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
174:                            1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9,
175:                            2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2,
176:                            3, 12 },
177:
178:                    { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1,
179:                            15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
180:                            7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5,
181:                            8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5,
182:                            6, 11 }, };
183:
184:            private static final byte[] P = { 16, 7, 20, 21, 29, 12, 28, 17, 1,
185:                    15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19,
186:                    13, 30, 6, 22, 11, 4, 25, };
187:
188:            private byte[] L = new byte[64];
189:            private byte[] tempL = new byte[32];
190:            private byte[] f = new byte[32];
191:            private byte[] preS = new byte[48];
192:
193:            private void encrypt(byte[] block, int fake) {
194:                int i;
195:                int t, j, k;
196:                int R = 32; // &L[32]
197:
198:                if (KS == null) {
199:                    KS = new byte[16 * 48];
200:                }
201:
202:                for (j = 0; j < 64; j++) {
203:                    L[j] = block[IP[j] - 1];
204:                }
205:                for (i = 0; i < 16; i++) {
206:                    int index = i * 48;
207:
208:                    for (j = 0; j < 32; j++) {
209:                        tempL[j] = L[R + j];
210:                    }
211:                    for (j = 0; j < 48; j++) {
212:                        preS[j] = (byte) (L[R + E[j] - 1] ^ KS[index + j]);
213:                    }
214:                    for (j = 0; j < 8; j++) {
215:                        t = 6 * j;
216:                        k = S[j][(preS[t + 0] << 5) + (preS[t + 1] << 3)
217:                                + (preS[t + 2] << 2) + (preS[t + 3] << 1)
218:                                + (preS[t + 4] << 0) + (preS[t + 5] << 4)];
219:                        t = 4 * j;
220:                        f[t + 0] = (byte) ((k >> 3) & 01);
221:                        f[t + 1] = (byte) ((k >> 2) & 01);
222:                        f[t + 2] = (byte) ((k >> 1) & 01);
223:                        f[t + 3] = (byte) ((k >> 0) & 01);
224:                    }
225:                    for (j = 0; j < 32; j++) {
226:                        L[R + j] = (byte) (L[j] ^ f[P[j] - 1]);
227:                    }
228:                    for (j = 0; j < 32; j++) {
229:                        L[j] = tempL[j];
230:                    }
231:                }
232:                for (j = 0; j < 32; j++) {
233:                    t = L[j];
234:                    L[j] = L[R + j];
235:                    L[R + j] = (byte) t;
236:                }
237:                for (j = 0; j < 64; j++) {
238:                    block[j] = L[FP[j] - 1];
239:                }
240:            }
241:
242:            /* EXPORT DELETE END */
243:
244:            /**
245:             * Creates a new Crypt object for use with the crypt method.
246:             *
247:             */
248:
249:            public Crypt() {
250:                // does nothing at this time
251:                super ();
252:            }
253:
254:            /**
255:             * Implements the libc crypt(3) function. 
256:             *
257:             * @param pw the password to "encrypt".
258:             *
259:             * @param salt the salt to use.
260:             *
261:             * @return A new byte[13] array that contains the encrypted
262:             * password. The first two characters are the salt.
263:             *
264:             */
265:
266:            public synchronized byte[] crypt(byte[] pw, byte[] salt) {
267:                int c, i, j, pwi;
268:                byte temp;
269:                byte[] block = new byte[66];
270:                byte[] iobuf = new byte[13];
271:
272:                /* EXPORT DELETE START */
273:
274:                pwi = 0;
275:
276:                for (i = 0; pwi < pw.length && i < 64; pwi++) {
277:                    c = pw[pwi];
278:                    for (j = 0; j < 7; j++, i++) {
279:                        block[i] = (byte) ((c >> (6 - j)) & 01);
280:                    }
281:                    i++;
282:                }
283:
284:                setkey(block);
285:
286:                for (i = 0; i < 66; i++) {
287:                    block[i] = 0;
288:                }
289:
290:                for (i = 0; i < 2; i++) {
291:                    c = salt[i];
292:                    iobuf[i] = (byte) c;
293:                    if (c > 'Z')
294:                        c -= 6;
295:                    if (c > '9')
296:                        c -= 7;
297:                    c -= '.';
298:                    for (j = 0; j < 6; j++) {
299:                        if (((c >> j) & 01) != 0) {
300:                            temp = E[6 * i + j];
301:                            E[6 * i + j] = E[6 * i + j + 24];
302:                            E[6 * i + j + 24] = temp;
303:                        }
304:                    }
305:                }
306:
307:                for (i = 0; i < 25; i++) {
308:                    encrypt(block, 0);
309:                }
310:
311:                for (i = 0; i < 11; i++) {
312:                    c = 0;
313:                    for (j = 0; j < 6; j++) {
314:                        c <<= 1;
315:                        c |= block[6 * i + j];
316:                    }
317:                    c += '.';
318:                    if (c > '9') {
319:                        c += 7;
320:                    }
321:                    if (c > 'Z') {
322:                        c += 6;
323:                    }
324:                    iobuf[i + 2] = (byte) c;
325:                }
326:                //iobuf[i+2] = 0;
327:                if (iobuf[1] == 0) {
328:                    iobuf[1] = iobuf[0];
329:                }
330:                /* EXPORT DELETE END */
331:                return (iobuf);
332:            }
333:
334:            /**
335:             * program to test the crypt routine.
336:             *
337:             * The first parameter is the cleartext password, the second is
338:             * the salt to use. The salt should be two characters from the 
339:             * set [a-zA-Z0-9./]. Outputs the crypt result.
340:             *
341:             * @param arg command line arguments.
342:             *
343:             */
344:
345:            public static void main(String arg[]) {
346:
347:                if (arg.length != 2) {
348:                    System.err.println("usage: Crypt password salt");
349:                    System.exit(1);
350:                }
351:
352:                Crypt c = new Crypt();
353:                try {
354:                    byte result[] = c.crypt(arg[0].getBytes("ISO-8859-1"),
355:                            arg[1].getBytes("ISO-8859-1"));
356:                    for (int i = 0; i < result.length; i++) {
357:                        System.out.println(" " + i + " " + (char) result[i]);
358:                    }
359:                } catch (java.io.UnsupportedEncodingException uee) {
360:                    // cannot happen
361:                }
362:            }
363:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.