首页 新闻 会员 周边 捐助

C# Sm3WithSm2签名 遇到难题 Scalar is not in the interval

0
悬赏园豆:5 [已解决问题] 解决于 2022-11-15 10:25

网上找的代码几乎都是BouncyCastle框架,核心代码大同小异,我搬运之后测试的时候遇到一个问题,没搜到有类似的解答

SM2Param sM2Param = new SM2Param();
                byte[] priKey = Base64.Decode(priKeyStr);
                BigInteger bi = new BigInteger(1, priKey);
                ECDomainParameters domain = new ECDomainParameters(GMNamedCurves.GetByName("SM2P256V1"));
                ECPrivateKeyParameters privateKeyParameters = new ECPrivateKeyParameters(bi, domain);

如上面的代码,在初始化ECPrivateKeyParameters的时候报错了:Scalar is not in the interval

求大佬们帮忙看看

我的私钥是

MIIGfgIBAzCCBjgGCSqGSIb3DQEHAaCCBikEggYlMIIGITCCAyMGCSqGSIb3DQEHBqCCAxQwggMQAgEAMIIDCQYJKoZIhvcNAQcBMCgGCiqGSIb3DQEMAQYwGgQU/60actpvZ/qc8+oReCSNALm8xr0CAgQAgIIC0GmusblUrDviePTMM5pThStAZy6FoSs/+/lE22Agxgsp2kcJgbrrgcg1ZkbKya+xqj+lCXrnh1rxpUl2VPvRCEi4G703nN7hog75tF5iF0x6N6AepYD9DlvZqWwP+GDcfD+LBYofwAwALxFmw0Pr3LMdeDl3R5XypNvsBSc1fyaqPIl0Xo7WDA/vLRwnI9SZkwSyfqES9l3fluLlLVM91fVRBKmkLfSZpqy9TsBvGmlqH4kIaXN2vRAjQeA9qaVs/JLTsMGvLNlZB5ne6kGbDUdQDLPumAdzyedPRgseIzAzZZ37RjKv+C3RQzpXwObTWxQsjX5OIvhQ2llGZoGUpUqJCCQ2pKXqQPyNnPjxJ2bJBYZdwafWkChYYLChbmtp+ZQWLZD9VwwMjRCm+HUqinAq7FuUAu8h1uR5cQrLrw/28Z6hthCXdL6SigEHA/w59u+xW4WCUFJBnYjf5LU/g36eMuKEYbYPXZsgwVgpROBDbaxX397L1CL4dGPv2DPQzJ/tV3bopqNu6wIwTBDz0jB1P/9gm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03eEMNuWHTjt/H6B+8nYx8SmAicRpiHCkQcfBAIDjNTuXqJUGSjSfD269aFrdJ7BJgKrOd2SDeDhDTZ0JZo+grBeq3dOTbjQyGop3DdycF2u4Ta0gA3zrWO85srOPvwQfzFG0Ya50R570Ll6QDK7aXzG8C9gJCJI7umkHxlWatn1PaMOtvqZRDOzHwIIXT+DRi1U0BGiJEk88r5fm1C57mMFgjsm99/g9oNOHeF5XFqbHWq8qulhTnBlqVsPiC7Xq65hlgkA5ZcF/Z2jFGMB8GCSqGSIb3DQEJFDESHhAAVQBzAGUAcgAgAEsAZQB5MCMGCSqGSIb3DQEJFTEWBBSgxiLfNxU/FvuDTrI4rTyphTx3VDA9MCEwCQYFKw4DAhoFAAQUia+dQmTcUyAmIncT0wJJVOOmNEYEFL65nzwo4Pu4FQ5UcCYSrRzm9KMnAgIEAA==

hangjy的主页 hangjy | 初学一级 | 园豆:4
提问于:2022-11-11 17:37
< >
分享
最佳答案
0

应该是私钥的问题,换成园子里 C# SM加密 这篇博文中的私钥,用你的代码可以正常运行,测试代码如下

using Org.BouncyCastle.Asn1.GM;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities.Encoders;
using System.Text;

var rawPrivateKey = "3690655E33D5EA3D9A4AE1A1ADD766FDEA045CDEAA43A9206FB8C430CEFE0D94";
var base64PrivateKey = Convert.ToBase64String(Encoding.ASCII.GetBytes(rawPrivateKey));
var priKeyStr = Convert.FromBase64String(base64PrivateKey);

byte[] priKey = Hex.Decode(priKeyStr);
var bi = new Org.BouncyCastle.Math.BigInteger(1, priKey);

var domain = new ECDomainParameters(GMNamedCurves.GetByName("sm2p256v1"));
domain.ValidatePrivateScalar(bi);
var privateKeyParameters = new ECPrivateKeyParameters(bi, domain);

所使用的 nuget 包

<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />

.NET 版本是 7.0

收获园豆:5
dudu | 高人七级 |园豆:31030 | 2022-11-12 13:26

谢谢,我后来用Pkcs12Store加载私钥文件就可以了,直接用字符串不行

hangjy | 园豆:4 (初学一级) | 2022-11-15 10:25

@hangjy: 你好,请问可以发下处理私钥字符串的c#代码么。我也是直接使用字符串后报错这个错。

mr.ming123 | 园豆:200 (初学一级) | 2024-06-06 13:58
其他回答(1)
0

找到原因了,公钥或者私钥错误,差一个字母就会报错。比如:lL1、oO0这些三方给的公钥或者私钥,还是用pdf给的经常会copy错误。建议用adebe pdf 、wps两个识别,确保每个字母正确

牛气大志 | 园豆:402 (菜鸟二级) | 2024-07-16 10:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册