有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

C语言中的Java X509EncodedKeySpec#

我有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
}

共 (1) 个答案

  1. # 1 楼答案

    里面什么也没有。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签名值来自何处,所以我只是作为字节数组离开