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

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:\\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);
    }
}
尚方宝剑之说
浏览 383回答 2
2回答

慕妹3242003

已解决 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; } }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java