博主首页

加解密demo(java、php)

数据格式

     * @param args
     * 撞库---入参加密字段signs加密前格式
     *
     * {
     *     "mobileMask": "134123412**",
     *     "city": "武汉",
     *     "system": "qxh"
     * }
     *
     * 撞库---返回加密字段signs加密前格式
     * {
     *     "md5List": [
     *         "01f32b38b7b208a574c512a40d30cff0",
     *         "01f32b38b7b208a574c512a40d30cff0"
     *     ]
     * }

java版

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo {
    /**
     * 引入依赖
     * <dependency>
         * <groupId>cn.hutool</groupId>
         * <artifactId>hutool-all</artifactId>
         * <version>5.6.5<</version>
     * </dependency>
     *
     * @param args
     * 撞库---入参加密字段signs加密前格式
     *
     * {
     *     "mobileMask": "134123412**",
     *     "city": "武汉",
     *     "system": "qxh"
     * }
     *
     * 撞库---返回加密字段signs加密前格式
     * {
     *     "md5List": [
     *         "01f32b38b7b208a574c512a40d30cff0",
     *         "01f32b38b7b208a574c512a40d30cff0"
     *     ]
     * }
     */
    public static void main(String[] args) {
        //撞库-----------------------------------------------------------------
        String checkUrl = "撞库url";
        String timestamp = String.valueOf(System.currentTimeMillis());
        String key = String.format("%s%s", "MCXBIjANke", timestamp).substring(0, 16);
        CheckVO checkVO = new CheckVO();
        checkVO.setMobileMask("134123412**")
                .setCity("武汉")
                .setSystem("qxh");
        String signs = SecureUtil.aes(key.getBytes(StandardCharsets.UTF_8)).encryptHex(JSONUtil.toJsonStr(checkVO));

        Map<String, Object> map = new HashMap<>();
        map.put("timestamp", timestamp);
        map.put("signs", signs);
        String resultData = HttpUtil.createPost(checkUrl).contentType("application/json")
                .body(JSONUtil.toJsonPrettyStr(map)).timeout(5000).execute().body();
        //请求完成---------------解析结果
        JSONObject jsonObject = JSONUtil.parseObj(resultData);
        String result = jsonObject.getStr("result");
        JSONObject data = jsonObject.getJSONObject("data");
        String signsReturn = data.getStr("signs");
        //-----------解密
        String decryptStr = SecureUtil.aes(key.getBytes(StandardCharsets.UTF_8)).decryptStr(signsReturn);
        JSONObject md5Map = JSONUtil.parseObj(decryptStr);
        JSONArray md5Array = md5Map.getJSONArray("md5List");
        List<String> md5List = md5Array.toList(String.class);
        System.out.println(JSONUtil.toJsonStr(md5List));
        //撞库-----------------------------------------------------------------

    }


    @Data
    @Accessors(chain = true)
    public static class CheckVO {
        private String mobileMask;
        private String city;
        private String system;
    }
}
View Code

php版

代码在线运行网址 https://www.cainiaojc.com/tool/php/

<?php
class AES {
    /**
 *
 * @param string $string 需要加密的字符串
 * @param string $key 密钥
 * @return string
 */ public static function encrypt($string, $key)
    {
        // 对接java,服务商做的AES加密通过SHA1PRNG算法(只要password一样,每次生成的数组都是一样的),Java的加密源码翻译php如下:
        // $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        // openssl_encrypt 加密不同Mcrypt,对秘钥长度要求,超出16加密结果不变
        $data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        $data = strtoupper(bin2hex($data)); return $data;
    }
    /**
    * @param string $string 需要解密的字符串
* @param string $key 密钥36b2539e9f1a09a5e8553d501022fc55
* @return string
    */
    public static function decrypt($string, $key)
    {
        // 对接java,服务商做的AES加密通过SHA1PRNG算法(只要password一样,每次生成的数组都是一样的),Java的加密源码翻译php如下:
         //$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
         $decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA); return $decrypted;
    }
}
$encrypt = AES::encrypt('{"md5List":[]}', 'MCXBIjANke172760');
$decrypt = AES::decrypt($encrypt, 'MCXBIjANke172760');
echo "加密后:".$encrypt."\n";
echo "解密:".$decrypt;
 
?>

 

posted @ 2024-10-01 11:09  笑~笑  阅读(21)  评论(0编辑  收藏  举报