您的位置:首页技术文章
文章详情页

des - 懂php和java的进来,求教一个问题

【字号: 日期:2023-11-22 14:41:00浏览:53作者:猪猪

问题描述

1 和别的公司对接业务,对方java,我是php。2 双方把把数据先DES加密,再base64加密。进行传输3 对方发了一个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;/** * 对外接口数据加密/解密类 * @author xin * */public class DesUtil { private final static String DES = 'DES'; public static void main(String[] args) throws Exception {String tDoc = '';// 请求报文String encoding = 'GBK';// 将函数参数赋给本地参数String path = '/Users/jieliu/Code/a.txt';// String path = 'F:testxmlrequestAppPolInp881.xml';String path1 = path;// 初始化文件对象fFile f = new File(path1);// 初始化读数据流对象readerInputStreamReader reader = new InputStreamReader(new FileInputStream(path1), encoding);// 根据f文件长度初始化字符串数据c[]char c[] = new char[(int) (f.length())];// 取到字符串长度,并将文件f内容写入数组cint length = reader.read(c);// 逐字节将字符串数组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 根据键值进行加密 * * @param data * @param key * 加密键byte数组 * @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 根据键值进行解密 * * @param data * @param key * 加密键byte数组 * @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); }/** * 根据键值解密并返回指定编码方式字符串 * @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 根据键值进行加密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception {// 生成一个可信任的随机数源SecureRandom sr = new SecureRandom();// 从原始密钥数据创建DESKeySpec对象DESKeySpec dks = new DESKeySpec(key);// 创建一个密钥工厂,然后用它把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 根据键值进行解密 * * @param data * @param key * 加密键byte数组 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception {// 生成一个可信任的随机数源SecureRandom sr = new SecureRandom();// 从原始密钥数据创建DESKeySpec对象DESKeySpec dks = new DESKeySpec(key);// 创建一个密钥工厂,然后用它把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); }}

问题解答

回答1:

https://github.com/sjclijie/p...

用这个试试吧,这和他用的什么java代码关系不大,你实现他的加密逻辑就可以了。

先 base64 decode 然后用des算法解密

回答2:

那你该直接找他给你出个PHP版本的

标签: java