开发者社区> 问答> 正文

懂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);
}

}

" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/3e1a6c3761a54afba05b93ba2793612c.png)

展开
收起
因为相信,所以看见。 2020-05-26 13:57:04 678 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    "

    <a href=""https://github.com/sjclijie/php-des"" rel=""nofollow noreferrer"">https://github.com/sjclijie/p...

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

    先 base64 decode 然后用des算法解密

    ######

    已解决

    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;
        }
    }
    " ![image.png](https://ucc.alicdn.com/pic/developer-ecology/0332f43c26714d129f3999425907b9c0.png)
    2020-05-27 10:06:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载