尝试将 SignData 与 RSA 一起使用时,C# 密钥集不存在

问题描述 投票:0回答:1

我是 RSA 许可新手,我使用了在线 RSA 密钥生成器来创建私钥和公钥。我想使用这些密钥签署一些数据并使用公共验证密钥。

我正在使用 .NET Framework 4.7.2。

这些是我尝试过的功能:

public static string GenerateLicenseFromUserInfo(UserLicense info)  //  To Generate user license
{

//  Temporary license keys I am trying to use
    byte[] privateKeyBytes = Convert.FromBase64String("MIICXAIBAAKBgQC8IG4CySTEOZhmDwlTdR4Yd6xoqoJGsfze/RvrZgY72HAuWK0Y3DKrNdFiM+Z2O1weArCyoTbGsZ7UcxhCFKQLSsHooZveQTTHDHvm7cIts8jEMDw/xbPDrMQH/vYjRshNo8vOsVwxQELVzvrMA7+A/3ITKvbL0PTSDxxMcHRQDwIDAQABAoGAUNNqEH5U8o2AQZECQ74U0RRRmaJwWGlOKIv8e9WYpgumnvLwY7bvegmkTRnZUUDNogMr4YNMIm/bupE8gd+WXpo8TrHoFDNYFGB07eAnDR/ZNcSboBLGw9JKnRCD8kZ2QYwmH/wKdJL2VppC+ooVit2AcDWeCh9pPl8gF5R054kCQQDwxfnogy19yEF6eXEBQD4um/nDUbjRaudyc/i16H6SBw1+q/sQhX/QArgDwxnU5vfUWRZ4BQGw6IlQWYNzbxuzAkEAyAYgCRIWu+lWbJEErLg4gGq4D7+B3kP7SP3+QU6SBL/eVwqtgSiWUEjB84jlu73u9eqcsDJMfVF3WxnAaOQcNQJAY7vuQBUOY/ruvJfPapA88bukYvbYEs8wniVR0bBDtaN8QItmzTovbm+h39USPzGJWQmqF/8i6y/3qTPbEpbkpwJAWzrH87snWU+EloHSEwD27ENAbhZXokt5WgJWq+ytFrN4MlTxa75aSIXWyD/BIE7xpYH7MzXNwz6b5JYrNuwLnQJBAIChHs1sxLfp0HPKnPtMEJDmYlRmM176HKywg7q77yyij2K/90ijjgSG8YrXkeLrDBieCbMdxuruevZ+yBQPhr4=");
    byte[] publicKeyBytes = Convert.FromBase64String("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8IG4CySTEOZhmDwlTdR4Yd6xoqoJGsfze/RvrZgY72HAuWK0Y3DKrNdFiM+Z2O1weArCyoTbGsZ7UcxhCFKQLSsHooZveQTTHDHvm7cIts8jEMDw/xbPDrMQH/vYjRshNo8vOsVwxQELVzvrMA7+A/3ITKvbL0PTSDxxMcHRQDwIDAQAB");


    // Create an RSA object with the public and private keys
    var rsa = new RSACryptoServiceProvider();
    rsa.ImportParameters(new RSAParameters
    {
        Modulus = publicKeyBytes,
        Exponent = new byte[] { 1, 0, 1 },
        D = privateKeyBytes
    });

    string userInfo = info.ToString();

    byte[] userInfoBytes = Encoding.UTF8.GetBytes(userInfo);

    // Sign the user information using the private key
    byte[] signature = rsa.SignData(userInfoBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);  //  Exception thrown here : Keyset does not exist

    // Convert the signature to a string for distribution to the user as the license key
    return Convert.ToBase64String(signature);
}


public static bool VerifyLicenseFromUserInfo(string receivedLicenseKey, UserLicense info)  //  To Verify license is for this user
{

    byte[] publicKeyBytes = Convert.FromBase64String("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8IG4CySTEOZhmDwlTdR4Yd6xoqoJGsfze/RvrZgY72HAuWK0Y3DKrNdFiM+Z2O1weArCyoTbGsZ7UcxhCFKQLSsHooZveQTTHDHvm7cIts8jEMDw/xbPDrMQH/vYjRshNo8vOsVwxQELVzvrMA7+A/3ITKvbL0PTSDxxMcHRQDwIDAQAB");

    // Convert the public key from bytes to an RSA object
    var rsa = new RSACryptoServiceProvider();
    rsa.ImportParameters(new RSAParameters
    {
        Modulus = publicKeyBytes,
        Exponent = new byte[] { 1, 0, 1 }
    });

    string userInfo = info.ToString();

    byte[] userInfoBytes = Encoding.UTF8.GetBytes(userInfo);


    // Convert the license key back to a byte array
    byte[] receivedSignature = Convert.FromBase64String(receivedLicenseKey);

    // Verify the license key using the public key
    return rsa.VerifyData(userInfoBytes, receivedSignature, HashAlgorithmName.SHA256,RSASignaturePadding.Pkcs1);
}

当我运行程序时,使用GenerateLicenseFromUserInfo 函数时,我得到以下信息:

System.Security.Cryptography.CryptographicException:“密钥集不存在”

在GenerateLicenseFromUserInfo中的SignData函数中

我认为我的问题是将我的私钥和公钥转换为 RSAParameters,但我找不到方法来做到这一点。

c# rsa
1个回答
0
投票

也许这会有所帮助。我刚刚遇到了类似的问题。我有一个 CspBlob,其中包含使用 CryptExportKey 从旧 NT C++ 代码导出的私钥和公钥。我可以成功地将 CspBlob .ImportCspBlob 导入到新的 RSACryptoServiceProvider 中,乍一看一切看起来都很好,但是当我尝试 .SignData 时,出现“密钥集不存在”异常。我注意到 CspKeyContainerInfo.KeyNumber 为 1(交换),并使用 CspParameters 将其设置为 2(签名),现在允许我使用私钥 .SignData。

© www.soinside.com 2019 - 2024. All rights reserved.