不少人对于3DES的算法有些陌生,尤其是从事金融类编程的用到的概率相对较大,网上比较完整的例子也很少,因此,写了个比较完整的例子,供大家参考eg:1、利用3DES算法计算MAC2、利用3DES算法解加密一个KEY3、计算校验值此代码采用双倍长密钥为例[java]viewplaincopyprint?1.importjava.security.SecureRandom;2.importjavax.crypto.Cipher;3.importjavax.crypto.SecretKey;4.importjavax.crypto.SecretKeyFactory;5.importjavax.crypto.spec.DESKeySpec;6.importjavax.crypto.spec.IvParameterSpec;7.8.importorg.apache.commons.lang3.ArrayUtils;9.10./**11.*12.*13.*@authorZhangYaMin14.*15.*/16.publicclassEncryptionMachine{17.18.19.20.21.//初始向量22.privatestaticStringivInfo=0000000000000000;23.24.privatestaticchar[]CHARARRAY={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};25.privatestaticbyte[]funStringToBcd(char[]data){26.intlen=data.length;27.if(len%2!=0||len==0){28.thrownewRuntimeException(数据长度错误);29.}30.byte[]outData=newbyte[len1];31.for(inti=0,j=0;jlen;i++){32.outData[i]=(byte)(((Character.digit(data[j],16)&0x0F)4)|(Character.digit(data[j+1],16)&0x0F));33.j++;34.j++;35.}36.37.returnoutData;38.}39.40.privatestaticStringfunByteToHexString(byte[]data){41.intlen=data.length;42.char[]outChar=newchar[len1];43.for(inti=0,j=0;jlen;j++){44.outChar[i++]=CHARARRAY[(0xF0&data[j])4];45.outChar[i++]=CHARARRAY[data[j]&0x0F];46.}47.StringoutString=newString(outChar);48.49.returnoutString;50.}51.52.53./**54.*加密函数(ECB)55.*56.*@paramdata57.*加密数据58.*@paramkey59.*密钥60.*@return返回加密后的数据61.*/62.privatestaticbyte[]ecbEncrypt(byte[]data,byte[]key){63.64.try{65.66.//DES算法要求有一个可信任的随机数源67.SecureRandomsr=newSecureRandom();68.69.//从原始密钥数据创建DESKeySpec对象70.DESKeySpecdks=newDESKeySpec(key);71.72.//创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象73.SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance(DES);74.SecretKeysecretKey=keyFactory.generateSecret(dks);75.76.//DES的ECB模式77.Ciphercipher=Cipher.getInstance(DES/ECB/NoPadding);78.79.//用密钥初始化Cipher对象80.cipher.init(Cipher.ENCRYPT_MODE,secretKey,sr);81.82.//执行加密操作83.byteencryptedData[]=cipher.doFinal(data);84.85.returnencryptedData;86.}catch(Exceptione){87.thrownewRuntimeException(ECB-DES算法,加密数据出错!);88.}89.90.}91.92./**93.*解密函数(ECB)94.*95.*@paramdata96.*解密数据97.*@paramkey98.*密钥99.*@return返回解密后的数据100.*/101.privatestaticbyte[]ecbDecrypt(byte[]data,byte[]key){102.try{103.//DES算法要求有一个可信任的随机数源104.SecureRandomsr=newSecureRandom();105.106.//byterawKeyData[]=/*用某种方法获取原始密匙数据*/;107.108.//从原始密匙数据创建一个DESKeySpec对象109.DESKeySpecdks=newDESKeySpec(key);110.111.//创建一个密匙工厂,然后用它把DESKeySpec对象转换成一个SecretKey对象112.SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance(DES);113.SecretKeysecretKey=keyFactory.generateSecret(dks);114.115.//DES的ECB模式116.Ciphercipher=Cipher.getInstance(DES/ECB/NoPadding);117.118.//用密钥初始化Cipher对象119.cipher.init(Cipher.DECRYPT_MODE,secretKey,sr);120.121.//正式执行解密操作122.bytedecryptedData[]=cipher.doFinal(data);123.124.returndecryptedData;125.}catch(Exceptione){126.thrownewRuntimeException(ECB-DES算法,解密出错。);127.}128.129.}130.131./**132.*加密函数(CBC)133.*134.*@paramdata135.*加密数据136.*@paramkey137.*密钥138.*@return返回加密后的数据139.*/140.privatestaticbyte[]cbcEncrypt(byte[]data,byte[]key,byte[]iv){141.142.try{143.//从原始密钥数据创建DESKeySpec对象144.DESKeySpecdks=newDESKeySpec(key);145.146.//创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象147.SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance(DES);148.SecretKeysecretKey=keyFactory.generateSecret(dks);149.150.//DES的CBC模式,采用NoPadding模式,data长度必须是8的倍数151.Ciphercipher=Cipher.getInstance(DES/CBC/NoPadding);152.153.//用密钥初始化Cipher对象154.IvParameterSpecparam=newIvParameterSpec(iv);155.cipher.init(Cipher.ENCRYPT_MODE,secretKey,param);156.157.//执行加密操作158.byteencryptedData[]=cipher.doFinal(data);159.160.returnencryptedData;161.}catch(Exceptione){162.thrownewRuntimeException(CBC-DES算法,加密数据出错!);163.}164.}165.166./**167.*解密函数(CBC)168.*169.*@paramdata170.*解密数据171.*@paramkey172.*密钥173.*@return返回解密后的数据174.*/175.@SuppressWarnings(unused)176.privatestaticbyte[]cbcDecrypt(byte[]data,byte[]key,byte[]iv){177.try{178.//从原始密匙数据创建一个DESKeySpec对象179.DESKeySpecdks=newDESKeySpec(key);180.181.//创建一个密匙工厂,然后用它把DESKeySpec对象转换成一个SecretKey对象182.SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance(DES);183.SecretKeysecretKey=keyFactory.generateSecret(dks);184.185.//DES的CBC模式,采用NoPadding模式,data长度必须是8的倍数186.Ciphercipher=Cipher.getInstance(DES/CBC/NoPadding);187.188.//用密钥初始化Cipher对象189.IvParameterSpecparam=newIvParameterSpec(iv);190.cipher.init(Cipher.DECRYPT_MODE,secretKey,param);191.192.//正式执行解密操作193.bytedecryptedData[]=cipher.doFinal(data);194.195.returndecryptedData;196.}catch(Exceptione){197.thrownewRuntimeException(CBC-DES算法,解密出错。);198.}199.200.}201.202./**203.*将工作密钥解为明文204.*@paramplaintextZmk205.*解workKey的Zmk(明文)206.*@paramciphertextWorkingKey207.*workKey密文208.*@return209.*返回workKey明文210.**/211.publicstaticStringanalogDecryptWorkingKey(StringplaintextZmk,StringciphertextWorkingKey){212.byte[]inPlaintextZmk=funStringToBcd(plaintextZmk.toCharArray());213.byte[]inCiphertextWorkingKey=funStringToBcd(ciphertextWorkingKey.toCharArray());214.215.byte[]leftPlaintextZmk=ArrayUtils.subarray(inPlaintextZmk,0,8);216.byte[]rightPlai