如何将受密码保护的RSA私钥导入RSACryptoServiceProvider

时间:2014-01-17 20:53:42

标签: c# encryption cryptography cryptoapi rsacryptoserviceprovider

我正在寻找与此问题相同的解决方案: How can I import an RSA private key into an RSACryptoServiceProvider?

不幸的是,没有提供解决方案的实际方法,我在最后的步骤中遇到了麻烦。概述:

我有一个使用CAPI创建的现有私钥:

// Abbreviated for clarity.
CryptAcquireContext(..., MS_ENHANCED_PROV, ...);

// Generate public/private key pair

CryptCreateHash(..., CALG_SHA1, ...);
CryptHashData(hash, password, ...);
CryptDeriveKey(..., CALG_3DES, hash, CRYPT_EXPORTABLE, ...);
CyrptExportKey(..., derivedKey, PRIVATEKEYBLOB, ...);

我需要将此密钥导入C#RSACryptoService提供程序。

基于另一个问题,我知道我必须使用PasswordDerivedBytes派生密钥,然后使用派生密钥解密密钥,但我不知道如何执行这些步骤。

我已经开始使用以下内容:

var parameters = new CspParameters
{
    ProviderName = "Microsoft Enhanced Cryptographic Provider v1.0",
    ProviderType = 1, 
    Flags = CspProviderFlags.UseMachineKeyStore, 
    KeyContainerName = "KeyContainer"
};

var csp = new RSACryptoServiceProvider(parameters);
byte[] pwd = Encoding.ASCII.GetBytes("Password");

PasswordDeriveBytes pdb = new PasswordDeriveBytes(pwd, new byte[0], parameters);
// This line throws a CryptographicException with "Invalid flags specified."
byte[] symKey = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, new byte[8]);

如果我从CspParamaters中删除KeyContainerName,那么我可以生成一个密钥,但我的印象是我必须使用相同的Csp来获得相同的密钥。

一旦我把钥匙拿出来,我不知道我应该用它来解密钥匙。

0 个答案:

没有答案