CryptSignHash错误2148073494“键集不存在”?

时间:2015-03-18 00:15:09

标签: c# hash rsa cryptoapi

我使用了一种名为CA-Visual Objects的奇怪语言。我正在尝试验证我在C#中创建的哈希签名。但是在调用CryptSignHash函数时,我得到错误代码2148073494,我相信它映射到" Keyset不存在"。

CryptAcquireContext( @hCryptProvider, NULL_PTR, NULL_PTR, PROV_RSA_FULL, 0) //returns true
CryptCreateHash(hCryptProvider, CALG_SHA1, 0, 0, @hHash) //returns true
CryptHashData(hHash, pData, dwDataLen, 0) //returns true
CryptImportKey(hCryptProvider, pKeyBlob, cbKeyBlob, 0, 0, @hKey) //returns true
CryptSignHashA(hHash, AT_SIGNATURE, null, 0, NULL, @dwSignLen) //returns false with error code 2148073494 

我对这个API不太熟悉,有人可以告诉我什么" Keyset"不完全存在吗?是AT_SIGNATURE吗?或其他什么?

这是签署数据的C#代码:

 private static void CreateSigningKey()
    {
        CspParameters p = new CspParameters();
        p.KeyNumber = (int)KeyNumber.Signature;

        using (RSACryptoServiceProvider rsaCreateSigningKey = new RSACryptoServiceProvider(2048, p))
        {
            rsaCreateSigningKey.PersistKeyInCsp = false;
            var parameters = rsaCreateSigningKey.ExportParameters(false);
            byte[] ex = parameters.Exponent;
            byte[] mod = parameters.Modulus;
            File.WriteAllBytes(publicSigningKeyPathMod, mod);
            File.WriteAllBytes(publicSigningKeyPathExp, ex);
            File.WriteAllText(privateSigningKeyPath, rsaCreateSigningKey.ToXmlString(true));
        }
    }


private static string Sign(byte[] plainBytes)
    {
        string signatureText;

        CspParameters parameters = new CspParameters();
        parameters.KeyNumber = (int) KeyNumber.Signature;

        using (RSACryptoServiceProvider rsaSign = new RSACryptoServiceProvider(2048)) //signing with private sign key
        {
            rsaSign.PersistKeyInCsp = false;
            rsaSign.FromXmlString(File.ReadAllText(privateSigningKeyPath)); //private sign key is held here at BP

            SHA1Managed hash = new SHA1Managed();
            byte[] hashByte = hash.ComputeHash(plainBytes);

            RSAPKCS1SignatureFormatter signFormatter = new RSAPKCS1SignatureFormatter(rsaSign);
            signFormatter.SetHashAlgorithm("SHA1");
            byte[] signature = signFormatter.CreateSignature(hashByte);

            signatureText = Convert.ToBase64String(signature);
        }
        return signatureText;
    }

1 个答案:

答案 0 :(得分:0)

问题确实是您怀疑的 $(function() { var start = moment().subtract(1, "days"); $("#demo").daterangepicker({ singleDatePicker: true, startDate: start, minDate: start }, function(start, end, label) { console.log(start.format("YYYY-MM-DD")); } ); }); 参数:<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" /> <script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script> <input type="text" id="demo" value="10/24/1984" /> 将创建一个具有 keySpec 的密钥,该密钥取决于输入密钥 blob 参数,在您的情况下,密钥 blob 与 {{ 1}}。因此,解决您的问题的方法是在 AT_SIGNATURE 调用中指定 CryptImportKey