首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >请教下 java Sm2签名结果 与 net Sm2签名结果生成不同是哪里有问题?

请教下 java Sm2签名结果 与 net Sm2签名结果生成不同是哪里有问题?

提问于 2023-03-09 17:36:49
回答 0关注 0查看 104
代码语言:javascript
复制
 //私钥
        String prvKey = "6911118b65d61f96ec2f2d19d5dd5d7bcda97e21daff622146098bbd5ee4383";
    

  Java 签名
      public String GetSign(String plainText, String priKey) throws Exception 
      {      
        BigInteger bigInteger = new BigInteger(priKey, 16);        
        X9ECParameters  parameters = GMNamedCurves.getByName("sm2p256v1");
        ECParameterSpec ecParameterSpec= new ECParameterSpec(parameters.getCurve(),
                 parameters.getG(), parameters.getN(), parameters.getH());
        
        KeyFactory keyFactory = KeyFactory.getInstance("EC", new BouncyCastleProvider());
        BCECPrivateKey privateKey = (BCECPrivateKey) keyFactory.generatePrivate(
                new ECPrivateKeySpec(bigInteger, ecParameterSpec));

        // 创建签名对象
        Signature signature = Signature.getInstance(GMObjectIdentifiers.sm2sign_with_sm3.toString(), provider);

        // 初始化为签名状态
        signature.initSign(privateKey);

        // 传入签名字节
        signature.update(plainText.getBytes());

        // 签名
        return Base64.getEncoder().encodeToString(signature.sign());
    }
    
    
   net 签名
    public string Encrypt(string privateKey, string sourceData)
    {
           byte[] byteSoureData = Encoding.UTF8.GetBytes(sourceData);
           SM2 sM2 = new SM2(privateKey, SM2.Mode.C1C3C2);
           string rvString2 = Convert.ToBase64String(Sign(byteSoureData));
    }    
     
    public byte[] Sign(byte[] msg, byte[] id = null)
      {
        Org.BouncyCastle.Asn1.X9.X9ECParameters sm2Ecparameters = GMNamedCurves.GetByName("SM2P256V1");
        ECDomainParameters domainParameters = 
            new ECDomainParameters(sm2Ecparameters.Curve, sm2Ecparameters.G, sm2Ecparameters.N);
        
        ICipherParameters PrivateKeyParameters = 
         new ECPrivateKeyParameters(new BigInteger(privkeyStr, 16),domainParameters );
         
            var sm2 = new SM2Signer(new SM3Digest());
            ICipherParameters cp;
            if (id != null) 
            {
              cp = new ParametersWithID(new ParametersWithRandom(PrivateKeyParameters), id);
            }
            else 
            {
              cp = new ParametersWithRandom(PrivateKeyParameters);
            }
            sm2.Init(true, cp);
            sm2.BlockUpdate(msg, 0, msg.Length);
            return sm2.GenerateSignature();
        }
    
    

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档