Java those things

JAVA RSA签名 解签(利用支付宝封装的函数)

2018-09-04  本文已影响31人  M_ENG
package com.mlgd.api.util;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取token接口中sign签名验签Demo
 * 算法说明:
 * 使用SHA256签名算法和RSA2048非对称算法加密
 * 
 * @author Administrator
 */
public class SignDemo {

    /**
     * 公钥
     */
    public static String publicKey_App2048 = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnLbS4NKB4K6lSwAruObksqJpcrs7t8w51Y38I9ix9r7WCBMiAjRxWu8DNnr2t5ya/XXoioHqyMH3oEkiqsUh3FX1pG/4N/9/hRtFDJqpkCo4Z/D76wEehY+y+k8sGEO+dO8m8bNbmfpcIGDwuPl3qVBFeyJQwPguuWd32/xtzLcYk2C/Eec25++ElHobGvLkRTHHlXeZ+v8IYCIPd058bQssnFMw7nn+Lgr2kQK/2gPQ8gXs8mi1712i/dgoo8bzw9mhzkFnWXExRqzgoXH5DfIMWnlRR4l1u2qXXPU+hIKFua2776qHREmeqPkKiHA2zoMc9LVydVDTGovJjSn00wIDAQAB";

    /**
     * 私钥
     */
    public static String privateKey_App2048 = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCcttLg0oHgrqVLACu45uSyomlyuzu3zDnVjfwj2LH2vtYIEyICNHFa7wM2eva3nJr9deiKgerIwfegSSKqxSHcVfWkb/g3/3+FG0UMmqmQKjhn8PvrAR6Fj7L6TywYQ7507ybxs1uZ+lwgYPC4+XepUEV7IlDA+C65Z3fb/G3MtxiTYL8R5zbn74SUehsa8uRFMceVd5n6/whgIg93TnxtCyycUzDuef4uCvaRAr/aA9DyBezyaLXvXaL92CijxvPD2aHOQWdZcTFGrOChcfkN8gxaeVFHiXW7apdc9T6EgoW5rbvvqodESZ6o+QqIcDbOgxz0tXJ1UNMai8mNKfTTA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/Xes9jHRmShVcDfwq210Qubmd81uUtDy/lVOY99qCb4rNsTu3RVrtMubD9syvivDroE6NtFrDBNHAtRaGB77Ivs+WdMdhDzNwGxVfgt6h/qJoY05+NmVPe3ExtC2fs8EWnZkXPbs2qurFxdvhBjxHIQHQwV54GaZQ==";

    /**
     * 字符集
     */
    private static String CHARSET = "UTF-8";

    public static void main(String[] args) {

        /*
         * 接口请求参数,注意  所有参数名称全部为小写
         */
        Map<String, String> map = new HashMap<String, String>();

        //系统参数
        map.put("appid", "123456789");// 授权用户名(固定值)

        map.put("timestamp", "20180801050402");// 请求时间戳,时区为GMT+8(北京时间)



        /*
         * 业务参数封装成bizcontent, 格式为json字符串
         */
        Map<String, Object> bizmap = new HashMap<String, Object>();

        bizmap.put("deviceid", "66666666");// 设备编号

        String bizcontent = JSON.toJSONString(bizmap);
        
        System.out.println("业务参数JSON格式字符串:"+bizcontent);

        map.put("bizcontent", bizcontent);//此处很重要,一定要传递bizcontent
        
        try {
            /*
             * 拼接待签名字符串  RSA2对应的函数
             */
            String content = AlipaySignature.getSignCheckContentV2(map);

            System.out.println("拼接待签名字符串:"+content);


            /*
             * 对数据签名  RSA2对应的函数和密钥
             */
            String sign = AlipaySignature.rsa256Sign(content, privateKey_App2048, CHARSET);

            System.out.println("原始sign:" + sign);

            map.put("sign", sign);

            /*
             * 进入验签流程
             */
            boolean result = AlipaySignature.rsaCheckV2(map, publicKey_App2048, "UTF-8", "RSA2");

            System.out.println("验签结果:" + result);
            
        }
        catch (AlipayApiException e)
        {
            e.printStackTrace();
        }
    }

}

上一篇下一篇

猜你喜欢

热点阅读