java版的DES加密,要用php解密,请教如何做 我把java版的DESPlus源码粘出   1 import java.security.*; 
  2 import javax.crypto.*; 
  3 
  4 public class DESPlus { 
  5    private static String strDefaultKey = "national"; 
  6    private Cipher encryptCipher = null; 
  7    private Cipher decryptCipher = null; 
  8 
  9    /** 
10      * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[] 
11      * hexStr2ByteArr(String strIn) 互为可逆的转换过程 
12      * 
13      * @param arrB 
14      *            需要转换的byte数组 
15      * @return 转换后的字符串 
16      * @throws Exception 
17      *            本方法不处理任何异常,所有异常全部抛出 
18      */ 
19    public static String byteArr2HexStr(byte[] arrB) throws Exception { 
20        int iLen = arrB.length; 
21        // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍 
22        StringBuffer sb = new StringBuffer(iLen * 2); 
23        for (int i = 0; i < iLen; i++) { 
24            int intTmp = arrB[i]; 
25            // 把负数转换为正数 
26            while (intTmp < 0) { 
27                intTmp = intTmp + 256; 
28            } 
29            // 小于0F的数需要在前面补0 
30            if (intTmp < 16) { 
31                sb.append("0"); 
32            } 
33            sb.append(Integer.toString(intTmp, 16)); 
34        } 
35        return sb.toString(); 
36    } 
37 
38    /** 
39      * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB) 
40      * 互为可逆的转换过程 
41      * 
42      * @param strIn 
43      *            需要转换的字符串 
44      * @return 转换后的byte数组 
45      * @throws Exception 
46      *            本方法不处理任何异常,所有异常全部抛出 
47      * @author <a href="mailto:[email protected]">LiGuoQing </a> 
48      */ 
49    public static byte[] hexStr2ByteArr(String strIn) throws Exception { 
50        byte[] arrB = strIn.getBytes(); 
51        int iLen = arrB.length; 
52 
53        // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2 
54        byte[] arrOut = new byte[iLen / 2]; 
55        for (int i = 0; i < iLen; i = i + 2) { 
56            String strTmp = new String(arrB, i, 2); 
57            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); 
58        } 
59        return arrOut; 
60    } 
61 
62    /** 
63      * 默认构造方法,使用默认密钥 
64      * 
65      * @throws Exception 
66      */ 
67    public DESPlus() throws Exception { 
68        this(strDefaultKey); 
69    } 
70 
71    /** 
72      * 指定密钥构造方法 
73      * 
74      * @param strKey 
75      *            指定的密钥 
76      * @throws Exception 
77      */ 
78    public DESPlus(String strKey) throws Exception { 
79        Security.addProvider(new com.sun.crypto.provider.SunJCE()); 
80        Key key = getKey(strKey.getBytes()); 
81 
82        encryptCipher = Cipher.getInstance("DES"); 
83        encryptCipher.init(Cipher.ENCRYPT_MODE, key); 
84 
85        decryptCipher = Cipher.getInstance("DES"); 
86        decryptCipher.init(Cipher.DECRYPT_MODE, key); 
87    } 
88 
89    /** 
90      * 加密字节数组 
91      * 
92      * @param arrB 
93      *            需加密的字节数组 
94      * @return 加密后的字节数组 
95      * @throws Exception 
96      */ 
97    public byte[] encrypt(byte[] arrB) throws Exception { 
98        return encryptCipher.doFinal(arrB); 
99    } 
100 
101    /** 
102      * 加密字符串 
103      * 
104      * @param strIn 
105      *            需加密的字符串 
106      * @return 加密后的字符串 
107      * @throws Exception 
108      */ 
109    public String encrypt(String strIn) throws Exception { 
110        return byteArr2HexStr(encrypt(strIn.getBytes())); 
111    } 
112 
113    /** 
114      * 解密字节数组 
115      * 
116      * @param arrB 
117      *            需解密的字节数组 
118      * @return 解密后的字节数组 
119      * @throws Exception 
120      */ 
121    public byte[] decrypt(byte[] arrB) throws Exception { 
122        return decryptCipher.doFinal(arrB); 
123    } 
124 
125    /** 
126      * 解密字符串 
127      * 
128      * @param strIn 
129      *            需解密的字符串 
130      * @return 解密后的字符串 
131      * @throws Exception 
132      */ 
133    public String decrypt(String strIn) throws Exception { 
134        return new String(decrypt(hexStr2ByteArr(strIn))); 
135    } 
136 
137    /** 
138      * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位 
139      * 
140      * @param arrBTmp 
141      *            构成该字符串的字节数组 
142      * @return 生成的密钥 
143      * @throws java.lang.Exception 
144      */ 
145    private Key getKey(byte[] arrBTmp) throws Exception { 
146        // 创建一个空的8位字节数组(默认值为0) 
147        byte[] arrB = new byte[8]; 
148 
149        // 将原始字节数组转换为8位 
150        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { 
151            arrB[i] = arrBTmp[i]; 
152        } 
153 
154        // 生成密钥 
155        Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); 
156 
157        return key; 
158    } 
159 } 

