C# USB eToken 签名和验证问题

标签 c# cryptography rsacryptoserviceprovider

我有一个带有公钥和私钥的 x509 证书,该证书存储在 safenet usb token 上。

我有一些数据要签名。我需要使用证书的公钥来验证签名。

使用我自己的自签名证书进行签名的最终代码:

RSACryptoServiceProvider rsa1 = (RSACryptoServiceProvider)useCertificate.PrivateKey;
byte[] digitalSignature = rsa1.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));

以及使用证书公钥验证的代码:

RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)useCertificate.PublicKey.Key;
Verified = rsa.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), digitalSignature);

使用自签名证书可以正常工作。我取回的签名是256
字节。

使用此代码获取签名然后验证的token,我只得到128字节的签名,验证失败:

CspParameters csp = new CspParameters(1, "SafeNet RSA CSP");
csp.Flags = CspProviderFlags.UseDefaultKeyContainer;
csp.KeyNumber = (int)KeyNumber.Signature;
RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(csp);

验证码同上。

我注意到我要使用的证书是 token 中的默认证书。为什么我只得到 128 字节而不是 256 字节的签名?我怀疑这就是它无法验证的原因。

我的 csp 中是否需要一些其他参数和设置?

谢谢

* 根据评论更新 *

很明显,当我指定 csp.keyNumber = (int)KeyNumber.Signature 时,我使用的是 1024 位 - 但这是 token 实际返回任何内容的唯一方式。即使 token key 大小为 2048 位且 key 规范为 AT_KEYEXCHANGE。当我使用我认为实际上正确的交换 key 号时,当我尝试计算签名时,系统提示我登录,但随后出现异常“参数无效”。因此,据我所知,我需要两件事之一:

1 - 如何使用公钥验证使用 1024 位的签名(没有 token - 我们需要在没有 token 的机器上验证)。

2 - 如何设置不正确的内容以便我们可以通过异常 - 我认为这是更好的主意。

有没有人对我可以如何处理此异常或可能导致它的原因有任何建议?

下面是完整的异常详细信息:

HResult = -2147024809 消息 = 参数不正确。 堆栈跟踪

在 System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 小时) 在 System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey,Int32 keyNumber,Int32 calgKey,Int32 calgHash,Byte [] hash,Int32 cbHash,ObjectHandleOnStack retSignature) 在 System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey,Int32 keyNumber,Int32 calgKey,Int32 calgHash,字节 [] 哈希) 在 System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, Int32 calgHash) 在 System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, String str) 在 TE.Program.Main(String[] args) 在 z:\Work\compusolve\enctest\TE\TE\Program.cs:line 77

最佳答案

这个问题的答案有两个。如果您正在使用这些设备之一,我发现在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\Defaults\Provider 下的注册表中

有 3 个不同的提供商。每个都具有相同的类型设置甚至图像 - 使用的 dll。但是选择不同的一个,在我的例子中是 Datakey RSP CSP,它提供了基于 2048 位 key 的 256 字节签名。您还必须确保您使用的证书是 token 中的默认证书。就我而言,有两个不同的证书。我正在使用一个进行验证,但使用另一个进行签名。

完整的测试客户端源代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;

namespace TE
{
class Program
{
    static void Main(string[] args)
    {
        try
        {

            // these variables should be changed to math your installation

            // find CSP's in this windows registry key:  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\Defaults\Provider
            string TokenCSPName = "Datakey RSA CSP";
            string TokenCertificateName = "ACME Inc";
            string NonTokenCertificateName = "SelfSigned";
            string certLocation = "Token"; // change to something else to use self signed "Token" for token
            // the certificate on the token should be installed into the local users certificate store
            // tokens will not store or export the private key, only the public key


            // find the certificate we want to use - there's no recovery if the certificate is not found

            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.OpenExistingOnly);
            X509Certificate2Collection certificates = store.Certificates;
            X509Certificate2 certificate = new X509Certificate2();
            X509Certificate2 useCertificate = new X509Certificate2();
            if (certLocation == "Token")
            {
                for (int i = 0; i < certificates.Count; i++)
                {
                    certificate = certificates[i];
                    string subj = certificate.Subject;
                    List<X509KeyUsageExtension> extensions = certificate.Extensions.OfType<X509KeyUsageExtension>().ToList();
                    if (certificate.GetNameInfo(X509NameType.SimpleName, false).ToString() == TokenCertificateName)
                    {
                        for (int j = 0; j < extensions.Count; j++)
                        {
                            if ((extensions[j].KeyUsages & X509KeyUsageFlags.DigitalSignature) == X509KeyUsageFlags.DigitalSignature)
                            {
                                useCertificate = certificate;
                                j = extensions.Count + 1;
                            }

                        }

                    }
                }
            } else
            {
                for (int i = 0; i < certificates.Count; i++)
                {
                    certificate = certificates[i];
                    string subj = certificate.Subject;
                    List<X509KeyUsageExtension> extensions = certificate.Extensions.OfType<X509KeyUsageExtension>().ToList();
                    if (certificate.GetNameInfo(X509NameType.SimpleName, false).ToString() == NonTokenCertificateName)
                        useCertificate = certificate;
                }

            }
            CspParameters csp = new CspParameters(1, TokenCSPName);

            csp.Flags = CspProviderFlags.UseDefaultKeyContainer;
            csp.KeyNumber = (int)KeyNumber.Exchange;
            RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(csp);
            string SignatureString = "Data that is to be signed";
            byte[] plainTextBytes = Encoding.ASCII.GetBytes(SignatureString);
            bool Verified = false;
            using (SHA1CryptoServiceProvider shaM = new SHA1CryptoServiceProvider())
            {
                // hash the data to be signed - you can use signData and avoid the hashing if you like

                byte[] hash = shaM.ComputeHash(plainTextBytes);
                // sign the hash
                byte[] digitalSignature = rsa1.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
                // check your signature size here - if not 256 bytes then you may not be using the proper
                // crypto provider

                // Verify the signature with the hash

                RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)useCertificate.PublicKey.Key;
                Verified = rsa.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), digitalSignature);
                if (Verified)
                {
                    Console.WriteLine("Signature Verified");
                }
                else
                {
                    Console.WriteLine("Signature Failed Verification");
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
  }
}

关于C# USB eToken 签名和验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43128705/

相关文章:

c - 在 C 中使用 libgcrypt 导出 key

c# - 如何使用使用 sha1ecdsa 的公钥根据签名验证数据?

java - JWT 数字签名是如何保护的?

windows - 微软CSP : Difference between AT_SIGNATURE and RSA_KEY_SIGN (and also AT_KEYEXCHANGE and CALG_RSA_KEYX)

c# - 在C#中使用RSA加密实现安全通信

c# - Lambda 解释和它是什么以及一个很好的例子

C# 无法从 SQL Server SELECT 转换计算列

c# - 用定时器替换代码而不是线程 sleep

rsa - 使用 SHA256 签名

c# - 将数组传递给存储过程时,对象必须实现 IConvertible 错误