java desede_DESede对称加密算法工具类

packagecom.pcict.util.test;importorg.apache.commons.codec.binary.Base64;import javax.crypto.*;importjavax.crypto.spec.DESedeKeySpec;importjava.nio.charset.Charset;importjava.security.InvalidKeyException;importjava.security.Key;importjava.security.NoSuchAlgorithmException;importjava.security.SecureRandom;importjava.security.spec.InvalidKeySpecException;importjava.util.Arrays;/*** DESede对称加密算法

*

* @Description

*@authorljz

* @created 2015年7月31日 上午11:30:04

*@version* @history

*@see

*/

public classDESedeUtils {//加密模式

public static final int ENCRYPT_MODE =Cipher.ENCRYPT_MODE;//解密模式

public static final int DECRYPT_MODE =Cipher.DECRYPT_MODE;private static final String ALGORITHM = "DESede";private static final Charset UTF8 = Charset.forName("UTF-8");private Cipher cipher = null;private int opmode = 0;//初始化加密或解密

public synchronized boolean init(intmode, String key) {if (opmode != 0) {return true;

}if (mode != ENCRYPT_MODE && mode !=DECRYPT_MODE) {return false;

}if (key == null ||key.isEmpty()) {return false;

}try{

cipher=Cipher.getInstance(ALGORITHM);

}catch(NoSuchAlgorithmException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(NoSuchPaddingException e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{if (cipher == null) {return false;

}

}

Key secKey=getSecKey(key);if (secKey == null) {return false;

}try{

cipher.init(mode, secKey,newSecureRandom());

}catch(InvalidKeyException e) {//TODO Auto-generated catch block

e.printStackTrace();return false;

}

opmode=mode;return true;

}private staticKey getSecKey(String key) {

SecretKey securekey= null;try{byte[] material =Arrays.copyOf(

Base64.decodeBase64(key.getBytes(UTF8)),24);

DESedeKeySpec keySpec= newDESedeKeySpec(material);

SecretKeyFactory keyFactory=SecretKeyFactory

.getInstance(ALGORITHM);

securekey=keyFactory.generateSecret(keySpec);

}catch(InvalidKeyException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(NoSuchAlgorithmException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(InvalidKeySpecException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnsecurekey;

}//加密

public synchronizedString encrypt(String data) {if (opmode !=ENCRYPT_MODE) {return null;

}if (data == null) {return null;

}byte[] encData = null;try{

encData=cipher.doFinal(data.getBytes(UTF8));

}catch(IllegalBlockSizeException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(BadPaddingException e) {//TODO Auto-generated catch block

e.printStackTrace();

}if (encData == null) {return null;

}return newString(Base64.encodeBase64(encData), UTF8);

}//解密

public synchronizedString decrypt(String data) {if (opmode !=DECRYPT_MODE) {return null;

}if (data == null) {return null;

}byte[] decData = null;try{

decData=cipher.doFinal(Base64.decodeBase64(data.getBytes(UTF8)));

}catch(IllegalBlockSizeException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(BadPaddingException e) {//TODO Auto-generated catch block

e.printStackTrace();

}if (decData == null) {return null;

}return newString(decData, UTF8);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值