• RSA之前端加密后端解密


    RSA之前端加密后端解密

    RSA加密解密方式有:
    (1)公钥加密,私钥解密;
    (2)私钥加密,公钥解密;
    此文章中以下我使用的是前端公钥加密,后端私钥解密

    秘钥对生成

    http://web.chacuo.net/netrsakeypair
    生成对应的公钥和私钥
    在这里插入图片描述

    前端公钥加密

    前端加密js文件
    链接: https://pan.baidu.com/s/1NIMayTcmjbMOf7BqPhPQoA 提取码: t7an
    下载js文件并引入

    <script th:src="@{/js/jsencrypt.min.js}"></script>
    <script type="text/javascript">
    function rsaPassword() {
    	var encryptor = new JSEncrypt();
    	encryptor.setPublicKey('公钥字符串'); // 设置公钥
    	var rsaPassword=encryptor.encrypt('要加密的字符串');
    }
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    后端私钥解密

    示例

    String encryptPassword=RsaUtil.encryptByPublicKey("要加密的字符串","公钥字符串");
    System.out.println("加密:"+encryptPassword);
    String decryptPassword=RsaUtil.decryptByPrivateKey(encryptPassword,"私钥字符串");
    System.out.println("解密:"+decryptPassword);
    
    • 1
    • 2
    • 3
    • 4

    后端加解密工具类

    import javax.crypto.Cipher;
    import java.security.*;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import java.util.Base64;
    import java.util.HashMap;
    import java.util.Map;
    
    
    /**
     * @author 初颜sir
     */
    public class RsaUtil {
    
        //签名算法名称
        private static final String RSA_KEY_ALGORITHM = "RSA";
    
        //RSA密钥长度,默认密钥长度是1024,密钥长度必须是64的倍数,在512到65536位之间,不管是RSA还是RSA2长度推荐使用2048
        private static final int KEY_SIZE = 2048;
    
        /**
         * 公钥加密(用于数据加密)
         *
         * @param data         加密前的字符串
         * @param publicKeyStr base64编码后的公钥
         * @return base64编码后的字符串
         * @throws Exception
         */
        public static String encryptByPublicKey(String data, String publicKeyStr) throws Exception {
            //Java原生base64解码
            byte[] pubKey = Base64.getDecoder().decode(publicKeyStr);
            //创建X509编码密钥规范
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);
            //返回转换指定算法的KeyFactory对象
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
            //根据X509编码密钥规范产生公钥对象
            PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
            //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式)
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            //用公钥初始化此Cipher对象(加密模式)
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            //对数据加密
            byte[] encrypt = cipher.doFinal(data.getBytes());
            //返回base64编码后的字符串
            return Base64.getEncoder().encodeToString(encrypt);
        }
    
    
        /**
         * 私钥解密(用于数据解密)
         *
         * @param data          解密前的字符串
         * @param privateKeyStr 私钥
         * @return 解密后的字符串
         * @throws Exception
         */
        public static String decryptByPrivateKey(String data, String privateKeyStr) throws Exception {
            //Java原生base64解码
            byte[] priKey = Base64.getDecoder().decode(privateKeyStr);
            //创建PKCS8编码密钥规范
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKey);
            //返回转换指定算法的KeyFactory对象
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
            //根据PKCS8编码密钥规范产生私钥对象
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式)
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            //用私钥初始化此Cipher对象(解密模式)
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            //对数据解密
            byte[] decrypt = cipher.doFinal(Base64.getDecoder().decode(data));
            //返回字符串
            return new String(decrypt);
        }
    
        
    
        /**
         * 私钥加密(用于数据签名)
         *
         * @param data          加密前的字符串
         * @param privateKeyStr base64编码后的私钥
         * @return base64编码后后的字符串
         * @throws Exception
         */
        public static String encryptByPrivateKey(String data, String privateKeyStr) throws Exception {
            //Java原生base64解码
            byte[] priKey = Base64.getDecoder().decode(privateKeyStr);
            //创建PKCS8编码密钥规范
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKey);
            //返回转换指定算法的KeyFactory对象
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
            //根据PKCS8编码密钥规范产生私钥对象
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式)
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            //用私钥初始化此Cipher对象(加密模式)
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            //对数据加密
            byte[] encrypt = cipher.doFinal(data.getBytes());
            //返回base64编码后的字符串
            return Base64.getEncoder().encodeToString(encrypt);
        }
    
        /**
         * 公钥解密(用于数据验签)
         *
         * @param data         解密前的字符串
         * @param publicKeyStr base64编码后的公钥
         * @return 解密后的字符串
         * @throws Exception
         */
        public static String decryptByPublicKey(String data, String publicKeyStr) throws Exception {
            //Java原生base64解码
            byte[] pubKey = Base64.getDecoder().decode(publicKeyStr);
            //创建X509编码密钥规范
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);
            //返回转换指定算法的KeyFactory对象
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
            //根据X509编码密钥规范产生公钥对象
            PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
            //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式)
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            //用公钥初始化此Cipher对象(解密模式)
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            //对数据解密
            byte[] decrypt = cipher.doFinal(Base64.getDecoder().decode(data));
            //返回字符串
            return new String(decrypt);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
  • 相关阅读:
    【Apifox Helper】自动生成接口文档,IDEA+Apifox懒人必备
    java集合之Vector与LinkedList[68]
    目标检测YOLO实战应用案例100讲-基于机器视觉的输电线路小目标检测和缺 陷识别
    shardingsphere分库分表示例(逻辑表,真实表,绑定表,广播表,单表)
    【Swift 60秒】61 - Computed properties
    【原创】十年带队经验,万字长文分享:如何管理好一个程序员团队?
    【Java】DDD领域驱动设计理解
    关于力扣链表的一个细节 头结点head
    翻译软件有哪些-翻译软件大家都推荐使用的有哪些
    舆情监控系统有哪些功能,行者AI告诉你
  • 原文地址:https://blog.csdn.net/weixin_42949219/article/details/136195145