1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
30
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
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
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
65
66 public static String encryptHexString(String text) {
67 return toHex(encryptString(text));
68 }
69
70
71
72 public static String decryptHexString(String text) {
73 return decryptString(toByteArray(text));
74 }
75
76
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
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 }