des - Quelqu'un qui connaît PHP et Java arrive et souhaite poser une question.
高洛峰
高洛峰 2017-05-18 10:45:52
0
2
655

1 Lorsque je fais des affaires avec d'autres sociétés, l'autre partie utilise Java et j'utilise PHP.
2 Les deux parties chiffreront d'abord les données avec DES, puis avec le chiffrement base64. Transmettre
3 L'autre partie a envoyé un cryptage et un décryptage Java, je devrais le suivre et en faire un php. Mais je n'arrive pas à comprendre le code Java.
4 J'espère que Dieu pourra être occupé, merci beaucoup.

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);
    }
}
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

répondre à tous(2)
phpcn_u1582

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

Essayez d'utiliser ceci. Cela n'a pas grand-chose à voir avec le code Java qu'il utilise. Il vous suffit d'implémenter sa logique de cryptage.

Décoder d'abord en base64 puis décrypter à l'aide de l'algorithme des

我想大声告诉你

Ensuite, vous devriez lui demander directement de vous fournir une version PHP

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!