1 package org.infoglue.cms.util; 2 3 import java.io.UnsupportedEncodingException ; 4 5 import javax.crypto.Cipher; 6 import javax.crypto.IllegalBlockSizeException; 7 import javax.crypto.KeyGenerator; 8 import javax.crypto.SecretKey; 9 10 32 33 public class DesEncryptionHelper 34 { 35 private static SecretKey key = null; 36 37 static 38 { 39 try 40 { 41 key = KeyGenerator.getInstance("DES").generateKey(); 44 } 45 catch (Exception e) 46 { 47 } 48 } 49 50 Cipher ecipher; 51 Cipher dcipher; 52 53 public DesEncryptionHelper() 54 { 55 try 56 { 57 ecipher = Cipher.getInstance("DES"); 58 dcipher = Cipher.getInstance("DES"); 59 ecipher.init(Cipher.ENCRYPT_MODE, key); 60 dcipher.init(Cipher.DECRYPT_MODE, key); 61 62 } 63 catch (javax.crypto.NoSuchPaddingException e) 64 { 65 } 66 catch (java.security.NoSuchAlgorithmException e) 67 { 68 } 69 catch (java.security.InvalidKeyException e) 70 { 71 } 72 } 73 74 public String encrypt(String str) 75 { 76 try 77 { 78 byte[] utf8 = str.getBytes("UTF8"); 80 81 byte[] enc = ecipher.doFinal(utf8); 83 84 return new sun.misc.BASE64Encoder().encode(enc); 86 } 87 catch (javax.crypto.BadPaddingException e) 88 { 89 } 90 catch (IllegalBlockSizeException e) 91 { 92 } 93 catch (UnsupportedEncodingException e) 94 { 95 } 96 catch (java.io.IOException e) 97 { 98 } 99 100 return null; 101 } 102 103 public String decrypt(String str) 104 { 105 try 106 { 107 byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); 109 110 byte[] utf8 = dcipher.doFinal(dec); 112 113 return new String (utf8, "UTF8"); 115 } 116 catch (javax.crypto.BadPaddingException e) 117 { 118 } 119 catch (IllegalBlockSizeException e) 120 { 121 } 122 catch (UnsupportedEncodingException e) 123 { 124 } 125 catch (java.io.IOException e) 126 { 127 } 128 129 return null; 130 } 131 } | Popular Tags |