search

Home  >  Q&A  >  body text

des - Someone who knows php and java comes in and wants to ask a question.

1 Do business with other companies, the other party is java, I am php.
2 Both parties encrypt the data with DES first and then base64 encryption. Transmit
3 The other party sent a java encryption and decryption, I should follow it and make a php one. But I can't understand the java code.
4 I hope God can be busy, thank you very much.

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:\testxml\requestAppPolInp881.xml";
        String path1 = path;
        // 初始化文件对象f
        File f = new File(path1);
        // 初始化读数据流对象reader
        InputStreamReader reader = new InputStreamReader(new FileInputStream(
                path1), encoding);
        // 根据f文件长度初始化字符串数据c[]
        char c[] = new char[(int) (f.length())];
        // 取到字符串长度,并将文件f内容写入数组c
        int length = reader.read(c);
        // 逐字节将字符串数组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 根据键值进行加密
     * 
     * @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);
    }
}
高洛峰高洛峰2752 days ago716

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-18 10:47:52

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

    Try using this. It has little to do with the java code he uses. You just need to implement his encryption logic.

    First base64 decode and then decrypt using des algorithm

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-18 10:47:52

    Then you should ask him directly to provide you with a PHP version

    reply
    0
  • Cancelreply