Skip to content
New issue

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

go & java go 解析 私钥格式异常 #210

Open
grayier opened this issue Dec 25, 2024 · 1 comment
Open

go & java go 解析 私钥格式异常 #210

grayier opened this issue Dec 25, 2024 · 1 comment

Comments

@grayier
Copy link

grayier commented Dec 25, 2024

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

@grayier grayier closed this as completed Dec 25, 2024
@grayier grayier reopened this Dec 25, 2024
@emmansun
Copy link

emmansun commented Jan 1, 2025

你明明用的是github.com/piligo/gmsm/sm2,怎么跑这儿来问呢?看你的私钥格式是无密码pkcs8,应该是用错API了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants