zzmarquis / gm Goto Github PK
View Code? Open in Web Editor NEWGo语言:实现国密SM2/SM3/SM4算法;实现SM2 X509v3证书签发和解析
License: Apache License 2.0
Go语言:实现国密SM2/SM3/SM4算法;实现SM2 X509v3证书签发和解析
License: Apache License 2.0
测试描述:
前置条件:相同的私钥,相同的数据,相同的公钥;
测试内容:私钥对数据签名,公钥进行验签
测试结果:偶发性的验签失败
请教下,我把加密卡导出的公钥和私钥传入sm2.RawBytesToPublicKey 和 sm2.RawBytesToPrivateKey ,sm2.Decrypt 时报错invalid cipher text 是什么原因了...
在sm2过程中,如何将x509的字节串转换成公钥,然后进行sm2加密?
函数notEncrypted有个bug,请看有注释的行:
func notEncrypted(encData []byte, in []byte) bool {
encDataLen := len(encData)
for i := 0; i != encDataLen; i++ {
if encData[i] != in[i] { //,这一行的作用为判断t的每一字节是否为0,原来这一行为if encData[i] != in[0],把0改为i就对了
return false
}
}
return true
}
我现在有这样的一个任务。就是用对方生成的sm2 公私钥。私钥是压缩格式,base64 .
我个人建议,你sign就生成两个big.Int(r/s),至于编码,完全可以调用者自己来决定用哪种方式。毕竟,你已经提供了MarshalSign/UnmarshalSign两个函数了。
附注:我这边不需要用ans1编码,直接r/s用bytes拼接下就可以了(类似于公钥)。所以,我现在sign之后,需要先UnmarshalSign获得r/s,然后再进行bytes拼接。
`func (s *SM4) Sm2SignWithSm3(priKey, uid, data string) (string, error) {
//获取私钥
// 解码hex私钥
//keyByte, err := hex.DecodeString(priKey)
//if err != nil {
// return "1", err
//}
keyByte, err := base64.StdEncoding.DecodeString(priKey)
if err != nil {
return "11", err
}
// 转成go版的私钥
pri, err := sm2.RawBytesToPrivateKey(keyByte)
if err != nil {
return "2", err
}
// 携带uid 的sign
signature, err := sm2.Sign(pri, []byte(uid), []byte(data))
if err != nil {
return "3", err
}
// 转 base64
return base64.StdEncoding.EncodeToString(signature), nil
}`
以上代码生成的签名,招商报错: rgument: SM2 signature error, must be 64 bytes and in the format r||s.
以上代码生成的签名 长度96,大于标准长度88
能根据签名和hash恢复出公钥吗
golang端用的国密算法库是:https://github.com/ZZMarquis/gm
java端用的国密算法库是:https://github.com/gotoworld/hsd-cipher-sm
经过测试后发现:Java端SM2加密,golang无法解密,golang端SM2加密,java无法解密
排查了很久搜索到一篇文章讲解了不同点:https://my.oschina.net/u/2539854/blog/3094186
最近在进行加解密时,发现和java进行联调时偶尔出现无法解密的情况,然后对比下和java代码里面国密库的实现,发现java中加密过程中会对公钥的x和y值进行填充,但是这里并没有,而是直接使用c1x.Bytes()
方法,这样就导致c1x值比较小时,得到的切片长度小于32,文件sm2/sm2.go, 如第217行我增加了填充处理: c1xBytes = append(make([]byte, 32-len(c1xBytes)), c1xBytes...)
(c1yBytes也做同样的处理);另外,Encrypt
和Decrypt
中也需要同样处理。
用此代码中的sm2 sign方法签名国密建链过程中的certificate verify消息,服务端是gmssl,针对该条消息服务端返回decrypt error错误,根据错误码查看gmssl代码是签名校验的时候失败了,想问下作者有类似场景的对接吗?可能的问题是什么?我之前用过tjfoc的gmsm库也有此问题
可以在示例中加入从公私钥文件从读取获取公私钥对象的方法吗?
如题
您好, 目前我遇到这样的一个问题,在对接银行接口时,银行提供的公钥以base64的格式加密。
如下:
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE1xPq3B3Cw2U+t+R7Fb0JCJvy87/LDbUDFilGjkQU89VLl57pbUPLKUwP2jnAyOEKmJS9USsz+VwXNd4/bjdIFA==
base64Decode之后,得到的bytes字节长度为92,大于64。
请问您遇到过类似的问题吗?该如何解决呢
非常感谢提供的开源库,
我在使用中发现SM4加密结果JAVA无法解密。 go使用的是最新版本,
另外想问下有SM4的ECB实现吗?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.