1 package org.jahia.clipbuilder.html.util; 2 import java.io.UnsupportedEncodingException ; 3 import java.security.*; 4 import java.security.spec.InvalidKeySpecException ; 5 6 import javax.crypto.*; 7 8 13 public class DESUtilities { 14 private static DESUtilities instance; 15 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DESUtilities.class); 16 17 18 19 26 public String encrypt(String key, String message) { 27 DESEncrypter encrypter = new DESEncrypter(); 29 30 encrypter.setKey(key.getBytes()); 32 33 String encryptedMessage = encrypter.encrypt(message); 35 return encryptedMessage; 36 } 37 38 39 46 public String decrypt(String key, String encryptedMessage) { 47 DESDecrypter decrypter = new DESDecrypter(); 49 50 decrypter.setKey(key.getBytes()); 52 53 String message = decrypter.decrypt(encryptedMessage); 55 return message; 56 } 57 58 59 64 public Key generateKey() { 65 try { 66 KeyGenerator kg = KeyGenerator.getInstance("DES"); 67 kg.init(56); 68 Key key = kg.generateKey(); 70 return key; 71 } 72 catch (NoSuchAlgorithmException ex) { 73 ex.printStackTrace(); 74 return null; 75 } 76 77 } 78 79 80 81 86 public static DESUtilities getInstance() { 87 if (instance == null) { 88 instance = new DESUtilities(); 89 } 90 return instance; 91 } 92 93 94 95 100 class DESEncrypter { 101 private Cipher ecipher; 102 103 104 107 public DESEncrypter() { 108 109 try { 110 ecipher = Cipher.getInstance("DES"); 111 } 112 catch (NoSuchPaddingException ex) { 113 ex.printStackTrace(); 114 } 115 catch (NoSuchAlgorithmException ex) { 116 ex.printStackTrace(); 117 } 118 119 } 120 121 122 123 128 public void setKey(byte[] keyByte) { 129 130 try { 131 javax.crypto.spec.DESKeySpec sks = new javax.crypto.spec.DESKeySpec(keyByte); 133 SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(sks); 134 135 ecipher.init(Cipher.ENCRYPT_MODE, sKey); 136 } 137 catch (NoSuchAlgorithmException ex) { 138 ex.printStackTrace(); 139 } 140 catch (InvalidKeySpecException ex) { 141 ex.printStackTrace(); 142 } 143 catch (InvalidKeyException ex) { 144 ex.printStackTrace(); 145 } 146 147 } 148 149 150 156 public String encrypt(String str) { 157 158 try { 159 byte[] utf8 = str.getBytes("UTF8"); 161 162 byte[] enc = ecipher.doFinal(utf8); 164 165 return new sun.misc.BASE64Encoder().encode(enc); 167 } 168 catch (BadPaddingException ex) { 169 ex.printStackTrace(); 170 return null; 171 } 172 catch (IllegalBlockSizeException ex) { 173 ex.printStackTrace(); 174 return null; 175 } 176 catch (UnsupportedEncodingException ex) { 177 ex.printStackTrace(); 178 return null; 179 } 180 181 } 182 183 } 184 185 186 191 class DESDecrypter { 192 private Cipher dcipher; 193 194 195 198 public DESDecrypter() { 199 200 try { 201 dcipher = Cipher.getInstance("DES"); 202 } 203 catch (NoSuchPaddingException ex) { 204 ex.printStackTrace(); 205 } 206 catch (NoSuchAlgorithmException ex) { 207 ex.printStackTrace(); 208 } 209 210 } 211 212 213 214 219 public void setKey(byte[] keyByte) { 220 221 try { 222 javax.crypto.spec.DESKeySpec sks = new javax.crypto.spec.DESKeySpec(keyByte); 224 SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(sks); 225 226 dcipher.init(Cipher.DECRYPT_MODE, sKey); 228 } 229 catch (NoSuchAlgorithmException ex) { 230 ex.printStackTrace(); 231 } 232 catch (InvalidKeySpecException ex) { 233 ex.printStackTrace(); 234 } 235 catch (InvalidKeyException ex) { 236 ex.printStackTrace(); 237 } 238 239 } 240 241 242 243 249 public String decrypt(String str) { 250 try { 251 byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); 253 254 byte[] utf8 = dcipher.doFinal(dec); 256 257 return new String (utf8, "UTF8"); 259 } 260 catch (javax.crypto.BadPaddingException e) { 261 262 e.printStackTrace(); 263 } 264 catch (IllegalBlockSizeException e) { 265 e.printStackTrace(); 266 } 267 catch (UnsupportedEncodingException e) { 268 e.printStackTrace(); 269 } 270 catch (java.io.IOException e) { 271 e.printStackTrace(); 272 } 273 return null; 274 } 275 } 276 277 } 278 279 | Popular Tags |