求java3DES加密算法代码转成php的代码

龍sky腾 发布于 2015/04/07 17:31
阅读 305
收藏 1

请高手们帮忙 把如下java代码转换php代码,



package com.test;


import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


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


import sun.misc.BASE64Encoder;






public class TripleDes {
private static final String Algorithm = "DESede/ECB/NoPadding"; // c  ECB/Zeros
private static final byte[] IV = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 };



private static boolean isBlank(String str){
return str==null || str.length()==0?true:false;
}



/**
* 指定私钥加密
* @param key
* @param src
* @return
*/
public static String encode(byte[] key, String src) {
if (isBlank(src)) {
return "";
}
return encryptMode(key, src);
}



/**
* 指定私钥解密
* @param key
* @param src
* @return
*/
public static String decode(byte[] key, String src) {
if (isBlank(src)) {
return "";
}
return decryptMode(key, src);
}

/**
* 指定密钥加密
* @param keybyte 密钥
* @param src 明文
* @return
*/
public static String encryptMode(byte[] keybyte, String src) {
try {
SecretKey deskey = new SecretKeySpec(keybyte, "TripleDES");
IvParameterSpec iv = new IvParameterSpec(IV);
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] s = src.getBytes();

//方式补位
byte[] tmp = new byte[(s.length%8>0? ((1+(s.length>>3))*8):s.length)];

if (s.length < tmp.length) {
System.arraycopy(s, 0 , tmp, 0, s.length);
} else {
System.arraycopy(s, 0 , tmp, 0, tmp.length);
}
return byte2Hex(c1.doFinal(tmp));
} catch (Exception e) {
e.printStackTrace();
return src;
}
}


/**
* 指定密钥解密
* @param keybyte 密钥
* @param src 密文
* @return
*/
public static String decryptMode(byte[] keybyte, String src) {
try {
SecretKey deskey = new SecretKeySpec(keybyte, "TripleDES");
Cipher c1 = Cipher.getInstance(Algorithm);

IvParameterSpec iv = new IvParameterSpec(IV);
c1.init(Cipher.DECRYPT_MODE, deskey);
byte[] d = c1.doFinal(hex2Byte(src));


return (new String(d)).trim();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 根据字符串生成密钥字节数组
* @param keyStr 密钥字符串
* @return
* @throws UnsupportedEncodingException
*/
public static byte[] build3DesKey(String keyStr)
throws UnsupportedEncodingException {
byte[] key = new byte[24]; // 声明一个24位的字节数组,默认里面都是0
byte[] temp = keyStr.getBytes();//hexStrToByte(keyStr); // 将字符串转成字节数组
System.out.println("key length:"+temp.length);
/*
* 执行数组拷贝 System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位)
*/
if (key.length > temp.length) {
// 如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
System.arraycopy(temp, 0, key, 0, temp.length);
// 补位
System.arraycopy(key, temp.length, key, temp.length, key.length - temp.length);
} else {
// 如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
System.arraycopy(temp, 0, key, 0, key.length);
}


return key;
}


private static String byte2Hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
//0×08

stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}


private static byte[] hex2Byte(String str) {
if (str == null)
return null;
str = str.trim();
int len = str.length();
if (len == 0 || len % 2 == 1)
return null;
byte[] b = new byte[len / 2];
try {
for (int i = 0; i < str.length(); i += 2) {
int byteValue = Integer.parseInt(str.substring(i, i + 2), 16);
b[i / 2] = (byte) byteValue;
}
return b;
} catch (Exception e) {
return null;
}
}




public static void main(String[] args) {

try {
String key = "123456";
String szSrc = "60011111111111111111111111111111111";
String encoded = encode(build3DesKey(key), szSrc);
System.out.println("密文:" + encoded);
System.out.println(decode(build3DesKey(key), encoded));

} catch (Exception e) {
e.printStackTrace();
}
}
}


加载中
0
氪金
氪金

http://blog.sina.com.cn/s/blog_8edc37a801016id7.html

按这个重写一下,很快的。

0
龍sky腾
龍sky腾
http://blog.sina.com.cn/s/blog_8edc37a801016id7.html  已经看过了,还是不行。最后用的phpseclib0.3.10里面的3DES解决的问题。http://phpseclib.sourceforge.net
返回顶部
顶部