解决方案 »

  1.   

    参考 http://www.php.net/manual/en/ref.mcrypt.php 中 Wilmo 的回覆
    将 MCRYPT_RIJNDAEL_256 改成 MCRYPT_DES 即可使用 DES 来编解码$key = $_REQUEST['key'];//Encrypt Function (编码)
    function encrypt($encrypt) {
    global $key;
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
    $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt, MCRYPT_MODE_ECB, $iv);
    $encode = base64_encode($passcrypt);
    return $encode;
    }//Decrypt Function (解码)
    function decrypt($decrypt) {
    global $key;
    $decoded = base64_decode($decrypt);
    $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv);
    return $decrypted;
    }
      

  2.   

    新书推荐!清华出版社新书《搜索引擎零距离》 
    http://www.china-pub.com/195494
    本书作者主页: http://www.rayeen.com
     【内容简介】
    随着网络信息资源的急剧增长,人们越来越多地关注如何快速有效地从海量的网络信息中,抽取出潜在的、有价值的信息,使之有效地在管理和决策中发挥作用。搜索引擎技术解决了用户检索网络信息的困难,目前搜索引擎技术正成为计算机科学界和信息产业界争相研究、开发的对象。.本书的作者是一位资深的搜索引擎开发人员,书中对数据获取(网络信息挖掘)与数据检索(搜索引擎)两个方面作了深入的介绍。本书首先提出了一套“网络数据挖掘”的完整理论,并给出一个实际的智能爬虫系统,通过理论与实际的完整呈现,使读者能够对“网络数据挖掘”有一个比较具体的认识,然后介绍了一个专用程序语言IRS,并给出了这个语言的编译器以及虚拟机的实现方法。本书还通过对多个开源搜索引擎项目抽丝剥茧的细致分析,引出搜索引擎的一些基本原理与开发方法,并介绍了一个商业化搜索引擎的实例。本书的最后还结合一个Java框架介绍了一些软件设计思想。..本书涉及网络数据挖掘、搜索引擎原理、编译原理、数据库原理、正则表达式、软件工程、设计模式、Ruby语言、HTTP协议等计算机科学与技术的知识,适合搜索引擎开发人员作为参考,也适合有一定计算机基础的读者阅读,以扩展视野。本书的内容中,既有教科书式的理论阐述,也有“七天入门”式的实例解析,还有《Linux内核情景分析》风格的细致的代码分析,甚至还有一些英语文献翻译,从初学者到有一定经验的搜索引擎开发人员,各个层次的读者都能找到一些适合自己阅读的章节。...
      

  3.   

    to:RobinKin
    我的java DES加密里没有base64编码,我按你的说明,并且把base64_decode去掉与不去掉二种测试了,还是不行谢谢
      

  4.   

    key 跟IV 都是给定的值,不然JAVA跟PHP的加密解密能相同?