1 和別的公司對(duì)接業(yè)務(wù),對(duì)方j(luò)ava,我是php。
2 雙方把資料先DES加密,然後base64加密。進(jìn)行傳輸
3 對(duì)方發(fā)了一個(gè)java的加密解密,我應(yīng)該照著做一個(gè)php的。但看不懂java程式碼。
4 求大神能忙,萬(wàn)謝。
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:\testxml\requestAppPolInp881.xml";
String path1 = path;
// 初始化文件對(duì)象f
File f = new File(path1);
// 初始化讀數(shù)據(jù)流對(duì)象reader
InputStreamReader reader = new InputStreamReader(new FileInputStream(
path1), encoding);
// 根據(jù)f文件長(zhǎng)度初始化字符串?dāng)?shù)據(jù)c[]
char c[] = new char[(int) (f.length())];
// 取到字符串長(zhǎng)度,并將文件f內(nèi)容寫入數(shù)組c
int length = reader.read(c);
// 逐字節(jié)將字符串?dāng)?shù)組c[],賦給變量tDoc
for (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);
}
}
擁有18年軟件開(kāi)發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級(jí)軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...
https://github.com/sjclijie/p...
用這個(gè)試試吧,這和他用的什麼java程式碼關(guān)係不大,你實(shí)作他的加密邏輯就可以了。
先 base64 decode 再用des演算法解密