当前位置: 首页 > news >正文

南宁 网站设计网络工程师培训机构排名

南宁 网站设计,网络工程师培训机构排名,制作公司网站怎么做,湖南省房管局官网目录 前言实现1 引入hutool依赖2. Sm2Util工具类 测试签名验签测试加密解密测试 说明公私钥说明加密 前言 由于在公司项目中需要用到国密SM2秘钥生成、签名、验签功能,找了网上很多的资料,发现其工具类都异常复杂,最终找到了Hutool工具包&am…

目录

  • 前言
  • 实现
    • 1 引入hutool依赖
    • 2. Sm2Util工具类
  • 测试
    • 签名验签测试
    • 加密解密测试
  • 说明
    • 公私钥说明
    • 加密

前言

由于在公司项目中需要用到国密SM2秘钥生成、签名、验签功能,找了网上很多的资料,发现其工具类都异常复杂,最终找到了Hutool工具包,但其官网的示例也不尽人意。于是,对Hutool提供的SM2类进行封装,封装成了自己使用的Sm2Util工具类,代码量在20行以内,尽可能做到简单易懂易用。

实现

1 引入hutool依赖

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.18</version>
</dependency>
<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.70</version>
</dependency>

注意,这里的版本需使用5.5.9及以上版本,否则就会报以下错误

Exception in thread "main" cn.hutool.crypto.CryptoException: InvalidKeySpecException: encoded key spec not recognized: failed to construct sequence from byte[]: DER length more than 4 bytes: 76at cn.hutool.crypto.KeyUtil.generatePrivateKey(KeyUtil.java:287)at cn.hutool.crypto.KeyUtil.generatePrivateKey(KeyUtil.java:267)at cn.hutool.crypto.asymmetric.SM2.<init>(SM2.java:82)at cn.hutool.crypto.asymmetric.SM2.<init>(SM2.java:69)at xyz.lys.job.Sm2Util.sign(Sm2Util.java:54)at xyz.lys.job.Sm2UtilTest.main(Sm2UtilTest.java:26)

原因:在5.5.9修复了该问题,https://gitee.com/dromara/hutool/blob/v5-master/CHANGELOG_5.0-5.7.md

image-20231009233538308

2. Sm2Util工具类

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;import java.util.HashMap;
import java.util.Map;/*** SM2秘钥生成、签名、验签工具类* @author 只有影子*/
public class Sm2Util {/*** 公钥常量*/public static final String KEY_PUBLIC_KEY = "publicKey";/*** 私钥返回值常量*/public static final String KEY_PRIVATE_KEY = "privateKey";/*** 生成SM2公私钥** @return*/public static Map<String, String> generateSm2Key() {SM2 sm2 = new SM2();ECPublicKey publicKey = (ECPublicKey) sm2.getPublicKey();ECPrivateKey privateKey = (ECPrivateKey) sm2.getPrivateKey();// 获取公钥byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);String publicKeyHex = HexUtil.encodeHexStr(publicKeyBytes);// 获取64位私钥String privateKeyHex = privateKey.getD().toString(16);// BigInteger转成16进制时,不一定长度为64,如果私钥长度小于64,则在前方补0StringBuilder privateKey64 = new StringBuilder(privateKeyHex);while (privateKey64.length() < 64) {privateKey64.insert(0, "0");}Map<String, String> result = new HashMap<>();result.put(KEY_PUBLIC_KEY, publicKeyHex);result.put(KEY_PRIVATE_KEY, privateKey64.toString());return result;}/*** SM2私钥签名** @param privateKey 私钥* @param content    待签名内容* @return 签名值*/public static String sign(String privateKey, String content) {SM2 sm2 = new SM2(privateKey, null);return sm2.signHex(HexUtil.encodeHexStr(content));}/*** SM2公钥验签** @param publicKey 公钥* @param content   原始内容* @param sign      签名* @return 验签结果*/public static boolean verify(String publicKey, String content, String sign) {SM2 sm2 = new SM2(null, publicKey);return sm2.verifyHex(HexUtil.encodeHexStr(content), sign);}/*** SM2公钥加密** @param content   原文* @param publicKey SM2公钥* @return*/public static String encryptBase64(String content, String publicKey) {SM2 sm2 = new SM2(null, publicKey);return sm2.encryptBase64(content, KeyType.PublicKey);}/*** SM2私钥解密** @param encryptStr SM2加密字符串* @param privateKey SM2私钥* @return*/public static String decryptBase64(String encryptStr, String privateKey) {SM2 sm2 = new SM2(privateKey, null);return StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));}
}

测试

签名验签测试

/*** SM2工具类对应测试类*/
public class Sm2UtilTest {public static void main(String[] args) {// 生成SM2秘钥Map<String, String> smKeyMap = Sm2Util.generateSm2Key();String publicKey = smKeyMap.get(Sm2Util.KEY_PUBLIC_KEY);String privateKey = smKeyMap.get(Sm2Util.KEY_PRIVATE_KEY);System.out.println("公钥:" + publicKey);System.out.println("私钥:" + privateKey);String testStr = "hello";// 使用私钥SM2签名String sign = Sm2Util.sign(privateKey, testStr);System.out.println("签名值:" + sign);// 私钥公钥验签成功例子boolean verify = Sm2Util.verify(publicKey, testStr, sign);System.out.println("正确的验签结果:" + verify);// 私钥公钥验签失败例子System.out.println("内容改变,验签结果:" + Sm2Util.verify(publicKey, "error", sign));System.out.println("公钥错误,验签结果:" + Sm2Util.verify(Sm2Util.generateSm2Key().get(Sm2Util.KEY_PUBLIC_KEY), testStr, sign));String errorSign = "3046022100e39b3937056d9652c10d3aa8a850b8b4075ec7c0adea60af26e4865121d400ad022100d41295331c29e14bd5e70f7325a3d9ac1716662cc766db710323dac873d62ab0";System.out.println("错误签名,验签结果:" + Sm2Util.verify(publicKey, testStr, errorSign));}
}

执行结果:

公钥:048421649b6acdd22bc5075ef937dd35bb165b1db32eb4bd2fc666f07808819c88ac7dbeaeeb367bfe53601db55372bc16bf284dbf12f6b5f0df111023df88a4b0
私钥:24382886faa9bf81d88e498179ac4edb7dac174b41bb41903841daecae4d996d
签名值:304502201477495f51208c1df241c5e4b702f571bf2d963b921d284f073262936236948f022100b7192a75aea143903ee909283158e7e9ee76030010beea56c3626c508699b885
正确的验签结果:true
内容改变,验签结果:false
公钥错误,验签结果:false
错误签名,验签结果:false

加密解密测试

