1.å¦ä½ç¨Javaè¿è¡3DESå å¯è§£å¯
2.å¦ä½ä½¿ç¨JAVAå®ç°å¯¹å符串çDESå å¯å解å¯
3.java des加密后的算a实算法串比原字符串长
å¦ä½ç¨Javaè¿è¡3DESå å¯è§£å¯
è¿éæ¯ä¾åï¼ç´æ¥æ¿æ¥ç¨å°±å¯ä»¥äºã
package com.nnff.des;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/*å符串 DESede(3DES) å å¯
* ECB模å¼/使ç¨PKCS7æ¹å¼å¡«å ä¸è¶³ä½,ç®åç»çå¯é¥æ¯ä½
* 3DESï¼å³Triple DESï¼æ¯DESåAESè¿æ¸¡çå å¯ç®æ³ï¼å¹´ï¼NISTå°3-DESæå®ä¸ºè¿æ¸¡ç
* å å¯æ åï¼ï¼æ¯DESçä¸ä¸ªæ´å®å ¨çåå½¢ãå®ä»¥DES为åºæ¬æ¨¡åï¼éè¿ç»ååç»æ¹æ³è®¾è®¡åºåç»å
* å¯ç®æ³ï¼å ¶å ·ä½å®ç°å¦ä¸ï¼è®¾Ek()åDk()代表DESç®æ³çå å¯å解å¯è¿ç¨ï¼K代表DESç®æ³ä½¿ç¨ç
* å¯é¥ï¼P代表ææï¼C代表å¯è¡¨ï¼è¿æ ·ï¼
* 3DESå å¯è¿ç¨ä¸ºï¼C=Ek3(Dk2(Ek1(P)))
* 3DES解å¯è¿ç¨ä¸ºï¼P=Dk1((EK2(Dk3(C)))
* */
public class ThreeDes {
/**
* @param argså¨javaä¸è°ç¨sunå ¬å¸æä¾ç3DESå å¯è§£å¯ç®æ³æ¶ï¼éè¦ä½¿
* ç¨å°$JAVA_HOME/jre/lib/ç®å½ä¸å¦ä¸ç4个jarå ï¼
*jce.jar
*security/US_export_policy.jar
*security/local_policy.jar
*ext/sunjce_provider.jar
*/
private static final String Algorithm = "DESede"; //å®ä¹å å¯ç®æ³,å¯ç¨ DES,DESede,Blowfish
//keybyte为å å¯å¯é¥ï¼é¿åº¦ä¸ºåè
//src为被å å¯çæ°æ®ç¼å²åºï¼æºï¼
public static byte[] encryptMode(byte[] keybyte,byte[] src){
try {
//çæå¯é¥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//å å¯
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);//å¨åä¸æ¹é¢çå å¯æ解å¯
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
//keybyte为å å¯å¯é¥ï¼é¿åº¦ä¸ºåè
//src为å å¯åçç¼å²åº
public static byte[] decryptMode(byte[] keybyte,byte[] src){
try {
//çæå¯é¥
SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
//解å¯
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
//转æ¢æåå è¿å¶å符串
public static String byte2Hex(byte[] b){
String hs="";
String stmp="";
for(int n=0; n<b.length; n++){
stmp = (java.lang.Integer.toHexString(b[n]& 0XFF));
if(stmp.length()==1){
hs = hs + "0" + stmp;
}else{
hs = hs + stmp;
}
if(n<b.length-1)hs=hs+":";
}
return hs.toUpperCase();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//æ·»å æ°å®å ¨ç®æ³,å¦æç¨JCEå°±è¦æå®æ·»å è¿å»
Security.addProvider(new com.sun.crypto.provider.SunJCE());
final byte[] keyBytes = { 0x, 0x, 0x4F, 0x,
(byte)0x, 0x, 0x, 0x, 0x, 0x, 0x, 0x,
(byte)0xCB,
(byte)0xDD, 0x, 0x, 0x, 0x, 0x,
(byte)0x, 0x, 0x, 0x,
(byte)0xE2
}; //åèçå¯é¥
String szSrc = "This is a 3DES test. æµè¯";
System.out.println("å å¯åçå符串:" + szSrc);
byte[] encoded = encryptMode(keyBytes,szSrc.getBytes());
System.out.println("å å¯åçå符串:" + new String(encoded));
byte[] srcBytes = decryptMode(keyBytes,encoded);
System.out.println("解å¯åçå符串:" + (new String(srcBytes)));
}
}
å¦ä½ä½¿ç¨JAVAå®ç°å¯¹å符串çDESå å¯å解å¯
/
*** ECB模å¼çdeså å¯ï¼ä»¥baseçç¼ç è¾åº
* @param message
* @param key
* @return
* @throws Exception
*/
public static String desEncrypt(String message, String key)throws Exception{
// DES/ECB CBC CFB OFB /PKCS5Padding NoPadding å å¯/模å¼/å¡«å
Cipher cipher = Cipher.getInstance("DES");//é»è®¤å°±æ¯ DES/ECB/PKCS5Padding
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
cipher.init(1, secretKey);
return new BASEEncoder().encode(cipher.doFinal(message.getBytes("UTF-8")));
}
/
*** ECB模å¼çdes解å¯
* @param message
* @param key
* @return
* @throws Exception
*/
public static String desDecrypt(String message, String key)throws Exception{
Cipher cipher = Cipher.getInstance("DES");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
cipher.init(2, secretKey);
return new String(cipher.doFinal(Base.decode(message)), "UTF-8");
}
ä½ èªå·±åmainæ¹æ³æµè¯ä¸ä¸ï¼åºè¯¥æ¯æ²¡é®é¢ç
java des加密后的串比原字符串长
DESPlus.java
/
*** @author 李国庆
* @company leemenz (C) copyright
* @time Nov 1, :: AM
* @version 1.0.0.0
* @package com.des
*/
package com.des;
import java.security.*;
import javax.crypto.*;
public class DESPlus {
private static String strDefaultKey = "national";
private Cipher encryptCipher = null;
private Cipher decryptCipher = null;
/
*** 将byte数组转换为表示进制值的字符串, 如:byte[]{ 8,法源}转换为:, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的算a实算法java源码1004java源码转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,法源所以字符串的算a实算法源码在线使用教程长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + ;
}
// 小于0F的数需要在前面补0
if (intTmp < ) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, ));
}
return sb.toString();
}
/
*** 将表示进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的法源exagear动态翻译源码转换过程
*
* @param strIn
* 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:leo@.com">LiGuoQing</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,算a实算法所以字节数组长度是法源字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, );
}
return arrOut;
}
/
*** 默认构造方法,使用默认密钥
*
* @throws Exception
*/
public DESPlus() throws Exception {
this(strDefaultKey);
}
/
*** 指定密钥构造方法
*
* @param strKey
* 指定的算a实算法密钥
* @throws Exception
*/
public DESPlus(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/
*** 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/
*** 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
/
*** 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/
*** 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}
/
*** 从指定字符串生成密钥,密钥所需的法源字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的算a实算法字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
}
测试程序 Test.java
/
*** @author 李国庆
* @company leemenz (C) copyright
* @time Nov 1, :: AM
* @version 1.0.0.0
* @package com.des
*/
package com.des;
/
*** @author Administrator
*
*/
public class Test {
/
*** @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String test = "Hellow Word!";
//DESPlus des = new DESPlus();//默认密钥
DESPlus des = new DESPlus("leemenz");//自定义密钥
System.out.println("加密前的字符:"+test);
System.out.println("加密后的字符:"+des.encrypt(test));
System.out.println("解密后的字符:"+des.decrypt(des.encrypt(test)));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
看看 对你有帮助的 不一定加密长度一样的