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 Fayçal SOUGRATI, Vincent Pautret, Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.tools.encrypt_prop;
025:
026: import java.security.InvalidKeyException;
027: import java.security.Key;
028: import java.security.MessageDigest;
029: import java.security.NoSuchAlgorithmException;
030: import java.security.Provider;
031:
032: import javax.crypto.Cipher;
033: import javax.crypto.spec.DESKeySpec; //import javax.crypto.spec.IvParameterSpec;
034: import javax.crypto.spec.SecretKeySpec;
035:
036: /**
037: *
038: * @author marchemi
039: */
040: public class MD5paswd {
041: static Key key;
042: private static String encoder = "DES";
043: private static String cyphencoder = "DES/ECB/PKCS5Padding";
044: /*private static byte[] iv = { (byte) 0xc9, (byte) 0x36, (byte) 0x78, (byte) 0x99,
045: (byte) 0x52, (byte) 0x3e, (byte) 0xea, (byte) 0xf2 };*/
046: //private static IvParameterSpec salt = new IvParameterSpec(iv);
047: private static Provider provider;
048: static Cipher cipher;
049: static {
050: try {
051: provider = new com.sun.crypto.provider.SunJCE();
052: cipher = Cipher
053: .getInstance(cyphencoder, provider.getName());
054: } catch (Exception e) {
055: e.printStackTrace();
056: }
057: }
058:
059: static void readkey(java.net.URL base) throws Exception {
060: //byte[] rawKey = null;
061: java.net.URL url_key = new java.net.URL(base + "/key.txt");
062: java.io.DataInputStream in = new java.io.DataInputStream(
063: url_key.openStream());
064: java.io.ByteArrayOutputStream bytekey = new java.io.ByteArrayOutputStream();
065: byte[] buf = new byte[1024];
066: int off = 0;
067: int len = 0;
068: while (len != -1) {
069: len = in.read(buf, 0, 1024);
070: if (len != -1) {
071: off += len;
072: bytekey.write(buf, 0, len);
073: }
074: }
075: in.close();
076: key = new SecretKeySpec(bytekey.toByteArray(), encoder);
077: bytekey.close();
078: }
079:
080: public static void writekey(String paht_base) throws Exception {
081: java.io.File keyFile = new java.io.File(paht_base
082: + java.io.File.separator + "key.txt");
083: java.io.DataOutputStream out = new java.io.DataOutputStream(
084: new java.io.FileOutputStream(keyFile));
085:
086: javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator
087: .getInstance(encoder, provider);
088: javax.crypto.SecretKey skey = kgen.generateKey();
089: byte[] raw = skey.getEncoded();
090: DESKeySpec desKey = new DESKeySpec(raw);
091: raw = desKey.getKey();
092: key = new SecretKeySpec(raw, encoder);
093:
094: out.write(raw, 0, raw.length);
095: out.close();
096:
097: }
098:
099: public static String getEncodedPassword(String clearTextPassword)
100: throws NoSuchAlgorithmException {
101: //return encrypString(clearTextPassword);
102:
103: clearTextPassword = clearTextPassword.trim();
104: MessageDigest md = MessageDigest.getInstance("MD5");
105: md.update(clearTextPassword.getBytes());
106: return toHexString(md.digest());
107:
108: }
109:
110: private static char[] hexChar = { '0', '1', '2', '3', '4', '5',
111: '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
112:
113: private static int charToNibble(char c) {
114: if ('0' <= c && c <= '9') {
115: return c - '0';
116: } else if ('a' <= c && c <= 'f') {
117: return c - 'a' + 0xa;
118: } else if ('A' <= c && c <= 'F') {
119: return c - 'A' + 0xa;
120: } else {
121: throw new IllegalArgumentException(
122: "Invalid hex character: " + c);
123: }
124: }
125:
126: private static String toHexString(byte[] b) {
127: StringBuffer sb = new StringBuffer(b.length * 2);
128: for (int i = 0; i < b.length; i++) {
129: // look up high nibble char
130: sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
131:
132: // look up low nibble char
133: sb.append(hexChar[b[i] & 0x0f]);
134: }
135: return sb.toString();
136: }
137:
138: private static byte[] toBytesArray(String s) {
139: int stringLength = s.length();
140: if ((stringLength & 0x1) != 0) {
141: throw new IllegalArgumentException(
142: "fromHexString requires an even number of hex characters");
143: }
144: byte[] b = new byte[stringLength / 2];
145: for (int i = 0, j = 0; i < stringLength; i += 2, j++) {
146: int high = charToNibble(s.charAt(i));
147: int low = charToNibble(s.charAt(i + 1));
148: b[j] = (byte) ((high << 4) | low);
149: }
150: return b;
151: }
152:
153: /*
154: private static String toHexString(byte bytes[]) {
155: StringBuffer retString = new StringBuffer();
156: for (int i = 0; i < bytes.length; ++i){
157: retString.append(
158: Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1));
159: }
160: return retString.toString();
161: }
162:
163: private static byte[] toBytesArray(String hexString){
164: //Convert hex string back to byte
165: byte[] hexByte = new BigInteger(hexString,16).toByteArray();
166: return hexByte;
167: }
168: */
169: public static boolean testPassword(String clearTextTestPassword,
170: String encodedActualPassword)
171: throws NoSuchAlgorithmException {
172: if (clearTextTestPassword.equals(encodedActualPassword))
173: return true;
174: /*String clearTextTestPassword2 = decryptString(encodedActualPassword);
175: System.out.println("MD5 compare : "+ clearTextTestPassword2 + " and " + clearTextTestPassword);
176: return (clearTextTestPassword2.equals(clearTextTestPassword));
177: */
178:
179: String encodedTestPassword = MD5paswd
180: .getEncodedPassword(clearTextTestPassword);
181: //System.out.println("MD5 compare : "+ encodedTestPassword + " and2 " + encodedActualPassword);
182: return (encodedTestPassword.equals(encodedActualPassword));
183:
184: }
185:
186: public static String decryptString(String hexString) {
187: byte[] data = toBytesArray(hexString);
188: return decryptData(data);
189: }
190:
191: public static String encrypString(String data) {
192: byte[] sdata = encryptData(data);
193: return toHexString(sdata);
194: }
195:
196: /*static private boolean testKeyWith(String cleardata){
197: try {
198: String codedata = encrypString(cleardata);
199: if (cleardata.equals(decryptString(codedata)))
200: return true;
201: }catch(Exception e){
202: return false;
203: }
204: return true;
205: }*/
206:
207: static private String decryptData(byte[] data) {
208: try {
209: //cipher.init(Cipher.DECRYPT_MODE, key, salt);
210: cipher.init(Cipher.DECRYPT_MODE, key);
211: byte[] original = cipher.doFinal(data);
212: //System.out.println("Decrypted data: " + new String(original));
213: return new String(original);
214: } catch (InvalidKeyException e) {
215: e.printStackTrace();
216: } catch (IllegalStateException e) {
217: e.printStackTrace();
218: } catch (Exception e) {
219: e.printStackTrace();
220: }
221: return null;
222: }
223:
224: static private byte[] encryptData(String sData) {
225: try {
226: byte[] data = sData.getBytes();
227: //cipher.init(Cipher.ENCRYPT_MODE, key, salt);
228: cipher.init(Cipher.ENCRYPT_MODE, key);
229: byte[] result = cipher.doFinal(data);
230: return result;
231: } catch (InvalidKeyException e) {
232: e.printStackTrace();
233: } catch (IllegalStateException e) {
234: e.printStackTrace();
235: } catch (Exception e) {
236: e.printStackTrace();
237: }
238: return null;
239: }
240: }
|