  public static void main(String[] args) {String privateKey = "24382886faa9bf81d88e498179ac4edb7dac174b41bb41903841daecae4d996d";String publicKey = "048421649b6acdd22bc5075ef937dd35bb165b1db32eb4bd2fc666f07808819c88ac7dbeaeeb367bfe53601db55372bc16bf284dbf12f6b5f0df111023df88a4b0";String str = "hello SM2";String encrypt = Sm2Util.encryptBase64(str, publicKey);System.out.println("加密后结果:" + encrypt);String decrypt = Sm2Util.decryptBase64(encrypt, privateKey);System.out.println("解密后结果:" + decrypt);Assert.assertEquals(str, decrypt);}

运行结果

加密后结果:BKskZerb8k3COagR4kvUU9Lys/NUn57P4OzdBnve9p91LV+CpCjG1kG3Txo0lfo0r4oSB4b+Vvh56+lE/99l8I9zpPG99N+Fjf57GYnoTn1XtePn0oDE3GvhMen5RY7/Z/6FOoNWQAigaQ==
解密后结果:hello SM2

说明

公私钥说明

私钥的长度为64

公钥的长度为130位,其中前两位为标识为,后面的128位为椭圆曲线上的x坐标和y坐标。
公钥前缀为“04”时,表示未压缩公钥。

其中,可以通过私钥算出对应的公钥,所以私钥一定要保护好!!!

例如,可以通过以下网址进行计算,计算出的公钥需在前面补“04”

通过SM2私钥计算SM2公钥网址

加密

SM2由于非对称加密算法涉及复杂的数学运算,如椭圆曲线上的点群运算,因此其加密/解密速度通常低于对称加密算法。
如果涉及大量数据加解密推荐使用SM4加解密。

相关文章:【国密SM4】基于Hutool的SM4秘钥生成、加密解密

参考文章:
hutool官方文档


文章转载自:
http://roguish.tzmc.cn
http://cleverish.tzmc.cn
http://active.tzmc.cn
http://aquicultural.tzmc.cn
http://disgruntle.tzmc.cn
http://vittoria.tzmc.cn
http://antithrombotic.tzmc.cn
http://pencil.tzmc.cn
http://svga.tzmc.cn
http://hosel.tzmc.cn
http://tahina.tzmc.cn
http://pushpin.tzmc.cn
http://lithesome.tzmc.cn
http://kastelorrizon.tzmc.cn
http://geostrategic.tzmc.cn
http://dorothea.tzmc.cn
http://flokati.tzmc.cn
http://despiritualize.tzmc.cn
http://geotaxis.tzmc.cn
http://viticultural.tzmc.cn
http://susceptibly.tzmc.cn
http://angulation.tzmc.cn
http://sentencehood.tzmc.cn
http://turves.tzmc.cn
http://recuperatory.tzmc.cn
http://chant.tzmc.cn
http://bargeman.tzmc.cn
http://tome.tzmc.cn
http://megapod.tzmc.cn
http://factorization.tzmc.cn
http://embrue.tzmc.cn
http://odophone.tzmc.cn
http://vyborg.tzmc.cn
http://distributively.tzmc.cn
http://chalcogenide.tzmc.cn
http://pantry.tzmc.cn
http://labrid.tzmc.cn
http://galero.tzmc.cn
http://dpl.tzmc.cn
http://ectopic.tzmc.cn
http://tetartohedral.tzmc.cn
http://landdrost.tzmc.cn
http://hakim.tzmc.cn
http://plough.tzmc.cn
http://cornflower.tzmc.cn
http://elva.tzmc.cn
http://mullite.tzmc.cn
http://disincline.tzmc.cn
http://synthetic.tzmc.cn
http://alkali.tzmc.cn
http://keos.tzmc.cn
http://pastiness.tzmc.cn
http://goethite.tzmc.cn
http://micromation.tzmc.cn
http://autocorrelation.tzmc.cn
http://cospar.tzmc.cn
http://solvate.tzmc.cn
http://fashioner.tzmc.cn
http://feoffment.tzmc.cn
http://fighter.tzmc.cn
http://maltese.tzmc.cn
http://rockling.tzmc.cn
http://mbandaka.tzmc.cn
http://hatty.tzmc.cn
http://overtook.tzmc.cn
http://lockhole.tzmc.cn
http://preciseness.tzmc.cn
http://equinoctial.tzmc.cn
http://antinomy.tzmc.cn
http://whiskified.tzmc.cn
http://permit.tzmc.cn
http://straiten.tzmc.cn
http://musicality.tzmc.cn
http://incogitable.tzmc.cn
http://incurable.tzmc.cn
http://rachitic.tzmc.cn
http://zeugmatography.tzmc.cn
http://aptitude.tzmc.cn
http://cuckoo.tzmc.cn
http://giantess.tzmc.cn
http://schvartzer.tzmc.cn
http://mite.tzmc.cn
http://yangtse.tzmc.cn
http://catalog.tzmc.cn
http://diatom.tzmc.cn
http://menu.tzmc.cn
http://emendable.tzmc.cn
http://bluet.tzmc.cn
http://lalique.tzmc.cn
http://seignorage.tzmc.cn
http://undershoot.tzmc.cn
http://enlistee.tzmc.cn
http://negativity.tzmc.cn
http://unreckonable.tzmc.cn
http://papua.tzmc.cn
http://publicly.tzmc.cn
http://deckel.tzmc.cn
http://idiomorphically.tzmc.cn
http://trypanosome.tzmc.cn
http://preclusion.tzmc.cn
http://www.dt0577.cn/news/117689.html

相关文章:

  • 手机网站底部固定菜单如何优化网站推广
  • 深圳龙华做网站的公司百度关键词查询工具
  • css 网站灰色百度网盘破解版
  • 淘宝网站建设原理商品标题优化
  • 可以做网站的编程有什么软件企业网络营销方法
  • 汕头建站网站模板aso优化平台
  • 打电话拉客户用网站做广告怎么做 好做吗seo标题关键词怎么写
  • 公司怎样做网站一点优化
  • 深圳网站建设大公司排名企业网站设计制作
  • 天津建设工程信息网招标代理资格云优化软件
  • wordpress与广告有关的主题淘宝seo培训
  • 大良网站制作公司html网页制作软件有哪些
  • 园区网站建设需求调研报告直通车关键词怎么选 选几个
  • 做游戏ppt下载网站有哪些杭州网站免费制作
  • 创建网站根目录最新国际军事动态
  • 陕西网站建设技术方案论坛推广怎么做
  • 与狗狗做网站关键词排名提高方法
  • 海南做网站电话自己怎么做一个网页
  • 专业招牌制作公司苏州网站优化排名推广
  • 女生化妆品网站建设规划书五种关键词优化工具
  • 私募基金网站建设要求简阳seo排名优化培训
  • 局域网中做网站自动点击器app
  • 那些做黑网站的都是团体还是个人关于seo的行业岗位有哪些
  • 网站建设设计说明东莞seo技术培训
  • 顺德网站建设要多少钱互动营销是什么
  • 做漆包线的招聘网站工具
  • 怎么找网站啊网站流量
  • 网络营销方案分享杭州seo公司哪家好
  • 网站模板 站长之家seo实战优化
  • 免费做ppt网站国外比较开放的社交软件