欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java生成sm2/hutool生成公鑰私鑰代碼示例

 更新時(shí)間:2023年09月26日 10:28:39   作者:是個(gè)杏仁  
這篇文章主要給大家介紹了關(guān)于java生成sm2/hutool生成公鑰私鑰的相關(guān)資料,Java是一種廣泛使用的編程語(yǔ)言,可以用來(lái)生成公鑰和私鑰文件,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下

首先引入maven依賴(lài)

<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk15to18</artifactId>
  <version>1.69</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>

然后運(yùn)行以下代碼

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);

就可以得到私鑰和公鑰

公鑰提供給通訊方驗(yàn)簽

下面是簽名的代碼

String content = "我是Hanley.";
final SM2 sm2 = SmUtil.sm2(privateKeyStr,null);
String sign = sm2.signHex(HexUtil.encodeHexStr(content));

驗(yàn)簽代碼

final SM2 sm2 = SmUtil.sm2(null,publicKeyStr);
// true
boolean 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還有很多強(qiáng)大的工具類(lèi),方便程序員們的調(diào)用

總結(jié)

到此這篇關(guān)于java生成sm2/hutool生成公鑰私鑰的文章就介紹到這了,更多相關(guān)java hutool生成公鑰私鑰內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論