001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.desktop.util;
007:
008: public class Password {
009: private static int DEFAULT_PAD_LENGTH = 15;
010:
011: public static String encode(String plaintext) {
012: char[] sbuf;
013: char[] nbuf;
014: char[] vbuf;
015: boolean firstFound = false;
016: int i;
017: int j;
018: int vj;
019: int a;
020: int numChars;
021: String inter;
022:
023: int lenn = plaintext.length();
024: sbuf = plaintext.toCharArray();
025: nbuf = new char[lenn];
026: for (i = 0; i < lenn; i++) {
027: a = Character.getNumericValue(sbuf[i]);
028: if ((a >= 10) && (a <= 22)) {
029: nbuf[i] = (char) (sbuf[i] + 13);
030: } else if ((a >= 23) && (a <= 35)) {
031: nbuf[i] = (char) (sbuf[i] - 13);
032: } else {
033: if ((sbuf[i] >= 91 && sbuf[i] <= 96)
034: || (sbuf[i] >= 123 && sbuf[i] <= 127)) {
035: nbuf[i] = (char) (sbuf[i] - 32);
036: } else {
037: nbuf[i] = (char) (sbuf[i] - 7);
038: }
039: }
040: }
041:
042: //------------------------------------------------------------------
043: // Now let's add some amount of random characters - take the first
044: // alpha char of the string buffer and use it to determine how many
045: // characters we'll add
046: //------------------------------------------------------------------
047:
048: vbuf = new char[lenn + 26];
049: vj = 0;
050: for (i = 0; i < lenn; i++) {
051: if (!firstFound) {
052: a = Character.getNumericValue(nbuf[i]);
053: if ((a >= 10) && (a <= 35)) {
054: vbuf[vj] = nbuf[i];
055: vj = vj + 1;
056: numChars = calcNum(nbuf[i]);
057: firstFound = true;
058: pad(vj, numChars, vbuf, firstFound);
059: vj += numChars;
060:
061: } else {
062: vbuf[vj] = nbuf[i];
063: vj = vj + 1;
064: }
065: } else {
066: vbuf[vj] = nbuf[i];
067: vj = vj + 1;
068: }
069: }
070: if (!firstFound) {
071: pad(vj, DEFAULT_PAD_LENGTH, vbuf, firstFound);
072: vj += DEFAULT_PAD_LENGTH;
073: }
074: return new String(vbuf, 0, vj);
075: }
076:
077: /**
078: * Decode an encoded password
079: *
080: * @param encodedtext The encoded password to be decoded.
081: * @return The decoded password.
082: **/
083: // the following code is kept for backward compatibility reason
084: public static String decode(String encodedtext) {
085: char[] sbuf;
086: char[] temp;
087: char[] temp2;
088: boolean found = false;
089: int i;
090: int a;
091: int j;
092: int l;
093: int count;
094:
095: int lenn = encodedtext.length();
096: sbuf = encodedtext.toCharArray();
097:
098: // get the ascii value of the 1st alpha char
099:
100: for (i = 0; i < lenn; i++) {
101: a = Character.getNumericValue(sbuf[i]);
102: if ((a >= 10) && (a <= 35)) {
103: found = true;
104: break;
105: }
106: }
107:
108: // check here if no alpha chars were found and exit with
109: // the input string
110:
111: if (!found) {
112: temp2 = new char[lenn];
113: rot13(sbuf, temp2);
114: return new String(temp2, 0, lenn - DEFAULT_PAD_LENGTH);
115: //return encodedtext;
116: }
117:
118: count = sbuf[i] % 20;
119: if (count == 0) {
120: count = 1;
121: }
122:
123: // now add the addfactor to set minmum number of chars
124: count = count + 7;
125:
126: // copy the non-alpha chars and the 1st alpha char
127:
128: temp = new char[lenn];
129: temp2 = new char[lenn];
130: i += 1;
131: for (l = 0; l < i; l++) {
132: temp[l] = sbuf[l];
133: }
134:
135: i += count;
136:
137: while (i < lenn) {
138: temp[l++] = sbuf[i++];
139: }
140: rot13(temp, temp2);
141:
142: return new String(temp2, 0, l);
143: }
144:
145: private static int calcNum(char c) {
146: int x = c % 20;
147: if (x == 0)
148: x++;
149: return (x + 7);
150: }
151:
152: private static void rot13(char[] vbuf, char[] nbuf) {
153: int i;
154: int k;
155:
156: for (i = 0; i < vbuf.length; i++) {
157: int a = Character.getNumericValue(vbuf[i]);
158: if ((a >= 10) && (a <= 22)) {
159: nbuf[i] = (char) (vbuf[i] + 13);
160: } else if ((a >= 23) && (a <= 35)) {
161: nbuf[i] = (char) (vbuf[i] - 13);
162: } else {
163: if ((vbuf[i] >= 59 && vbuf[i] <= 64)
164: || (vbuf[i] >= 91 && vbuf[i] <= 95)) {
165: nbuf[i] = (char) (vbuf[i] + 32);
166: } else {
167: nbuf[i] = (char) (vbuf[i] + 7);
168: }
169: }
170: }
171: }
172:
173: private static void pad(int start, int numChars, char[] vbuf,
174: boolean found) {
175: int ranInt;
176: int rand;
177: int vj = start;
178: for (int j = 1; j <= numChars; j++) {
179: while (true) {
180: if (found) {
181: rand = (int) (Math.floor(Math.random() * 25) + 97);
182: ranInt = rand % 123;
183: if (ranInt < 65) {
184: continue;
185: }
186: if ((ranInt >= 91) && (ranInt <= 96)) {
187: continue;
188: }
189: } else {
190: rand = (int) (Math.floor(Math.random() * 25) + 33);
191: ranInt = rand % 127;
192: if (ranInt < 33) {
193: continue;
194: }
195: if ((ranInt >= 65) && (ranInt <= 90)) {
196: continue;
197: }
198: if ((ranInt >= 97) && (ranInt <= 122)) {
199: continue;
200: }
201: }
202: break;
203: }
204: vbuf[vj] = (char) ranInt;
205: vj = vj + 1;
206: }
207:
208: }
209:
210: }
|