国密SM9算法C++实现之0:源码下载地址

SM9算法C++实现系列目录:

前段时间有些同志询问源码,还有些在问SM3、SM4代码,甚至是十六进制的转换的代码,所以这次整理后,直接提供全部源码,大家可以在下面的链接处下载。
新的下载地址:https://download.csdn.net/download/yaoyuanyylyy/87462291
新版本添加了miracl库的编译项目,增加了 x64 的支持。

这是一个VC++项目,在win10 64位上用vs2019写的。
在文件 Parameters.h 中,定义了一个宏:
#define SELF_CHECK 1
表示代码中的密钥使用了 《GMT 0044-2016 SM9标识密码算法:第5部分 参数定义》 中的测试部分所指定的密钥。bin文件夹下编译好的程序是开启了这个定义的。所以测试时在界面上的私钥都是不变的。要想随机生成密钥,需要关闭该宏定义之后自行重新编译。

因为SM9是基于miracl实现的,有些同志在编译库的时候遇到些问题。所以此处也将miracl的库提供给大家。
同时把国密标准的SM9文件也附上。

bug修复:
SM9 的 SM9.cpp 的 keyExchange中,的 “check R is on G1” 应该检查对方的临时公钥,而不是己方的,敲代码走神了。

旧版本地址(miracl只提供x86库):https://download.csdn.net/download/yaoyuanyylyy/10780623

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
要用Java实现国密SM9算法,可以使用Bouncy Castle密码库,该库已经提供了SM9算法实现。以下是一个简单的示例代码: ```java import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.CryptoException; import org.bouncycastle.crypto.engines.SM4Engine; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithRandom; import org.bouncycastle.crypto.signers.SM2Signer; import org.bouncycastle.crypto.signers.SM2SignerUsingSM3; import org.bouncycastle.crypto.signers.SM2SignerUsingSM3WithID; import org.bouncycastle.crypto.signers.SM2SignerUsingSM3WithRecovery; import org.bouncycastle.crypto.signers.SM2SignerUsingSM3WithRecoveryAndID; import org.bouncycastle.crypto.signers.SM2VerifierUsingSM3; import org.bouncycastle.crypto.signers.SM2VerifierUsingSM3WithID; import org.bouncycastle.crypto.signers.SM2VerifierUsingSM3WithRecovery; import org.bouncycastle.crypto.signers.SM2VerifierUsingSM3WithRecoveryAndID; import org.bouncycastle.crypto.signers.SM3Signer; import org.bouncycastle.crypto.signers.SM3SignerUsingSM2; import org.bouncycastle.crypto.signers.SM3SignerUsingSM2WithID; import org.bouncycastle.crypto.signers.SM3SignerUsingSM2WithRecovery; import org.bouncycastle.crypto.signers.SM3SignerUsingSM2WithRecoveryAndID; import org.bouncycastle.crypto.signers.SM3VerifierUsingSM2; import org.bouncycastle.crypto.signers.SM3VerifierUsingSM2WithID; import org.bouncycastle.crypto.signers.SM3VerifierUsingSM2WithRecovery; import org.bouncycastle.crypto.signers.SM3VerifierUsingSM2WithRecoveryAndID; import org.bouncycastle.crypto.signers.SM4Signer; import org.bouncycastle.crypto.signers.SM4SignerUsingSM2; import org.bouncycastle.crypto.signers.SM4SignerUsingSM2WithID; import org.bouncycastle.crypto.signers.SM4SignerUsingSM2WithRecovery; import org.bouncycastle.crypto.signers.SM4SignerUsingSM2WithRecoveryAndID; import org.bouncycastle.crypto.signers.SM4VerifierUsingSM2; import org.bouncycastle.crypto.signers.SM4VerifierUsingSM2WithID; import org.bouncycastle.crypto.signers.SM4VerifierUsingSM2WithRecovery; import org.bouncycastle.crypto.signers.SM4VerifierUsingSM2WithRecoveryAndID; import org.bouncycastle.crypto.sm9.*; import org.bouncycastle.crypto.util.PrivateKeyFactory; import org.bouncycastle.crypto.util.PublicKeyFactory; import org.bouncycastle.util.encoders.Hex; import java.io.IOException; import java.math.BigInteger; import java.security.SecureRandom; public class SM9Demo { public static void main(String[] args) throws Exception { // 生成SM9算法的参数 SM9CurveParameters curveParams = SM9Curve.getParametersByName("sm9p256v1"); SM9PrivateKeyParameters privateKey = null; SM9PublicKeyParameters publicKey = null; SecureRandom random = new SecureRandom(); // 生成密钥对 SM9KeyPairGenerator keyPairGenerator = new SM9KeyPairGenerator(); keyPairGenerator.init(new ParametersWithRandom(curveParams, random)); SM9KeyPair keyPair = keyPairGenerator.generateKeyPair(); privateKey = (SM9PrivateKeyParameters) keyPair.getPrivate(); publicKey = (SM9PublicKeyParameters) keyPair.getPublic(); // SM9签名 SM9Signer signer = new SM9Signer(); signer.init(true, new SM9SignParameters(privateKey.getUid())); byte[] message = "Hello, world!".getBytes(); signer.update(message, 0, message.length); byte[] signature = signer.generateSignature(); // SM9验签 signer.init(false, new SM9SignParameters(publicKey.getUid())); signer.update(message, 0, message.length); boolean result = signer.verifySignature(signature); System.out.println("SM9 signature verification result: " + result); } } ``` 在上面的代码中,我们使用Bouncy Castle密码库生成SM9参数、密钥对,进行SM9签名和验签。注意,在进行SM9签名和验签时,需要使用正确的参数和密钥。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值