Tag: ecdsa

使用C#中的Bouncy CastlevalidationECDSA签名

当我尝试在C#中validationBouncy Castle中的ECDSA签名时,我遇到了问题。 代码是从我拥有的Java示例中采用的,因此我100%确定公钥和签名是正确的。 但是C#实现总是返回签名无效。 我检查了曲线参数,它们是正确的。 我尝试使用DER和“原始”签名,但它再次无法正常工作。 任何人都可以发现我做错了什么: namespace TestECDSA { class Program { static void Main(string[] args) { byte[] b = new byte[] { 0x2B, 0xA1, 0x41, 0x00 }; string pubKey = “044F6D3F294DEA5737F0F46FFEE88A356EED95695DD7E0C27A591E6F6F65962BAF”; string signature = “AAD03D3D38CE53B673CF8F1C016C8D3B67EA98CBCF72627788368C7C54AA2FC4”; X9ECParameters curve = SecNamedCurves.GetByName(“secp128r1”); ECDomainParameters curveSpec = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed()); ECPublicKeyParameters key = new ECPublicKeyParameters(“ECDSA”, […]

UWP ECDSP签名

我想用这段代码制作一个ECDSA签名: AsymmetricKeyAlgorithmProvider objAsymmAlgProv = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.EcdsaSha256); CryptographicKey keypair = objAsymmAlgProv.CreateKeyPairWithCurveName(EccCurveNames.SecP256r1); BinaryStringEncoding encoding = BinaryStringEncoding.Utf8; buffMsg = CryptographicBuffer.ConvertStringToBinary(“Test Message”, encoding); IBuffer buffSIG = CryptographicEngine.Sign(keypair, buffMsg); byte [] SignByteArray = buffSIG.ToArray(); bool res = CryptographicEngine.VerifySignature(keypair, buffMsg, buffSIG); VerifySignature始终返回true,这没关系。 但我在签名方面遇到了一些问题。 为什么签名长度( SignByteArray )已修复? (0x40字节)。 为什么SignByteArray [0]和SignByteArray [2]值不正确? (我认为它们应该是0x30和0x02) 我期待像https://kjur.github.io/jsrsasign/sample-ecdsa.html这样的东西

转换椭圆曲线参数(BC到MS)

我正在尝试生成ECDSA自签名证书,如使用ECDSA生成证书中所述。 将所有来自bartonjs的答案放在一起并使用Net.Framework 4.7 (或Net.Core 2.0 )代码似乎正在起作用,尽管有一些含糊不清(至少有一个): 我不确定如何正确地将私钥(’D’参数)从BC-BigInteger为MS-byte[] 。 使用BigInteger.ToByteArray()抛出exception: CryptographicException :指定的键参数无效。 QX和QY是必填字段。 QX,QY必须长度相同。 如果指定了D,则它必须与命名曲线的QX和QY的长度相同,或者与显式曲线的Order的长度相同。 同时validationECParameters(方法ECParameters.Validate() )。 使用BigInteger.ToByteArrayUnsigned()可以提供更好的结果(几百个生成的密钥对失败),但仍然…… 当使用ToByteArray()转换’D’时通常长一个字节(’D’与DX相比有33个字节,而DY有32个字节)。 使用ToByteArrayUnsigned() ,’D’有时短一个字节。 所以我的问题是使用ToByteArrayUnsigned()是否可以。 private const string NCryptExportPolicyProperty = “Export Policy”; private const string SignatureAlgorithm = “Sha256WithECDSA”; private static readonly ECCurve MsCurve = ECCurve.NamedCurves.nistP256; private static readonly DerObjectIdentifier BcCurve = SecObjectIdentifiers.SecP256r1; // must correspond with MsCurve public static […]

在c#中使用ECDSA生成证书

我正在尝试使用ECDSA生成带有私钥的(自签名)证书。 目标是获得与使用openssl时“相同”(pkcs12)的证书: openssl ecparam -genkey -name secp256r1 -out mykey.key openssl req -new -key mykey.key -out myreq.csr openssl req -x509 -days 7 -key mykey.key -in myreq.csr -out mycert.crt openssl pkcs12 -export -out mycert.pfx -inkey mykey.key -in mycert.crt 我已经使用BouncyCastle帮助我创建基于RSA的证书,因此接下来的步骤或多或少都遵循我用来创建RSA证书的方式。 (请注意, BC前缀用于来自BouncyCastle的类, MS用于.NET类) 1生成密钥对:私钥和公钥 BC.IAsymmetricCipherKeyPairGenerator bcKpGen = BC.GeneratorUtilities.GetKeyPairGenerator(“ECDSA”); bcKpGen.Init(new BC.ECKeyGenerationParameters(BC.SecObjectIdentifiers.SecP256r1, new BC.SecureRandom())); BC.AsymmetricCipherKeyPair bcSubjKeys = bcKpGen.GenerateKeyPair(); 2使用私钥与一些额外的数据(主题,有效期等)签署公钥 BC.X509V3CertificateGenerator […]

.NET中的ECDiffieHellmanCng是否具有实现NIST SP 800-56A的密钥派生函数,第5.8.1节

我有一项任务需要使用NIST SP 800-56A第5.8.1节中描述的密钥导出函数来获取密钥材料。 我不是密码学方面的专家,所以如果问题是天真的,请原谅。 这是我到目前为止所做的: 我有另一方的公钥和我的私钥 现在我尝试使用C#(。NET 4)ECDiffieHellmanCng类使用ECDH 1.3.132.1.12生成共享密钥,如下所示: // The GetCngKey method reads the private key from a certificate in my Personal certificate store CngKey cngPrivateKey = GetCngKey(); ECDiffieHellmanCng ecDiffieHellmanCng = new ECDiffieHellmanCng(cngPrivateKey); ecDiffieHellmanCng.HashAlgorithm = CngAlgorithm.ECDiffieHellmanP256; ecDiffieHellmanCng.KeyDerivationFunction = ?? // What do I set here 最后这样做: ecDiffieHellmanCng.DeriveKeyMaterial(otherPartyPublicKey:); 在哪里/如何设置其他参数算法ID,Party U Info,Party V Info? 编辑我愿意使用像Bouncy Castle这样的其他库(前提是它们可以从.NET调用)