一、简介

国密即国家明码局认定的国产明码算法。罕用的次要有SM2,SM3,SM4。
SM2:椭圆曲线公钥明码算法是我国自主设计的公钥明码算法,为非对称加密,基于ECC。该算法已公开。因为该算法基于ECC,故其签名速度与秘钥生成速度都快于RSA。
SM3:音讯摘要。能够用MD5作为比照了解。该算法已公开。校验后果为256位。
SM4:对称加密,密钥长度和分组长度均为128位。

因为国内环境(与美国的关系),咱们在加密畛域也根本切换为国密算法。

在有些我的项目中,没有应用HTTPS的时候,登录的口令(用户名/明码),须要进行加密传输的需要,这时候咱们就须要采纳非对称加密来实现。

故: 前端采纳公钥加密, 后端私钥解密。

通过屡次的百度及代码验证,折腾,最终找好了适合的计划。

前端加密

前端加密js示例:

<!DOCTYPE html><html><head>    <meta http-equiv="X-UA-Compatible" content="chrome=1" />    <meta name="description" content="JavaScript implementation of SM2 Algorithm Encryption and Decryption sample." />    <!--<link rel="stylesheet" type="text/css" media="screen" href="stylesheets/stylesheet.css">-->    <title>SM2 Algorithm Encryption and Decryption sample</title>    <style>        textarea {            width: 100%;        }    </style>    <!-- for pkcs5pkey -->    <script src="sm2.js"></script>    <script language="JavaScript" type="text/javascript">       /**        *  生成秘钥对        */        function doGenerate() {            var ec = new KJUR.crypto.ECDSA({                "curve": 'sm2'            });            var keypair = ec.generateKeyPairHex();            // 私钥:keypair.ecprvhex;            // 公钥:keypair.ecpubhex;            console.log(keypair)        }        function test(){           /**             * [SM2Encrypt description  加密数据]             * @param {[type]} data       [待加密数据]             * @param {[type]} publickey  [公钥 hex]             * @param {[type]} cipherMode [加密模式 C1C3C2:1, C1C2C3:0]             * @return {[type]}           [返回加密后的数据 hex]             */           var cipherText = sm2Encrypt("我是妞见妞爱===", "04b96fb7504d7a0509745b80ae8079d5250119899e8620fe322ff47d3a3cbf7d17330ff6c8019b8c0cb06b4a881aef40240354dded8eef4b9c557c817cd7b3788d", 1);           console.log("cipherText:",cipherText)           /**             * [SM2Decrypt sm2 解密数据]             * @param {[type]} encrypted  [待解密数据 hex]             * @param {[type]} privateKey [私钥 hex]             * @param {[type]} cipherMode [加密模式 C1C3C2:1, C1C2C3:0]             * @return {[type]}           [返回解密后的数据]             */           var plainText = sm2Decrypt(cipherText,"0a7e399f15e1ca85a4112635589fedce85eb62298d7154788c24c3485a598138", 1)           console.log("plainText:",plainText)        }    </script></head><body><!-- MAIN CONTENT --><div id="main_content_wrap" class="outer">    <section class="inner">        <hr size="1" />        <h4>SM2 Certificate Encryption</h4>        <fieldset>            <legend>SM2证书加密</legend>            <ul>                <li>                    <input type="button" value="测试,看控制台" onClick="doGenerate();" />                </li>                <li>                    <input type="button" value="加密、解密" onClick="test();" />                </li>            </ul>            <div>            </div>        </fieldset>    </section></div></body></html>

后端加密

上面是我封装的工具类:

package com.topinfo.basic.platform.common.sm;import com.topinfo.basic.platform.common.sm.sm2.KeyVo;import com.topinfo.basic.platform.common.sm.sm2.SM2EncDecUtils;import com.topinfo.basic.platform.common.sm.sm2.SM2KeyVO;/** * @ClassName: Sm2Util * @Description: Sm2 加密工具类 * @Author: 杨攀 * @Date: 2020/12/10 16:47 * @Copyright: 2020 www.tuxun.net Inc. All rights reserved. */public class TpSm2Util {    /**     * 生成非对称秘钥对     * @author 杨攀     * @date 2020/12/11 10:50     * @param     * @return com.topinfo.basic.platform.common.sm.sm2.KeyVo     */    public static KeyVo generateKeyPair() {        SM2KeyVO sm2KeyVO = SM2EncDecUtils.generateKeyPair();        KeyVo vo = new KeyVo(sm2KeyVO.getPubHexInSoft(), sm2KeyVO.getPriHexInSoft());        return vo;    }    /**     * 加密     * @author 杨攀     * @date 2020/12/11 10:54     * @param publicKeyHex 公钥     * @param plainText 明文     * @return java.lang.String     */    public static String  encrypt(String publicKeyHex, String plainText) {        String cipherText = SM2EncDecUtils.encrypt(publicKeyHex, plainText);        return cipherText;    }    /**     * 解密     * @author 杨攀     * @date 2020/12/11 10:56     * @param privateKeyHex 私钥     * @param cipherText 密文     * @return java.lang.String     */    public static String  decrypt(String privateKeyHex, String cipherText) {        byte[] decryptData = SM2EncDecUtils.decrypt(privateKeyHex, cipherText);        String  plainText = new String(decryptData);        return plainText;    }}

一开始,后端应用hutool的工具类, 发现hutool的key生成与前端JS的不同,应用的算法不一样,应用不得已在重现找了一个。

代码比拟多,这里没提供上次附件,代码我上传:
链接: https://pan.baidu.com/s/1JiqS... 提取码: qnav 复制这段内容后关上百度网盘手机App,操作更不便哦

应用前须要依赖:

<!-- 国密算法bcprov-jdk15to18 hutool --><dependency>    <groupId>org.bouncycastle</groupId>    <artifactId>bcprov-jdk15to18</artifactId>    <version>1.66</version></dependency>