1 Apabila menjalankan perniagaan dengan syarikat lain, pihak lain menggunakan Java dan saya menggunakan PHP.
2 Kedua-dua pihak akan menyulitkan data dengan DES dahulu dan kemudian penyulitan base64. Hantar
3 Pihak lain menghantar penyulitan dan penyahsulitan java, saya harus mengikutinya dan membuat satu php. Tetapi saya tidak dapat memahami kod java.
4 Semoga Allah sibukkan, terima kasih banyak-banyak.
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;
/**
* 對外接口數(shù)據(jù)加密/解密類
* @author xin
*
*/
public class DesUtil {
private final static String DES = "DES";
public static void main(String[] args) throws Exception {
String tDoc = "";// 請求報文
String encoding = "GBK";
// 將函數(shù)參數(shù)賦給本地參數(shù)
String path = "/Users/jieliu/Code/a.txt";
// String path = "F:\testxml\requestAppPolInp881.xml";
String path1 = path;
// 初始化文件對象f
File f = new File(path1);
// 初始化讀數(shù)據(jù)流對象reader
InputStreamReader reader = new InputStreamReader(new FileInputStream(
path1), encoding);
// 根據(jù)f文件長度初始化字符串數(shù)據(jù)c[]
char c[] = new char[(int) (f.length())];
// 取到字符串長度,并將文件f內容寫入數(shù)組c
int length = reader.read(c);
// 逐字節(jié)將字符串數(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ù)鍵值進行加密
*
* @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ù)鍵值進行解密
*
* @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ù)鍵值進行加密
*
* @param data
* @param key
* 加密鍵byte數(shù)組
* @return
* @throws Exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數(shù)源
SecureRandom sr = new SecureRandom();
// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對象
DESKeySpec dks = new DESKeySpec(key);
// 創(chuàng)建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher對象實際完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密鑰初始化Cipher對象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
/**
* Description 根據(jù)鍵值進行解密
*
* @param data
* @param key
* 加密鍵byte數(shù)組
* @return
* @throws Exception
*/
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
// 生成一個可信任的隨機數(shù)源
SecureRandom sr = new SecureRandom();
// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對象
DESKeySpec dks = new DESKeySpec(key);
// 創(chuàng)建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher對象實際完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密鑰初始化Cipher對象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
}
擁有18年軟件開發(fā)和IT教學經驗。曾任多家上市公司技術總監(jiān)、架構師、項目經理、高級軟件工程師等職務。 網(wǎng)絡人氣名人講師,...
https://github.com/sjclijie/p...
Cuba gunakan ini. Ia mempunyai sedikit kaitan dengan kod java yang dia gunakan. Anda hanya perlu melaksanakan logik penyulitannya.
Nyahkod base64 pertama dan kemudian menyahsulit menggunakan algoritma des
Maka anda harus memintanya secara langsung untuk memberikan anda versi PHP