001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Mikael MARCHE, Fayçal Sougrati, Vincent Pautret
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023: package org.objectweb.salome_tmf.api;
024:
025: import java.security.*;
026: import javax.crypto.spec.SecretKeySpec;
027: import javax.crypto.Cipher; //import javax.crypto.spec.IvParameterSpec;
028: import javax.crypto.spec.DESKeySpec;
029: import java.security.Provider;
030:
031: /**
032: *
033: * @author marchemi
034: */
035: public class MD5paswd {
036: static Key key;
037: private static String encoder = "DES";
038: private static String cyphencoder = "DES/ECB/PKCS5Padding";
039: //private static byte[] iv = { (byte) 0xc9, (byte) 0x36, (byte) 0x78, (byte) 0x99, (byte) 0x52, (byte) 0x3e, (byte) 0xea, (byte) 0xf2 };
040: //private static IvParameterSpec salt = new IvParameterSpec(iv);
041: private static Provider provider;
042: static Cipher cipher;
043: static {
044: try {
045: provider = new com.sun.crypto.provider.SunJCE();
046: cipher = Cipher
047: .getInstance(cyphencoder, provider.getName());
048: } catch (Exception e) {
049: e.printStackTrace();
050: }
051: }
052:
053: static void readkey(java.net.URL base) throws Exception {
054: //byte[] rawKey = null;
055: java.net.URL url_key = new java.net.URL(base + "/cfg/key.txt");
056: java.io.DataInputStream in = new java.io.DataInputStream(
057: url_key.openStream());
058: java.io.ByteArrayOutputStream bytekey = new java.io.ByteArrayOutputStream();
059: byte[] buf = new byte[1024];
060: int off = 0;
061: int len = 0;
062: while (len != -1) {
063: len = in.read(buf, 0, 1024);
064: if (len != -1) {
065: off += len;
066: bytekey.write(buf, 0, len);
067: }
068: }
069: in.close();
070: key = new SecretKeySpec(bytekey.toByteArray(), encoder);
071: bytekey.close();
072: }
073:
074: public static void writekey(String paht_base) throws Exception {
075: java.io.File keyFile = new java.io.File(paht_base
076: + java.io.File.separator + "cfg"
077: + java.io.File.separator + "key.txt");
078: java.io.DataOutputStream out = new java.io.DataOutputStream(
079: new java.io.FileOutputStream(keyFile));
080:
081: javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator
082: .getInstance(encoder, provider);
083: javax.crypto.SecretKey skey = kgen.generateKey();
084: byte[] raw = skey.getEncoded();
085: DESKeySpec desKey = new DESKeySpec(raw);
086: raw = desKey.getKey();
087: key = new SecretKeySpec(raw, encoder);
088:
089: out.write(raw, 0, raw.length);
090: out.close();
091:
092: }
093:
094: public static String getEncodedPassword(String clearTextPassword)
095: throws NoSuchAlgorithmException {
096: //return encrypString(clearTextPassword);
097:
098: clearTextPassword = clearTextPassword.trim();
099: MessageDigest md = MessageDigest.getInstance("MD5");
100: md.update(clearTextPassword.getBytes());
101: return toHexString(md.digest());
102:
103: }
104:
105: private static char[] hexChar = { '0', '1', '2', '3', '4', '5',
106: '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
107:
108: private static int charToNibble(char c) {
109: if ('0' <= c && c <= '9') {
110: return c - '0';
111: } else if ('a' <= c && c <= 'f') {
112: return c - 'a' + 0xa;
113: } else if ('A' <= c && c <= 'F') {
114: return c - 'A' + 0xa;
115: } else {
116: throw new IllegalArgumentException(
117: "Invalid hex character: " + c);
118: }
119: }
120:
121: private static String toHexString(byte[] b) {
122: StringBuffer sb = new StringBuffer(b.length * 2);
123: for (int i = 0; i < b.length; i++) {
124: // look up high nibble char
125: sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
126:
127: // look up low nibble char
128: sb.append(hexChar[b[i] & 0x0f]);
129: }
130: return sb.toString();
131: }
132:
133: private static byte[] toBytesArray(String s) {
134: int stringLength = s.length();
135: if ((stringLength & 0x1) != 0) {
136: throw new IllegalArgumentException(
137: "fromHexString requires an even number of hex characters");
138: }
139: byte[] b = new byte[stringLength / 2];
140: for (int i = 0, j = 0; i < stringLength; i += 2, j++) {
141: int high = charToNibble(s.charAt(i));
142: int low = charToNibble(s.charAt(i + 1));
143: b[j] = (byte) ((high << 4) | low);
144: }
145: return b;
146: }
147:
148: /*
149: private static String toHexString(byte bytes[]) {
150: StringBuffer retString = new StringBuffer();
151: for (int i = 0; i < bytes.length; ++i){
152: retString.append(
153: Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1));
154: }
155: return retString.toString();
156: }
157:
158: private static byte[] toBytesArray(String hexString){
159: //Convert hex string back to byte
160: byte[] hexByte = new BigInteger(hexString,16).toByteArray();
161: return hexByte;
162: }
163: */
164: public static boolean testPassword(String clearTextTestPassword,
165: String encodedActualPassword)
166: throws NoSuchAlgorithmException {
167: if (clearTextTestPassword.equals(encodedActualPassword))
168: return true;
169: /*String clearTextTestPassword2 = decryptString(encodedActualPassword);
170: System.out.println("MD5 compare : "+ clearTextTestPassword2 + " and " + clearTextTestPassword);
171: return (clearTextTestPassword2.equals(clearTextTestPassword));
172: */
173:
174: String encodedTestPassword = MD5paswd
175: .getEncodedPassword(clearTextTestPassword);
176: //System.out.println("MD5 compare : "+ encodedTestPassword + " and2 " + encodedActualPassword);
177: return (encodedTestPassword.equals(encodedActualPassword));
178:
179: }
180:
181: public static String decryptString(String hexString) {
182: byte[] data = toBytesArray(hexString);
183: return decryptData(data);
184: }
185:
186: static String encrypString(String data) {
187: byte[] sdata = encryptData(data);
188: return toHexString(sdata);
189: }
190:
191: /*static private boolean testKeyWith(String cleardata){
192: try {
193: String codedata = encrypString(cleardata);
194: if (cleardata.equals(decryptString(codedata)))
195: return true;
196: }catch(Exception e){
197: return false;
198: }
199: return true;
200: }*/
201:
202: static private String decryptData(byte[] data) {
203: try {
204: //cipher.init(Cipher.DECRYPT_MODE, key, salt);
205: cipher.init(Cipher.DECRYPT_MODE, key);
206: byte[] original = cipher.doFinal(data);
207: //System.out.println("Decrypted data: " + new String(original));
208: return new String(original);
209: } catch (InvalidKeyException e) {
210: e.printStackTrace();
211: } catch (IllegalStateException e) {
212: e.printStackTrace();
213: } catch (Exception e) {
214: e.printStackTrace();
215: }
216: return null;
217: }
218:
219: static private byte[] encryptData(String sData) {
220: try {
221: byte[] data = sData.getBytes();
222: //cipher.init(Cipher.ENCRYPT_MODE, key, salt);
223: cipher.init(Cipher.ENCRYPT_MODE, key);
224: byte[] result = cipher.doFinal(data);
225: return result;
226: } catch (InvalidKeyException e) {
227: e.printStackTrace();
228: } catch (IllegalStateException e) {
229: e.printStackTrace();
230: } catch (Exception e) {
231: e.printStackTrace();
232: }
233: return null;
234: }
235: }
|