View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.preferences.util;
25  
26  import com.mindtree.techworks.insight.InsightConstants;
27  import com.mindtree.techworks.insight.gui.widgets.StatusBar;
28  /**
29   * The Crypt class helps in encrypting/decrypting a string and converts the resultant string as a hex string.
30   * @author m1003308
31   */
32  public class Crypt {
33  	
34  	private static byte[] key = {(byte)0x01,(byte)0xE3,(byte)0xA2,(byte)0x19,(byte)0x59,(byte)0xBD,
35  			(byte)0xEE,(byte)0xAB};
36  	private static String algorithm = "DES";
37  	private static javax.crypto.spec.SecretKeySpec keySpec = new javax.crypto.spec.SecretKeySpec(key,
38  			algorithm);
39  	
40  	/** Encrypts the given String to an array of bytes */
41  	private static byte[] encryptString(String text) {
42  		try {
43  			javax.crypto.Cipher cipher =
44  				javax.crypto.Cipher.getInstance(algorithm);
45  			cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, keySpec);
46  			return cipher.doFinal(text.getBytes());
47  		} catch (Exception e) {
48  			return null;
49  		}
50  	}
51  	
52  	/** Decrypts the given array of bytes to a String */
53  	private static String decryptString(byte[] b) {
54  		try {
55  			javax.crypto.Cipher cipher =
56  				javax.crypto.Cipher.getInstance(algorithm);
57  			cipher.init(javax.crypto.Cipher.DECRYPT_MODE, keySpec);
58  			return new String(cipher.doFinal(b));
59  		} catch (Exception e) {
60  			return null;
61  		}
62  	}
63  	
64  	/** Encrypts the given String to a hex representation of the array
65  	 of bytes */
66  	public static String encryptHexString(String text) {
67  		return toHex(encryptString(text));
68  	}
69  	
70  	/** Decrypts the given hex representation of the array of bytes to
71  	 a String */
72  	public static String decryptHexString(String text) {
73  		return decryptString(toByteArray(text));
74  	}
75  	
76  	/** Converts the given array of bytes to a hex String */
77  	private static String toHex(byte[] buf) {
78  		String res = "";
79  		for (int i=0; i<buf.length; i++) {
80  			int b = buf[i];
81  			if (b<0) {
82  				res = res.concat("-");
83  				b = -b;
84  			}
85  			if (b<16) {
86  				res = res.concat("0");
87  			}
88  			res = res.concat(Integer.toHexString(b).toUpperCase());
89  		}
90  		return res;
91  	}
92  	
93  	/** Converts the given hex String to an array of bytes */
94  	private static byte[] toByteArray(String hex) {
95  		java.util.Vector res = new java.util.Vector();
96  		String part;
97  		int pos = 0;
98  		int len = 0;
99  		try{
100 			while (pos<hex.length()) {
101 				len = ((hex.substring(pos,pos+1).equals("-")) ? 3 : 2);
102 				part = hex.substring(pos,pos+len);
103 				pos += len;
104 				try{
105 					int test = Integer.parseInt(part,16);
106 					res.add(new Byte((byte)test));
107 				}
108 				catch(NumberFormatException ne){
109 					StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("PASSWORD_DECRYTPTION_FAILED"), false);
110 					return null;
111 				}
112 				
113 			}
114 		}
115 		catch(StringIndexOutOfBoundsException se){
116 			StatusBar.getInstance().setDisplayText(0,InsightConstants.getLiteral("PASSWORD_DECRYTPTION_FAILED"), false);
117 			return null;
118 		}
119 		if (res.size()>0) {
120 			byte[] b = new byte[res.size()];
121 			for (int i=0; i<res.size(); i++) {
122 				Byte a = (Byte)res.elementAt(i);
123 				b[i] = a.byteValue();
124 			}
125 			return b;
126 		} else {
127 			return null;
128 		}
129 	}
130 }