编程语言 php java

Java进行3DES加密解密算法实现

java HTML我帮您 1年前  0次浏览
3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准),加密算法,其具体实现如下:设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密文,这样:
3DES加密过程为:C=Ek3(Dk2(Ek1(P)))
3DES解密过程为:P=Dk1(EK2(Dk3(C)))

   import java.security.Security;  
import java.util.Arrays;

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

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

/***
* bcprov-jdk15on-150.jar
*
* @ClassName Test3DES
* @author html580.com
* @date 2014年6月16日
*/
public class Test3DES {

public static void main(String<> args) throws Exception {
String key = "30313233343536373938373635343332";
String data = "0088757365722020202020202020202020202020202020202020202020202013111816000001123456bc445951f5f60762f951146ece3bed0fbde2c4198f7dc43018f1d5f2c807c47f26FFD30AAF8E3823";
String result = encryptStr(data,key);
System.out.println("加密后:"+result);

System.out.println("解密后:"+decryptStr(result,key));
}

/**
* 3DES加密 (亦称为:DESede加密)
*
* CBC模式
* 填充模式:零字节填充 ZeroBytePadding
*
* @Method: encrypt3Str
* @param @param data
* @param @param key
* @param @return
* @param @throws Exception
* @return String
* @throws
*/
public static String encryptStr(String data,String key) throws Exception {
String result = "";
try {
Security.addProvider(new BouncyCastleProvider());
byte<> bKey = Hex.decode(key);//十六进制转换成字节数据
byte<> bMsg = Hex.decode(data);

byte<> keyBytes = Arrays.copyOf(bKey, 24);
int j = 0, k = 16;
while (j < 8) {
keyBytes = keyBytes;
}

SecretKey key3 = new SecretKeySpec(keyBytes, "DESede");
IvParameterSpec iv3 = new IvParameterSpec(new byte<8>);//初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Cipher cipher3 = Cipher.getInstance("DESede/CBC/ZeroBytePadding"); //DESede/CBC/ZeroBytePadding,DESede/ECB/NoPadding,DESede/CBC/NoPadding,DES/ECB/PKCS5Padding 根据具体情况来定,有些不需要使用到向量的,去掉向量
cipher3.init(Cipher.ENCRYPT_MODE, key3, iv3);

byte<> bMac = cipher3.doFinal(bMsg);
result= new String(Hex.encode(bMac));//encode方法字节数组转换成十六进制
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return result;
}

/**
* 3DES 解密
* @Method: decryptStr
* @param @param data
* @param @param key
* @param @return
* @param @throws Exception
* @return String
* @throws
*/
public static String decryptStr(String data,String key) throws Exception{
String result = "";
try {
Security.addProvider(new BouncyCastleProvider());
byte<> bKey = Hex.decode(key);//十六进制转换成字节数据
byte<> bMsg = Hex.decode(data);

byte<> keyBytes = Arrays.copyOf(bKey, 24);
int j = 0, k = 16;
while (j < 8) {
keyBytes = keyBytes;
}

SecretKey key3 = new SecretKeySpec(keyBytes, "DESede");
IvParameterSpec iv3 = new IvParameterSpec(new byte<8>);//初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Cipher cipher3 = Cipher.getInstance("DESede/CBC/ZeroBytePadding");
cipher3.init(Cipher.DECRYPT_MODE, key3, iv3);

byte<> bMac = cipher3.doFinal(bMsg);
result= new String(Hex.encode(bMac));//encode方法字节数组转换成十六进制
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return result;
}
}



发表评论