1 packagecom.util;2 importjava.security.Key;3 importjava.security.SecureRandom;4
5 importjavax.crypto.Cipher;6 importjavax.crypto.SecretKeyFactory;7 importjavax.crypto.spec.DESKeySpec;8
9 importorg.apache.commons.codec.binary.Base64;10
11 importcom.util.DesEncrypt;12
13 /**
14 * 加密解密15 *@authorbian16 * 2015 上午11:13:3617 */
18 public classDesEncrypt {19 Key key;20 publicDesEncrypt(String str) {21 try{22 String key =str;23 setKey(key);//生成密匙
24 }catch(Exception e){25
26 }27 }28
29 publicDesEncrypt() {30 setKey("heimazhifuqw233344");31 }32
33 /**
34 * 根据参数生成KEY35 */
36 public voidsetKey(String strKey) {37 try{38 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");39 this.key = keyFactory.generateSecret(new DESKeySpec(strKey.getBytes("UTF8")));40 } catch(Exception e) {41 throw newRuntimeException(42 "Error initializing SqlMap class. Cause: " +e);43 }44 }45
46
47 /**
48 * 加密String明文输入,String密文输出49 */
50 publicString encrypt(String strMing) {51 byte[] byteMi = null;52 byte[] byteMing = null;53 String strMi = "";54 //BASE64Encoder base64en = new BASE64Encoder();
55 try{56 byteMing = strMing.getBytes("UTF8");57 byteMi = this.getEncCode(byteMing);58 strMi = new String(Base64.encodeBase64(byteMi), "UTF8");59 } catch(Exception e) {60 throw newRuntimeException(61 "Error initializing SqlMap class. Cause: " +e);62 } finally{63 //base64en = null;
64 byteMing = null;65 byteMi = null;66 }67 returnstrMi;68 }69
70 /**
71 * 解密 以String密文输入,String明文输出72 *73 *@paramstrMi74 *@return
75 */
76 publicString decrypt(String strMi) {77 ///BASE64Decoder base64De = new BASE64Decoder();
78 byte[] byteMing = null;79 byte[] byteMi = null;80 String strMing = "";81 try{82 byteMi =Base64.decodeBase64(strMi);83 byteMing = this.getDesCode(byteMi);84 strMing = new String(byteMing, "UTF8");85 } catch(Exception e) {86 throw newRuntimeException(87 "Error initializing SqlMap class. Cause: " +e);88 } finally{89 //base64De = null;
90 byteMing = null;91 byteMi = null;92 }93 returnstrMing;94 }95
96 /**
97 * 加密以byte[]明文输入,byte[]密文输出98 *99 *@parambyteS100 *@return
101 */
102 private byte[] getEncCode(byte[] byteS) {103 byte[] byteFina = null;104 Cipher cipher;105 try{106 cipher = Cipher.getInstance("DES");107 cipher.init(Cipher.ENCRYPT_MODE, key,SecureRandom.getInstance("SHA1PRNG"));108 byteFina =cipher.doFinal(byteS);109 } catch(Exception e) {110 throw newRuntimeException(111 "Error initializing SqlMap class. Cause: " +e);112 } finally{113 cipher = null;114 }115 returnbyteFina;116 }117
118 /**
119 * 解密以byte[]密文输入,以byte[]明文输出120 *121 *@parambyteD122 *@return
123 */
124 private byte[] getDesCode(byte[] byteD) {125 Cipher cipher;126 byte[] byteFina = null;127 try{128 cipher = Cipher.getInstance("DES");129 cipher.init(Cipher.DECRYPT_MODE, key,SecureRandom.getInstance("SHA1PRNG"));130 byteFina =cipher.doFinal(byteD);131 } catch(Exception e) {132 throw newRuntimeException(133 "Error initializing SqlMap class. Cause: " +e);134 } finally{135 cipher = null;136 }137 returnbyteFina;138 }139
140
141
142 public static voidmain(String args[]) {143 String key = "123456789012345678901234567890123456";//36位
144 String str = "中国";145 DesEncrypt des = newDesEncrypt(key);146
147 //DES加密
148 String mStr =des.encrypt(str);149 //DES解密
150 String deStr =des.decrypt(mStr);151
152 System.out.println("密文:" +mStr);153 System.out.println("明文:" +deStr);154 }155 }