1 14 package org.compiere.util; 15 16 import java.security.AlgorithmParameters ; 17 import java.security.MessageDigest ; 18 import java.security.NoSuchAlgorithmException ; 19 20 import javax.crypto.Cipher; 21 import javax.crypto.KeyGenerator; 22 import javax.crypto.SecretKey; 23 24 30 public class Secure 31 { 32 33 34 private static Cipher s_cipher = null; 35 private static SecretKey s_key = null; 36 37 38 public static final String CLEARTEXT = "xyz"; 39 40 43 private static void initCipher() 44 { 45 try 46 { 47 s_cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 48 if (false) 50 { 51 KeyGenerator keygen = KeyGenerator.getInstance("DES"); 52 s_key = keygen.generateKey(); 53 byte[] key = s_key.getEncoded(); 54 System.out.print("Key " + s_key.getAlgorithm() + "(" + key.length + ")= "); 55 for (int i = 0; i < key.length; i++) 56 System.out.print(key[i] + ","); 57 System.out.println(""); 58 } 59 else 60 s_key = new javax.crypto.spec.SecretKeySpec(new byte[] {100,25,28,-122,-26,94,-3,-26}, "DES"); 61 } 62 catch (Exception ex) 63 { 64 ex.printStackTrace(); 65 } 66 } 68 73 public static String encrypt (String value) 74 { 75 String clearText = value; 76 if (clearText == null) 77 clearText = ""; 78 if (s_cipher == null) 80 initCipher(); 81 if (s_cipher != null) 83 { 84 try 85 { 86 s_cipher.init(Cipher.ENCRYPT_MODE, s_key); 87 byte[] encBytes = s_cipher.doFinal(clearText.getBytes()); 88 String encString = convertToHexString(encBytes); 89 return encString; 91 } 92 catch (Exception ex) 93 { 94 ex.printStackTrace(); 95 } 96 } 97 return CLEARTEXT + value; 98 } 100 105 public static String decrypt (String value) 106 { 107 if (value == null) 108 return null; 109 if (value.length() == 0) 110 return value; 111 if (value.startsWith(CLEARTEXT)) 112 return value.substring(3); 113 if (s_cipher == null) 115 initCipher(); 116 117 if (s_cipher != null && value != null && value.length() > 0) 119 { 120 try 121 { 122 AlgorithmParameters ap = s_cipher.getParameters(); 123 s_cipher.init(Cipher.DECRYPT_MODE, s_key, ap); 124 byte[] out = s_cipher.doFinal(convertHexString(value)); 125 return new String (out); 127 } 128 catch (Exception ex) 129 { 130 System.err.println ("Secure.decrypt=" + value); 131 ex.printStackTrace(); 132 } 133 } 134 return value; 135 } 137 138 139 144 public static int hash (String key) 145 { 146 long tableSize = 2147483647; long hashValue = 0; 148 149 for (int i = 0; i < key.length(); i++) 150 hashValue = (37 * hashValue) + (key.charAt(i) -31); 151 152 hashValue %= tableSize; 153 if (hashValue < 0) 154 hashValue += tableSize; 155 156 int retValue = (int)hashValue; 157 return retValue; 158 } 160 161 162 169 public static String getDigest (String message) 170 { 171 MessageDigest md = null; 172 try 173 { 174 md = MessageDigest.getInstance("MD5"); 175 } 177 catch (NoSuchAlgorithmException nsae) 178 { 179 nsae.printStackTrace(); 180 } 181 md.reset(); 183 byte[] input = message.getBytes(); 185 md.update(input); 187 byte[] output = md.digest(); 189 md.reset(); 190 return convertToHexString(output); 192 } 194 199 public static boolean isDigest (String value) 200 { 201 if (value == null || value.length() != 32) 202 return false; 203 return (convertHexString(value) != null); 205 } 207 208 209 214 static private String convertToHexString (byte[] bytes) 215 { 216 int size = bytes.length; 218 StringBuffer buffer = new StringBuffer (size*2); 219 for(int i=0; i<size; i++) 220 { 221 int x = bytes[i]; 223 if (x < 0) 225 x += 256; 226 String tmp = Integer.toHexString(x); 227 if (tmp.length() == 1) 229 buffer.append("0"); 230 buffer.append(tmp); 231 } 232 return buffer.toString(); 233 } 235 236 241 static private byte[] convertHexString (String hexString) 242 { 243 int size = hexString.length()/2; 244 byte[] retValue = new byte[size]; 245 String inString = hexString.toLowerCase(); 246 247 try 248 { 249 for (int i = 0; i < size; i++) 250 { 251 int index = i*2; 252 int ii = Integer.parseInt(inString.substring(index, index+2), 16); 253 retValue[i] = (byte)ii; 254 } 255 return retValue; 256 } 257 catch (Exception e) 258 { 259 System.err.println("Secure.convertHexString error - " + e.getMessage()); 260 } 261 return null; 262 } 264 265 266 270 public static void main (String [] args) 271 { 272 String [] testString = new String [] 273 {"This is a test!", "", "This is a verly long test string 1624$%"}; 274 String [] digestResult = new String [] 275 {"702edca0b2181c15d457eacac39de39b", "d41d8cd98f00b204e9800998ecf8427e", "934e7c5c6f5508ff50bc425770a10f45"}; 276 277 for (int i = 0; i < testString.length; i++) 278 { 279 String digestString = getDigest(testString[i]); 280 if (digestResult[i].equals(digestString)) 281 System.out.println("OK - digest"); 282 else 283 System.err.println("Digest=" + digestString + " <> " + digestResult[i]); 284 } 285 286 System.out.println("IsDigest true=" + isDigest(digestResult[0])); 287 System.out.println("IsDigest false=" + isDigest("702edca0b2181c15d457eacac39DE39J")); 288 System.out.println("IsDigest false=" + isDigest("702e")); 289 290 292 String in = "4115da655707807F00FF"; 295 byte[] bb = convertHexString(in); 296 String out = convertToHexString(bb); 297 if (in.equalsIgnoreCase(out)) 298 System.out.println("OK - conversion"); 299 else 300 System.err.println("Conversion Error " + in + " <> " + out ); 301 302 304 String test = "This is a test!!"; 305 String result = "28bd14203bcefba1c5eaef976e44f1746dc2facaa9e0623c"; 306 String test_1 = decrypt(result); 308 if (test.equals(test_1)) 309 System.out.println("OK - dec_1"); 310 else 311 System.out.println("TestDec=" + test_1 + " <> " + test); 312 313 315 String testEnc = encrypt(test); 316 if (result.equals(testEnc)) 317 System.out.println("OK - enc"); 318 else 319 System.err.println("TestEnc=" + testEnc + " <> " + result); 320 321 String testDec = decrypt(testEnc); 322 if (test.equals(testDec)) 323 System.out.println("OK - dec"); 324 else 325 System.out.println("TestDec=" + testDec + " <> " + test); 326 327 328 } 330 } | Popular Tags |