1 和別的公司對接業(yè)務(wù),對方j(luò)ava,我是php。2 雙方把把數(shù)據(jù)先DES加密,再base64加密。進行傳輸3 對方發(fā)了一個java的加密解密,我應該照著做一個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;
/**
* 對外接口數(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內(nèi)容寫入數(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轉(zhuǎn)換成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轉(zhuǎn)換成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);
}
}
2 回答

慕妹3242003
TA貢獻1824條經(jīng)驗 獲得超6個贊
已解決
class DES {
protected $cipher; //密碼
protected $mode; //模式
private $_secret_key; //密鑰
public function __construct($key, $cipher=MCRYPT_TRIPLEDES, $mode=MCRYPT_MODE_ECB) {
$this->_secret_key = $key;
$this->cipher = $cipher;
$this->mode = $mode;
}
public function encrypt($str, $padding = true) {
if ($padding) {
$size = mcrypt_get_block_size($this->cipher, $this->mode);
$pad = $size - (strlen($str) % $size);
$str .= str_repeat(chr($pad), $pad);
}
$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $this->_secret_key, $iv);
$data = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return base64_encode($data);
}
public function decrypt($str, $padding = true) {
$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $this->_secret_key, $iv);
$data = mdecrypt_generic($td, base64_decode($str));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
if ($padding) {
$pad = ord($data{strlen($data) - 1});
return substr($data, 0, -1 * $pad);
}
return $data;
}
}
添加回答
舉報
0/150
提交
取消