We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java 私钥加密 公钥验证过
import cn.hutool.core.codec.Base64; import cn.hutool.crypto.SmUtil; import cn.hutool.crypto.asymmetric.SM2;
public class TestSMUtilk { public static void main(String[] args) {
String pubKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEQ21tA18UykL6a/ooWGM0DhJMG4b/9U3KCtHEtCUO48scyHIBYEY93fDedr9dfX//AdjyNsCJb07eRYwgQqZDZg=="; String prvKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgSdKybI/xvRi/VlyLUewnfADhtdkRIbhjanf7Qi6rG+ygCgYIKoEcz1UBgi2hRANCAARDbW0DXxTKQvpr+ihYYzQOEkwbhv/1TcoK0cS0JQ7jyxzIcgFgRj3d8N52v119f/8B2PI2wIlvTt5FjCBCpkNm"; String msg = "content"; String sign = getSign(msg, prvKey); System.out.println("私钥加密 " + sign); boolean is = verify(sign, msg, pubKey); System.out.println("公钥验证 " + is); } public static String getSign(String content, String privateKey) { SM2 sm2Sign = SmUtil.sm2(Base64.decode(privateKey), null); sm2Sign.usePlainEncoding(); byte[] sign = sm2Sign.sign(content.getBytes(), null); return Base64.encode(sign); } public static Boolean verify(String sign, String content, String publicKey) { SM2 sm2 = SmUtil.sm2(null, Base64.decode(publicKey)); sm2.usePlainEncoding(); return sm2.verify(content.getBytes(), Base64.decode(sign)); }
}
go 实现相同的代码 私钥 解不出来 私钥格式问题嘛
package main
import ( "encoding/base64" "fmt" "github.com/piligo/gmsm/sm2" )
func main() { prvKey := "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgSdKybI/xvRi/VlyLUewnfADhtdkRIbhjanf7Qi6rG+ygCgYIKoEcz1UBgi2hRANCAARDbW0DXxTKQvpr+ihYYzQOEkwbhv/1TcoK0cS0JQ7jyxzIcgFgRj3d8N52v119f/8B2PI2wIlvTt5FjCBCpkNm" prvBYtes, _ := base64.StdEncoding.DecodeString(prvKey)
pri, err := sm2.ParseSm2PrivateKey(prvBYtes) if err != nil { panic(err) } fmt.Println(pri)
panic: x509: failed to parse SM2 private key: asn1: structure error: tags don't match (4 vs {class:0 tag:16 length:19 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} @5
The text was updated successfully, but these errors were encountered:
你明明用的是github.com/piligo/gmsm/sm2,怎么跑这儿来问呢?看你的私钥格式是无密码pkcs8,应该是用错API了。
Sorry, something went wrong.
No branches or pull requests
java 私钥加密 公钥验证过
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
public class TestSMUtilk {
public static void main(String[] args) {
}
go 实现相同的代码 私钥 解不出来 私钥格式问题嘛
package main
import (
"encoding/base64"
"fmt"
"github.com/piligo/gmsm/sm2"
)
func main() {
prvKey := "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgSdKybI/xvRi/VlyLUewnfADhtdkRIbhjanf7Qi6rG+ygCgYIKoEcz1UBgi2hRANCAARDbW0DXxTKQvpr+ihYYzQOEkwbhv/1TcoK0cS0JQ7jyxzIcgFgRj3d8N52v119f/8B2PI2wIlvTt5FjCBCpkNm"
prvBYtes, _ := base64.StdEncoding.DecodeString(prvKey)
}
panic: x509: failed to parse SM2 private key: asn1: structure error: tags don't match (4 vs {class:0 tag:16 length:19 isCompound:true}) {optional:false explicit:false application:false private:false defaultValue: tag: stringType:0 timeType:0 set:false omitEmpty:false} @5
The text was updated successfully, but these errors were encountered: