首先引入Maven依赖 org.bouncycastle bcprov-jdk15to18 1.69 cn.hutool hutool-all 5.8.16 然后运行以下代码
首先引入Maven依赖
<dependency> <groupId>org.bouncycastlegroupId> <artifactId>bcprov-jdk15to18artifactId> <version>1.69version>dependency><dependency> <groupId>cn.hutoolgroupId> <artifactId>hutool-allartifactId> <version>5.8.16version>dependency>
然后运行以下代码
KeyPair pair = SecureUtil.generateKeyPair("SM2");byte[] privateKey = pair.getPrivate().getEncoded();byte[] publicKey = pair.getPublic().getEncoded();//私钥String privateKeyStr=Base64.getEncoder().encodeToString(privateKey);//公钥String publicKeyStr=Base64.getEncoder().encodeToString(publicKey);
就可以得到私钥和公钥
公钥提供给通讯方验签
下面是签名的代码
String content = "我是Hanley.";final SM2 sm2 = SmUtil.sm2(privateKeyStr,null);String sign = sm2.signHex(HexUtil.encodeHexStr(content));
验签代码
final SM2 sm2 = SmUtil.sm2(null,publicKeyStr);// trueboolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
加密
// 公钥加密,私钥解密SM2 sm2 = SmUtil.sm2(null, publicKey);String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
解密
// 公钥加密,私钥解密SM2 sm2 = SmUtil.sm2(privateKeyStr, null);String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
好了,以上就是sm2util的使用介绍了,hutool还有很多强大的工具类,方便程序员们的调用
来源地址:https://blog.csdn.net/t_1000poke/article/details/131928448
--结束END--
本文标题: java生成sm2使用hutool生成公钥私钥
本文链接: https://www.lsjlt.com/news/401297.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0