1 package com.quikj.server.framework; 2 3 import java.security.*; 4 import javax.crypto.*; 5 import javax.crypto.spec.*; 6 11 public final class Encryption 12 { 13 private javax.crypto.SecretKey secretKey = null; 14 15 18 public Encryption() 19 throws 20 InvalidKeyException, 21 NoSuchAlgorithmException, 22 java.security.spec.InvalidKeySpecException 23 { 24 super(); 25 26 Provider sun_jce = new com.sun.crypto.provider.SunJCE(); 27 Security.addProvider(sun_jce); 28 } 29 35 public static String byteToString(byte[] inp) 36 { 37 StringBuffer buffer = new StringBuffer (); 38 for (int i = 0; i < inp.length; i++) 39 { 40 String n = Byte.toString(inp[i]); 41 int len = n.length(); 42 43 if (len == 1) 44 { 45 buffer.append("$$$" + n); 46 } 47 else if (len == 2) 48 { 49 buffer.append("$$" + n); 50 } 51 else if (len == 3) 52 { 53 buffer.append("$" + n); 54 } 55 else 56 { 57 buffer.append(n); 58 } 59 } 60 61 return buffer.toString(); 62 63 } 64 70 public String decrypt(String text, String secret) 71 { 72 try 73 { 74 DESKeySpec des_key_spec = new DESKeySpec(secret.getBytes()); 75 SecretKeyFactory key_factory = SecretKeyFactory.getInstance("DES"); 76 javax.crypto.SecretKey secret_key = key_factory.generateSecret(des_key_spec); 77 78 byte[] input = stringToByte(text); 79 if (input == null) 80 { 81 return null; 82 } 83 84 Cipher cipher = Cipher.getInstance("DES"); 85 cipher.init(Cipher.DECRYPT_MODE, secret_key); 86 byte[] decrypt_text = cipher.doFinal(input); 87 return new String (decrypt_text); 88 } 89 catch (Exception ex) 90 { 91 return null; 92 } 93 } 94 100 public String encrypt(String text, String secret) 101 { 102 try 103 { 104 byte[] secret_bytes = secret.getBytes(); 105 DESKeySpec des_key_spec = new DESKeySpec(secret_bytes); 106 SecretKeyFactory key_factory = SecretKeyFactory.getInstance("DES"); 107 javax.crypto.SecretKey secret_key = key_factory.generateSecret(des_key_spec); 108 109 Cipher cipher = Cipher.getInstance("DES"); 110 cipher.init(Cipher.ENCRYPT_MODE, secret_key); 111 byte[] cipher_text = cipher.doFinal(text.getBytes()); 112 113 return byteToString(cipher_text); 114 } 115 catch (Exception ex) 116 { 117 return null; 118 } 119 } 120 125 public static void main(String [] args) 126 { 127 try 128 { 129 if (args.length < 3) 130 { 131 System.err.println("Usage <-e/-d> text secret"); 132 System.exit(1); 133 } 134 135 Encryption enc = new Encryption(); 136 if (args[0].equals("-e") == true) 137 { 138 System.out.println("Result: " + enc.encrypt(args[1], args[2])); 139 140 } 141 else if (args[0].equals("-d") == true) 142 { 143 System.out.println("Result: " + enc.decrypt(args[1], args[2])); 144 } 145 else 146 { 147 System.err.println("Unknown option " + args[0]); 148 System.exit(2); 149 } 150 } 151 catch (Exception ex) 152 { 153 System.err.println(ex.getClass().getName() + ": " + ex.getMessage()); 154 System.exit(3); 155 } 156 157 System.exit(0); 158 } 159 165 public static byte[] stringToByte(String text) 166 { 167 int len = text.length(); 168 if ((len % 4) > 0) 169 { 170 return null; 171 } 172 173 byte[] input = new byte[len / 4]; 174 for (int i = 0; i < len; i += 4) 175 { 176 String element = text.substring(i, i + 4); 177 178 if (element.startsWith("$$$") == true) 179 { 180 element = element.substring(3); 181 } 182 else if (element.startsWith("$$") == true) 183 { 184 element = element.substring(2); 185 } 186 else if (element.startsWith("$") == true) 187 { 188 element = element.substring(1); 189 } 190 191 try 192 { 193 input[i / 4] = Byte.parseByte(element); 194 } 195 catch (NumberFormatException ex) 196 { 197 return null; 198 } 199 } 200 return input; 201 } 202 } 203 | Popular Tags |