packagecom.zhx.base.util;importorg.apache.commons.codec.binary.Base64;importjavax.crypto.Cipher;importjavax.crypto.KeyGenerator;importjavax.crypto.SecretKey;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESedeKeySpec;importjava.security.Key;public classTripleDES {public static final String CIPHERMODE = "DESede/ECB/PKCS5Padding";public static final String ENCODE = "utf-8";/*** 初始化密钥
*@return*@throwsException*/
public static String getKeyStr() throwsException{returnBase64.encodeBase64String(getKey().getEncoded());
}/*** 获取密钥
*@return*@throwsException*/
public static Key getKey() throwsException{//实例化
KeyGenerator kg = KeyGenerator.getInstance("DESede");
kg.init(168);//生成密钥
SecretKey secretKey =kg.generateKey();returnsecretKey;
}public staticString encrypt(String key, String src) {try{
DESedeKeySpec dks= newDESedeKeySpec(key.getBytes(ENCODE));
SecretKeyFactory keyFactory=SecretKeyFactory
.getInstance("DESede");
SecretKey securekey=keyFactory.generateSecret(dks);
Cipher cipher=Cipher.getInstance(CIPHERMODE);
cipher.init(Cipher.ENCRYPT_MODE, securekey);byte[] b =cipher.doFinal(src.getBytes());return Base64.encodeBase64String(b).replaceAll("\r", "").replaceAll("\n", "");
}catch(Exception e) {
e.printStackTrace();
}return null;
}public staticString decrypt(String key, String src) {try{byte[] bytesrc =Base64.decodeBase64(src);
DESedeKeySpec dks= newDESedeKeySpec(key.getBytes(ENCODE));
SecretKeyFactory keyFactory=SecretKeyFactory
.getInstance("DESede");
SecretKey securekey=keyFactory.generateSecret(dks);
Cipher cipher=Cipher.getInstance(CIPHERMODE);
cipher.init(Cipher.DECRYPT_MODE, securekey);byte[] retByte =cipher.doFinal(bytesrc);return new String(retByte, "utf-8");
}catch(Exception e) {
e.printStackTrace();
}return null;
}public static void main(String[] args) throwsException {
String key=getKeyStr();
System.out.println(key);
System.out.println(key.getBytes().length);
String test= TripleDES.encrypt(key, "1232135sdsadsasdshgjhkjhkadsadsdgdfhgdjhkbvnbcnbhgfjhgjhgkhghfdsgfdsgadsadadsad");
System.out.println(test);
test= test.replace("=","");
System.out.println(test);//System.out.println(Base64Utils.getBase64(test));
System.out.println(TripleDES.decrypt(key,test));
}
}