使用alipay.sdk生成RSA和RSA2加密解密算法工具类

2021/7/8 11:07:49

本文主要是介绍使用alipay.sdk生成RSA和RSA2加密解密算法工具类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、pom.xml中加入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/>
    </parent>
    
    <groupId>com.example</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis-plus</name>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- 根据自己需要引入相关版本依赖。 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.10</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.10</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <!-- 重点引入:阿里云支付SDK依赖包:https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.13.58.ALL</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、创建工具类

package com.ehe.elder.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class RsaUtils {

    //公钥
    public static String publicKey = "。。。。使用支付宝开放平台助手生成公钥。。。。。";

    //私钥
    public static String privateKey = "。。。使用支付宝开放平台助手生成私钥。。。。。。";
    
    /**
     * 字符集
     */
    public static final String CHAR_SET = "UTF-8";
    
    /**
     * 签名类型  RSA 或 RSA2
     */
    final String SIGN_TYPE = "RSA";

    /**
     * 签名类型  RSA 或 RSA2
     */
    final String SIGN_TYPE2 = "RSA2";


    /**
     * 加密
     * @param param 参数
     * @param publicKey 公钥
     * @param charSet 编码
     * @return 加密后的字符串
     * @throws AlipayApiException
     */
    public static String rsaEncrypt(String param,String publicKey,String charSet) throws AlipayApiException {
        String rsaEncrypt = AlipaySignature.rsaEncrypt(param, publicKey, charSet);
        log.info("加密后 :" + rsaEncrypt);
        return rsaEncrypt;
    }

    /**
     * 解密
     * @param rsaEncrypt 加密后的字符串
     * @param privateKey 私钥
     * @param charSet  字符编码
     * @return
     * @throws AlipayApiException
     */
    public static String rsaDecrypt(String rsaEncrypt,String privateKey,String charSet) throws AlipayApiException {
        String rsaDecrypt = AlipaySignature.rsaDecrypt(rsaEncrypt, privateKey, charSet);
        log.info("解密后 :" + rsaDecrypt);
        return rsaDecrypt;
    }

    /**
     * 加签,生成签名证书并拼接到参数中
     * @param paramMap
     * @param privateKey
     * @param charSet
     * @param singType
     * @return
     * @throws AlipayApiException
     */
    public static String rsaSign(Map<String,String> paramMap,String privateKey,String charSet,String singType) throws AlipayApiException {
        //拼接待加签的字符串
        String content = AlipaySignature.getSignContent(paramMap);
        //私钥
        String sign = AlipaySignature.rsaSign(content, privateKey, charSet, singType);
        log.info("当前签名 : " + sign);
        //添加签名到参数
        paramMap.put("sign", sign);
        return sign;
    }

    /**
     * 验签
     * @param paramMap 参数
     * @param publicKey 公钥
     * @param charSet 字符编码
     * @param singType 加密类型
     * @return
     * @throws AlipayApiException
     */
    public static Boolean rsaCheckV1(Map<String,String> paramMap,String publicKey,String charSet,String singType) throws AlipayApiException {
        //公钥-对数据验签
        boolean result = AlipaySignature.rsaCheckV1(paramMap, publicKey, charSet, singType);
        return result;
    }

}

3、使用支付宝开放平台助手下载
支付宝文档:https://opendocs.alipay.com/open/291/105971

windows下的开放平台助手下载地址:https://ideservice.alipay.com/ide/getPluginUrl.htm?clientType=assistant&platform=win&channelType=WEB
在这里插入图片描述
4、工具下载以后生成秘钥
在这里插入图片描述



这篇关于使用alipay.sdk生成RSA和RSA2加密解密算法工具类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程