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 salomeTMF_plug.bugzilla.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: public 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 readkey(java.io.File keyFile) throws Exception {
081: //byte[] rawKey = null;
082: java.net.URL url_key = keyFile.toURL();
083: java.io.DataInputStream in = new java.io.DataInputStream(
084: url_key.openStream());
085: java.io.ByteArrayOutputStream bytekey = new java.io.ByteArrayOutputStream();
086: byte[] buf = new byte[1024];
087: int off = 0;
088: int len = 0;
089: while (len != -1) {
090: len = in.read(buf, 0, 1024);
091: if (len != -1) {
092: off += len;
093: bytekey.write(buf, 0, len);
094: }
095: }
096: in.close();
097: key = new SecretKeySpec(bytekey.toByteArray(), encoder);
098: bytekey.close();
099: }
100:
101: public static void writekey(String paht_base) throws Exception {
102: java.io.File keyFile = new java.io.File(paht_base
103: + java.io.File.separator + "key.txt");
104: java.io.DataOutputStream out = new java.io.DataOutputStream(
105: new java.io.FileOutputStream(keyFile));
106:
107: javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator
108: .getInstance(encoder, provider);
109: javax.crypto.SecretKey skey = kgen.generateKey();
110: byte[] raw = skey.getEncoded();
111: DESKeySpec desKey = new DESKeySpec(raw);
112: raw = desKey.getKey();
113: key = new SecretKeySpec(raw, encoder);
114:
115: out.write(raw, 0, raw.length);
116: out.close();
117:
118: }
119:
120: public static String getEncodedPassword(String clearTextPassword)
121: throws NoSuchAlgorithmException {
122: //return encrypString(clearTextPassword);
123:
124: clearTextPassword = clearTextPassword.trim();
125: MessageDigest md = MessageDigest.getInstance("MD5");
126: md.update(clearTextPassword.getBytes());
127: return toHexString(md.digest());
128:
129: }
130:
131: private static char[] hexChar = { '0', '1', '2', '3', '4', '5',
132: '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
133:
134: private static int charToNibble(char c) {
135: if ('0' <= c && c <= '9') {
136: return c - '0';
137: } else if ('a' <= c && c <= 'f') {
138: return c - 'a' + 0xa;
139: } else if ('A' <= c && c <= 'F') {
140: return c - 'A' + 0xa;
141: } else {
142: throw new IllegalArgumentException(
143: "Invalid hex character: " + c);
144: }
145: }
146:
147: private static String toHexString(byte[] b) {
148: StringBuffer sb = new StringBuffer(b.length * 2);
149: for (int i = 0; i < b.length; i++) {
150: // look up high nibble char
151: sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
152:
153: // look up low nibble char
154: sb.append(hexChar[b[i] & 0x0f]);
155: }
156: return sb.toString();
157: }
158:
159: private static byte[] toBytesArray(String s) {
160: int stringLength = s.length();
161: if ((stringLength & 0x1) != 0) {
162: throw new IllegalArgumentException(
163: "fromHexString requires an even number of hex characters");
164: }
165: byte[] b = new byte[stringLength / 2];
166: for (int i = 0, j = 0; i < stringLength; i += 2, j++) {
167: int high = charToNibble(s.charAt(i));
168: int low = charToNibble(s.charAt(i + 1));
169: b[j] = (byte) ((high << 4) | low);
170: }
171: return b;
172: }
173:
174: /*
175: private static String toHexString(byte bytes[]) {
176: StringBuffer retString = new StringBuffer();
177: for (int i = 0; i < bytes.length; ++i){
178: retString.append(
179: Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1));
180: }
181: return retString.toString();
182: }
183:
184: private static byte[] toBytesArray(String hexString){
185: //Convert hex string back to byte
186: byte[] hexByte = new BigInteger(hexString,16).toByteArray();
187: return hexByte;
188: }
189: */
190: public static boolean testPassword(String clearTextTestPassword,
191: String encodedActualPassword)
192: throws NoSuchAlgorithmException {
193: if (clearTextTestPassword.equals(encodedActualPassword))
194: return true;
195: /*String clearTextTestPassword2 = decryptString(encodedActualPassword);
196: System.out.println("MD5 compare : "+ clearTextTestPassword2 + " and " + clearTextTestPassword);
197: return (clearTextTestPassword2.equals(clearTextTestPassword));
198: */
199:
200: String encodedTestPassword = MD5paswd
201: .getEncodedPassword(clearTextTestPassword);
202: //System.out.println("MD5 compare : "+ encodedTestPassword + " and2 " + encodedActualPassword);
203: return (encodedTestPassword.equals(encodedActualPassword));
204:
205: }
206:
207: public static String decryptString(String hexString) {
208: byte[] data = toBytesArray(hexString);
209: return decryptData(data);
210: }
211:
212: public static String encrypString(String data) {
213: byte[] sdata = encryptData(data);
214: return toHexString(sdata);
215: }
216:
217: /*static private boolean testKeyWith(String cleardata){
218: try {
219: String codedata = encrypString(cleardata);
220: if (cleardata.equals(decryptString(codedata)))
221: return true;
222: }catch(Exception e){
223: return false;
224: }
225: return true;
226: }*/
227:
228: static private String decryptData(byte[] data) {
229: try {
230: //cipher.init(Cipher.DECRYPT_MODE, key, salt);
231: cipher.init(Cipher.DECRYPT_MODE, key);
232: byte[] original = cipher.doFinal(data);
233: //System.out.println("Decrypted data: " + new String(original));
234: return new String(original);
235: } catch (InvalidKeyException e) {
236: e.printStackTrace();
237: } catch (IllegalStateException e) {
238: e.printStackTrace();
239: } catch (Exception e) {
240: e.printStackTrace();
241: }
242: return null;
243: }
244:
245: static private byte[] encryptData(String sData) {
246: try {
247: byte[] data = sData.getBytes();
248: //cipher.init(Cipher.ENCRYPT_MODE, key, salt);
249: cipher.init(Cipher.ENCRYPT_MODE, key);
250: byte[] result = cipher.doFinal(data);
251: return result;
252: } catch (InvalidKeyException e) {
253: e.printStackTrace();
254: } catch (IllegalStateException e) {
255: e.printStackTrace();
256: } catch (Exception e) {
257: e.printStackTrace();
258: }
259: return null;
260: }
261: }
|