3des加密解密

这两天的项目里有用到加解密的算法,与大家分享一下!

项目要求:

采用3DES对加密字符串进行加密并以BASE64格式返加密串

返回参数,均以采用3DES对解密串(Base64格式)进行解密(固定密钥)

package net.nearmobile.fj.webservice.test;


import java.security.Security;


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class ThreeDes {
//private static final String Algorithm = "DESede";


/**
* 3DES+BASE64加密
* 商务领航接口中的参数 EncryptedCallParams 的加密算法
* @param sourceString
* @param hex3DES
* @return
*/
public String crypt3DESToBase64(String sourceString, String hex3DES) {
String strRTN = null;
try {
byte[] bytesString = sourceString.getBytes("UTF-8");
byte[] bytesKey = Hex.decode(hex3DES);
Security.addProvider(new BouncyCastleProvider());
Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC");
//Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding");
cEnc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytesKey, "DESede"),
new IvParameterSpec(Hex.decode("0102030405060708")));
//cEnc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytesKey, "DESede/CBC/PKCS7Padding"));
byte[] bytes3DES = cEnc.doFinal(bytesString);
BASE64Encoder base64en = new BASE64Encoder();
strRTN = base64en.encode(bytes3DES);
}
catch (Exception e) {
System.out.println(
"CommonAlgorithm.calculateRS(String sourceString,String hex3DES)" +
e.getMessage());
return strRTN;
}
return strRTN;
}


public String deCrypt3DESFromBase64(String sourceString, String hex3DES) {
String strRTN = null;
try {
BASE64Decoder base64en = new BASE64Decoder();
byte[] bytesString = base64en.decodeBuffer(sourceString);
byte[] bytesKey = Hex.decode(hex3DES);
Security.addProvider(new BouncyCastleProvider());
Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC");
cEnc.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(bytesKey, "DESede"), new IvParameterSpec(
Hex.decode("0102030405060708")));
byte[] bytes3DES = cEnc.doFinal(bytesString);
strRTN=new String(bytes3DES,"UTF-8");
} catch (Exception e) {
System.out
.println("CommonAlgorithm.calculateRS(String sourceString,String hex3DES)"
+ e.getMessage());
return strRTN;
}
return strRTN;
}












public static void main(String[] args)throws Exception {
ThreeDes test = new ThreeDes();
System.out.println(test.crypt3DESToBase64("测试", "D46C29117F507DF7852AED6C52A4F2C084AACE67054BD170"));
String cryCode = test.crypt3DESToBase64("测试", "D46C29117F507DF7852AED6C52A4F2C084AACE67054BD170");
System.out.println(test.deCrypt3DESFromBase64(cryCode,"D46C29117F507DF7852AED6C52A4F2C084AACE67054BD170"));
//System.out.println(test.base64ToDecrypt("D46C29117F507DF7852AED6C52A4F2C084AACE67054BD170",cryCode));
//System.out.println(test.getDesString("D46C29117F507DF7852AED6C52A4F2C084AACE67054BD170",cryCode));


/*BASE64Encoder base64en = new BASE64Encoder();
String strRTN = base64en.encode("测试".getBytes());
BASE64Decoder base64de = new BASE64Decoder();
String srcCode = new String(base64de.decodeBuffer(cryCode));
System.out.println(srcCode);*/
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值