des - 懂php和java的進(jìn)來,求教一個(gè)問題
問題描述
1 和別的公司對(duì)接業(yè)務(wù),對(duì)方j(luò)ava,我是php。2 雙方把把數(shù)據(jù)先DES加密,再base64加密。進(jìn)行傳輸3 對(duì)方發(fā)了一個(gè)java的加密解密,我應(yīng)該照著做一個(gè)php的。但看不懂java代碼。4 求大神能忙,萬謝。
package com.ab.mediation.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** * 對(duì)外接口數(shù)據(jù)加密/解密類 * @author xin * */public class DesUtil { private final static String DES = 'DES'; public static void main(String[] args) throws Exception {String tDoc = '';// 請(qǐng)求報(bào)文String encoding = 'GBK';// 將函數(shù)參數(shù)賦給本地參數(shù)String path = '/Users/jieliu/Code/a.txt';// String path = 'F:testxmlrequestAppPolInp881.xml';String path1 = path;// 初始化文件對(duì)象fFile f = new File(path1);// 初始化讀數(shù)據(jù)流對(duì)象readerInputStreamReader reader = new InputStreamReader(new FileInputStream(path1), encoding);// 根據(jù)f文件長度初始化字符串?dāng)?shù)據(jù)c[]char c[] = new char[(int) (f.length())];// 取到字符串長度,并將文件f內(nèi)容寫入數(shù)組cint length = reader.read(c);// 逐字節(jié)將字符串?dāng)?shù)組c[],賦給變量tDocfor (int i = 0; i < length; i++) { tDoc = tDoc + c[i];}String key = '12dc293d43db3b237849';System.out.println(encrypt(tDoc, key));System.out.println(decrypt(encrypt(tDoc, key), key)); } /** * Description 根據(jù)鍵值進(jìn)行加密 * * @param data * @param key * 加密鍵byte數(shù)組 * @return * @throws Exception */ public static String encrypt(String data, String key) throws Exception {byte[] bt = encrypt(data.getBytes(), key.getBytes());String strs = new BASE64Encoder().encode(bt);return strs; }/** * 指定字符編碼方式并加密 * @param data * @param key * @param encoding * @return * @throws Exception */ public static String encrypt(String data, String key, String encoding) throws Exception {byte[] bt = encrypt(data.getBytes(encoding), key.getBytes());String strs = new BASE64Encoder().encode(bt);return strs; } /** * Description 根據(jù)鍵值進(jìn)行解密 * * @param data * @param key * 加密鍵byte數(shù)組 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, String key) throws IOException, Exception {if (data == null) return null;BASE64Decoder decoder = new BASE64Decoder();byte[] buf = decoder.decodeBuffer(data);byte[] bt = decrypt(buf, key.getBytes());return new String(bt); }/** * 根據(jù)鍵值解密并返回指定編碼方式字符串 * @param data * @param key * @param encoding * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, String key, String encoding) throws IOException,Exception {if (data == null) return null;BASE64Decoder decoder = new BASE64Decoder();byte[] buf = decoder.decodeBuffer(data);byte[] bt = decrypt(buf, key.getBytes());return new String(bt, encoding); } /** * Description 根據(jù)鍵值進(jìn)行加密 * * @param data * @param key * 加密鍵byte數(shù)組 * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception {// 生成一個(gè)可信任的隨機(jī)數(shù)源SecureRandom sr = new SecureRandom();// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象DESKeySpec dks = new DESKeySpec(key);// 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec轉(zhuǎn)換成SecretKey對(duì)象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks);// Cipher對(duì)象實(shí)際完成加密操作Cipher cipher = Cipher.getInstance(DES);// 用密鑰初始化Cipher對(duì)象cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);return cipher.doFinal(data); } /** * Description 根據(jù)鍵值進(jìn)行解密 * * @param data * @param key * 加密鍵byte數(shù)組 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception {// 生成一個(gè)可信任的隨機(jī)數(shù)源SecureRandom sr = new SecureRandom();// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象DESKeySpec dks = new DESKeySpec(key);// 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec轉(zhuǎn)換成SecretKey對(duì)象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks);// Cipher對(duì)象實(shí)際完成解密操作Cipher cipher = Cipher.getInstance(DES);// 用密鑰初始化Cipher對(duì)象cipher.init(Cipher.DECRYPT_MODE, securekey, sr);return cipher.doFinal(data); }}
問題解答
回答1:https://github.com/sjclijie/p...
用這個(gè)試試吧,這和他用的什么java代碼關(guān)系不大,你實(shí)現(xiàn)他的加密邏輯就可以了。
先 base64 decode 然后用des算法解密
回答2:那你該直接找他給你出個(gè)PHP版本的
