【问题标题】:C# sign data ECDSA from smartcardC# 从智能卡签署数据 ECDSA
【发布时间】:2020-10-09 18:08:05
【问题描述】:

我正在尝试使用智能卡中的私钥对一些数据进行签名。关键算法是 ECDSA。当我尝试获取私钥对象时,会发生系统不支持的异常。

经过一番研究,我知道 X509Certificate2 不支持 EC 密钥。

sysSec.X509Certificate2 cert = CertHelper.GetSignCertificate(serialNumber); //Get Certificate from Store var

key = cert.PrivateKey;

然后我尝试使用 Bouncy Castle 库。但是在这里我在解析 X509Certificate2 后无法获得 ECPrivateKeyParameters 。有一个代码:

 byte[] pkcs12Bytes = cert.Export(sysSec.X509ContentType.Pkcs12,"test");

            Pkcs12Store pkcs12 = new Pkcs12StoreBuilder().Build();
            pkcs12.Load(new MemoryStream(pkcs12Bytes, false), "test".ToCharArray());


            ECPrivateKeyParameters privKey = null;
            foreach (string alias in pkcs12.Aliases)
            {
                if (pkcs12.IsKeyEntry(alias))
                {
                    privKey = (ECPrivateKeyParameters)pkcs12.GetKey(alias).Key;
                    break;
                }
            }

它也不起作用。但是当我创建 CMS 文件时会发生奇怪的事情。有用。

  public  byte[] Sign(byte[] data , X509Certificate2 certificate ,bool detached )
        {

            if (data == null)
                throw new ArgumentNullException("data");
            if (certificate == null)
                throw new ArgumentNullException("certificate");

            // setup the data to sign
           // ContentInfo content = new ContentInfo( new Oid("1.3.14.3.2.26"), data);
            ContentInfo content = new ContentInfo( data);
            SignedCms signedCms = new SignedCms(content, detached);

            CmsSigner signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, certificate);

            signer.SignedAttributes.Add(new Pkcs9DocumentName("testname"));
            signer.SignedAttributes.Add(new Pkcs9SigningTime());
            //signer.;

            // CmsRecipientCollection recipest =new CmsRecipientCollection ()
            // create the signature


            signedCms.ComputeSignature(signer);
           // signedCms.ComputeSignature()
            byte[] res =  signedCms.Encode();
            foreach (SignerInfo info in signedCms.SignerInfos)
            {

                foreach (var item in info.SignedAttributes)
                {
                    string frname = item.Oid.FriendlyName ?? "null";
                    Console.WriteLine(string.Format(" OID {0}  : Value {1}", frname, item.Oid.Value.ToString()));
                }

                foreach (var item in info.UnsignedAttributes)
                {
                    string frname = item.Oid.FriendlyName ?? "null";
                    Console.WriteLine(string.Format(" OID {0}  : Value {1}", frname, item.Oid.Value.ToString()));
                }
            }
            Console.WriteLine("Signed !");
            return res; 
        }

那么有谁知道如何处理它? 还有如何使用 Bouncy Castle 从智能卡签名?

【问题讨论】:

  • 你想使用cert.GetECDsaPrivateKey(),它将使用内置提供程序(可以与智能卡对话)

标签: c# cryptography digital-signature smartcard ecdsa


【解决方案1】:

据我了解,BouncyCastle 是一个密码库。如果您提供密钥,它可以签署一些东西。然而,智能卡通常不会导出私钥(所以我有一些疑问,您的证书是否包含智能卡中的私钥),但期望命令来签署某些东西,例如。 G。通过接收相应的哈希值并返回签名(在确保适当的用户身份验证之后)。

这通常使用 PKCS#11 接口(假设您有一个与您的卡的命令集匹配的驱动程序)或通过从您的应用程序将适当的命令 APDU 直接发送到卡(相当复杂)来完成。我在充气城堡网站上一无所获,这表明对寻址智能卡有一些支持。 (如果您的卡符合该标准,它可能隐藏在 OpenPGP 功能中。)

所以在不熟悉 BouncyCastle 的情况下,在我看来,它不会符合您的期望。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多