1 package com.quikj.client.framework; 2 3 import lava.security.des.*; 4 import java.util.*; 5 6 11 public final class DESEncryption 12 { 13 private static final String PAD8 = " "; 14 15 18 public DESEncryption() 19 { 20 } 21 27 public static String byteToString(byte[] inp) 28 { 29 StringBuffer buffer = new StringBuffer (); 30 for (int i = 0; i < inp.length; i++) 31 { 32 String n = Byte.toString(inp[i]); 33 int len = n.length(); 34 35 if (len == 1) 36 { 37 buffer.append("$$$" + n); 38 } 39 else if (len == 2) 40 { 41 buffer.append("$$" + n); 42 } 43 else if (len == 3) 44 { 45 buffer.append("$" + n); 46 } 47 else 48 { 49 buffer.append(n); 50 } 51 } 52 53 return buffer.toString(); 54 55 } 56 62 public String decode(String text, String secret) 63 { 64 try 65 { 66 byte[] input = stringToByte(text); 67 if (input == null) 68 { 69 return null; 70 } 71 72 byte[] output = new byte[input.length]; 73 74 DesKey des_key = new DesKey(secret.getBytes(), 0); 75 DesEngine engine = new DesEngine(des_key); 76 77 engine.decrypt(input, output, input.length); 78 return new String (output); 79 } 80 catch (Exception ex) 81 { 82 return null; 83 } 84 } 85 91 public String encode(String text, String secret) 92 { 93 try 94 { 95 int pad_length = 8 - (text.length() % 8); 96 97 String padded_text = text + PAD8.substring(0, pad_length); 98 byte[] input = padded_text.getBytes(); 99 100 byte[] output = new byte[input.length]; 101 102 DesKey des_key = new DesKey(secret.getBytes(), 0); 103 DesEngine engine = new DesEngine(des_key); 104 105 engine.encrypt(input, output, text.length()); 106 return byteToString(output); 107 } 108 catch (Exception ex) 109 { 110 return null; 111 } 112 } 113 118 public static void main(String [] args) 119 { 120 try 121 { 122 if (args.length < 3) 123 { 124 System.err.println("Usage <-e/-d> text secret"); 125 System.exit(1); 126 } 127 128 DESEncryption enc = new DESEncryption(); 129 if (args[0].equals("-e") == true) 130 { 131 System.out.println("Result: " + enc.encrypt(args[1], args[2])); 132 133 } 134 else if (args[0].equals("-d") == true) 135 { 136 System.out.println("Result: --" + enc.decrypt(args[1], args[2]) 137 +"--"); 138 } 139 else 140 { 141 System.err.println("Unknown option " + args[0]); 142 System.exit(2); 143 } 144 } 145 catch (Exception ex) 146 { 147 System.err.println(ex.getClass().getName() + ": " + ex.getMessage()); 148 System.exit(3); 149 } 150 151 System.exit(0); 152 } 153 159 public static byte[] stringToByte(String text) 160 { 161 int len = text.length(); 162 if ((len % 4) > 0) 163 { 164 return null; 165 } 166 167 byte[] input = new byte[len / 4]; 168 for (int i = 0; i < len; i += 4) 169 { 170 String element = text.substring(i, i + 4); 171 172 if (element.startsWith("$$$") == true) 173 { 174 element = element.substring(3); 175 } 176 else if (element.startsWith("$$") == true) 177 { 178 element = element.substring(2); 179 } 180 else if (element.startsWith("$") == true) 181 { 182 element = element.substring(1); 183 } 184 185 try 186 { 187 input[i / 4] = Byte.parseByte(element); 188 } 189 catch (NumberFormatException ex) 190 { 191 return null; 192 } 193 } 194 return input; 195 } 196 197 public String encrypt(String text, String secret) 198 { 199 return text.length() + ":" + encode(text, secret); 200 } 201 202 public String decrypt(String text, String secret) 203 { 204 StringTokenizer tokens = new StringTokenizer(text, ":"); 205 int num_tokens = tokens.countTokens(); 206 207 if (num_tokens < 2) 208 { 209 return null; 210 } 211 212 String size_s = tokens.nextToken(); 213 int size = 0; 214 try 215 { 216 size = Integer.parseInt(size_s); 217 } 218 catch (NumberFormatException ex) 219 { 220 return null; 221 } 222 223 String encrypted_message = text.substring(size_s.length() + 1); 224 String decrypted_str = decode(encrypted_message, secret); 225 if (decrypted_str == null) 226 { 227 return null; 228 } 229 230 return decrypted_str.substring(0, size); 231 } 232 } 233 | Popular Tags |