使用支付寶SDK進行RSAj加簽驗籤以及加密解密

背景

支付寶開元SDK中提供了RSA工具類,能夠方便的對數據進行加解密,經測試演示版本sdk支持分段加解密,而且提供了加簽與驗籤的經常使用方法。並支持RSA與RSA2兩種類的簽名類型。java

注:生產RSA簽名請參考 https://docs.open.alipay.com/291/105971maven

Maven 支付寶SDK依賴

注:maven中央庫直達鏈接 https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java工具

<dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>3.7.4.ALL</version>
  </dependency>

RSA普通加密解密示例

1.調用 AlipaySignature.rsaEncrypt() 方法加密
2.調用 AlipaySignature.rsaDecrypt() 方法解密測試

public static void main(String[] args) throws Exception {

        //公鑰
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDp9FaWlZc/doHX" +
                "il60NcJB56trQOoYSMx5LUgBr+rGANJNBJvUJOI3Jqfs5axf4Lx/JqlhGcmlBuNE" +
                "gt16iEwwBLj6GSpHIm+qZ/bT1xHqJqJzS55PUu6JNhZsV6ERz/tvs3dITuTa0zxT" +
                "CSX4c9DniIF0VuWCLgEvRVuI2pqBYQIDAQAB";

        //私鑰
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOn0VpaVlz92gdeK" +
                "XrQ1wkHnq2tA6hhIzHktSAGv6sYA0k0Em9Qk4jcmp+zlrF/gvH8mqWEZyaUG40SC" +
                "3XqITDAEuPoZKkcib6pn9tPXEeomonNLnk9S7ok2FmxXoRHP+2+zd0hO5NrTPFMJ" +
                "Jfhz0OeIgXRW5YIuAS9FW4jamoFhAgMBAAECgYBS+Rg4gcdJd7MnCWj7CtHhY9OU" +
                "z0n24OK720ZdatGd9a6gKBFIFOix3KDT0MTooCDaC5Eo4ed1AtNZefwot+wksf2R" +
                "dXgoAf/uS1GpxbzXhzFHmfDBOf7yKRXm4K/xzVWu7+1DI2siGwuQ6GWfyhZlGCgj" +
                "DASHmSYOAidG8t6KQQJBAPeotqqoY0Q7yICWyRbJIVgtABRxSR6xE+4fGjHev219" +
                "pYopK7r0KRQsyAgHsFoFwzx9Rzr8YG85LBQ7lAeenzkCQQDx1XchnIRRuAl5f9EL" +
                "QuZjz8orT08nKhrM2aNswfmY5UC7amJPvPSZihYGO+V75NBGii6glG1Fskp+o4Hb" +
                "kctpAkA/DChUkcGToVSNx9Uxg8qFVA9tJNnkglf8itZ0U6yQByiZX5nOYlIC3NOf" +
                "l7WBdW4afBoiHhW+uuNJ01EAD5WhAkANBpx2GQIfjLQl8TaHTsEuGyEemBUBXQk4" +
                "g3iEASa6j46fCanl9Z2PL5rkFxHTTI3TXtoIGDaSjHnqvE/gTcgRAkEA92TtlNrc" +
                "VMm7ZkXz26YXkJ5dLDAFB7X8GxZU7DBJ3nFU8Ezj3V5y+5ka494A7/8WVYZUpnm9" +
                "KspvMmNNK4l/lQ==";


        String content = "123456";

        //公鑰-加密
        String rsaEncrypt = AlipaySignature.rsaEncrypt(content, publicKey, "UTF-8");
        //私鑰-解密
        String rsaDecrypt = AlipaySignature.rsaDecrypt(rsaEncrypt, privateKey, "UTF-8");

        System.out.println("加密前長度 :" + content.length());
        System.out.println("加密後 :" + rsaEncrypt);
        System.out.println("解密後 :" + rsaDecrypt);
        System.out.println("解密後長度 :" + rsaDecrypt.length());
    }

結果
在這裏插入圖片描述編碼

RSA加簽驗籤示例

/** * 支付寶 sdk 加簽驗籤測試 * * @author 碼農猿 */
public class SignDemo {

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

