这两天的项目里有用到加解密的算法,与大家分享一下!
项目要求:
采用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);*/
}
}