我在Java中有代码示例,我需要在C#中使用相同的功能。是否有用于样本的类的替代方法?
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import javax.xml.bind.DatatypeConverter;
publicKey = publicKey.replaceAll("-----(BEGIN|END).*", "").trim();
X509EncodedKeySpec spec = new X509EncodedKeySpec(DatatypeConverter.parseBase64Binary(publicKey));
KeyFactory keyFactory = KeyFactory.getInstance("EC");
PublicKey pKey = keyFactory.generatePublic(spec);
Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initVerify(pKey);
ecdsaSign.update(stringToVerify.getBytes("UTF-8"));
if (ecdsaSign.verify(new BigInteger(ECDSA, 16).toByteArray())) {
// true
}
答案 0 :(得分:0)
.NET(本身)中没有任何东西只能读取公钥结构。但是,如果您可以获得完整的证书,那么您可以在.NET 4.6.1中编写以下内容:
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace Demo
{
public static class DemoClass
{
public static bool ECDsaSha256Verify(string pemCert, string data, byte[] signature)
{
using (var cert = new X509Certificate2(Encoding.ASCII.GetBytes(pemCert)))
using (ECDsa ecdsa = cert.GetECDsaPublicKey())
{
if (ecdsa == null)
{
throw new ArgumentException("Certificate does not have an ECDSA key.");
}
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
return ecdsa.VerifyData(dataBytes, signature, HashAlgorithmName.SHA256);
}
}
}
}
我不确定你的BigInteger签名值来自哪里,所以我刚刚离开了一个字节数组。