    /** * 私鑰 */
    private static final String PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCcttLg0oHgrqVLACu45uSyomlyuzu3zDnVjfwj2LH2vtYIEyICNHFa7wM2eva3nJr9deiKgerIwfegSSKqxSHcVfWkb/g3/3+FG0UMmqmQKjhn8PvrAR6Fj7L6TywYQ7507ybxs1uZ+lwgYPC4+XepUEV7IlDA+C65Z3fb/G3MtxiTYL8R5zbn74SUehsa8uRFMceVd5n6/whgIg93TnxtCyycUzDuef4uCvaRAr/aA9DyBezyaLXvXaL92CijxvPD2aHOQWdZcTFGrOChcfkN8gxaeVFHiXW7apdc9T6EgoW5rbvvqodESZ6o+QqIcDbOgxz0tXJ1UNMai8mNKfTTAgMBAAECggEAaWS+/7oy5JMv0Pflb63Awq5dcm6fI+XyQ0ABAW6biREgyj9r0MXKMlip9XrO7/8fcTRZ1sS2zJs+WQq2iNiZBCC/Wf5/ldF7xl3nmylsverXEnhMQ+j1yOcyfArA4fS1YtjvfP+drDlmudPYMN59bl3wzHXwj0aZzdDbGBR1F4Y+CzgRObq5092acJz/foUO5SAg/+KLhmQQ28AvBhXPTgV467ER34pDHexCgAnsLsH4KVCPxsbW0eh7vaIgUjWQ/sTAfRzFW5CLJRYO1xH6mDS42dImiUJBiWiz2fK4FRrSM05QXCHkKfXb1DpMnvrNyHyCDZfshRp7UsZoxtVeaQKBgQDvWrX7JNb3Km5JncdKRq+I8lqx+d750Ffj0Ln2wTR0lahonJyF+YL4yuP5R8+sCeFRjcPWMUkZ2oSxxT6gaHRl0Z2fJXqwuCaO9NKq9C03olPmiFXM/H4knXsr1urfcRmE5sg3k0kcmtDnxoyDBBb3w8SRb8p0jqRl/NspDq9IJwKBgQCnnNj/JgCZJkWnK7JPTqnjTlavkV1Zicz89dvQ9Eq7Ff0e/BJiEi+DrndSP2v2YOZEJr0LOpIxJwaNCgea67t055/IlsBqHBu0cBtGQ7i2ykd/3Q1v+q0dEKf3C92lvwcsLRQo01gjnDMRiiSkIf0cYdxs66wgzAxh4xd9BvwNdQKBgAEVrydxNkwudAt/XIzYnykGuCSAVmNZb1yH8J/Oplc06mt28jqlM9O+z6OskKNd9BhzhQSuen9Ufy9zDmKZtpVTitxSxiiQ3RPeximiK6ZJ5QlxarogFs5BrHI5ah0THSN+DEA8OaOYjAPQ4Ygid5wt1fE2yXsXvmT77V5VQ7QXAoGALDRNdl0LY1iYnhIEIK5aV7xdWEg6GlchXMVqh50l6FlQPE+2eW7aYRwuE97uFjhQAkFFMiTsUVI9hAzVHKJ2+cnsdfZsII/xLpEyYEGUAYEvgiVGWfX+md++rITQm9nZhmkNHAdBA4M6ZLHOIAtmuYmFnKQ67RtjWJ2PoEWrS9ECgYBW6QtwiRxoz/Xes9jHRmShVcDfwq210Qubmd81uUtDy/lVOY99qCb4rNsTu3RVrtMubD9syvivDroE6NtFrDBNHAtRaGB77Ivs+WdMdhDzNwGxVfgt6h/qJoY05+NmVPe3ExtC2fs8EWnZkXPbs2qurFxdvhBjxHIQHQwV54GaZQ==";

    /** * 字符集 */
    private static final String CHAR_SET = "UTF-8";
    /** * 簽名類型 RSA 或 RSA2 */
    private static final String SIGN_TYPE = "RSA2";


    public static void main(String[] args) {

        //建立參數
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("params1", "value..1..");
        paramMap.put("params2", "value..2..");
        paramMap.put("params3", "value..3..");
        paramMap.put("params4", "value..4..");

        try {

            //拼接待加簽的字符串
            String content = AlipaySignature.getSignContent(paramMap);

            //私鑰-對數據加簽
            String sign = AlipaySignature.rsaSign(content, PRIVATE_KEY, CHAR_SET, SIGN_TYPE);

            //添加簽名到參數
            paramMap.put("sign", sign);

            //公鑰-對數據驗籤
            boolean result = AlipaySignature.rsaCheckV1(paramMap, PUBLIC_KEY, CHAR_SET, SIGN_TYPE);

            System.out.println("簽名類型 : " + SIGN_TYPE + " 編碼規則: " + CHAR_SET);
            System.out.println("拼接待加簽的字符串 : " + content);
            System.out.println("當前簽名 : " + sign);
            System.out.println("驗簽結果 : " + result);

        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }

}

在這裏插入圖片描述

RSA與RSA2

如下介紹來自支付寶開放平臺
在這裏插入圖片描述加密

相關文章
相關標籤/搜索