简单使用的RSACryptoServiceProvider KeyPassword失败 [英] Simple use of RSACryptoServiceProvider KeyPassword fails
问题描述
我要保护我的RSA私钥用密码(谁不会),但下面的C#失败:
I want to protect my RSA private key with a password (who wouldn't) but the following C# fails:
SecureString pw = new SecureString();
pw.AppendChar('x');
CspParameters prms = new CspParameters();
prms.KeyPassword = pw;
RSACryptoServiceProvider crypto = new RSACryptoServiceProvider(prms);
byte[] encrypted = crypto.Encrypt(Encoding.ASCII.GetBytes("encryptme"), true);
......与CryptographicException:指定了无效的类型。如果我参加了KeyPassword分配出来,它工作正常。
...with the CryptographicException: "Invalid type specified". If I take the KeyPassword assignment out it works fine.
我是什么,还是微软,做错了吗?
What am I, or Microsoft, doing wrong?
推荐答案
设置 CspParameters.KeyPassword
等同于调用 CryptSetProvParam
与 PP_KEYEXCHANGE_PIN
(或 PP_SIGNATURE_PIN
)。这个标志是不支持默认的Microsoft加密服务提供者(它的目的是为使用基于智能卡的电信运营商)。
Setting CspParameters.KeyPassword
is equivalent to calling CryptSetProvParam
with PP_KEYEXCHANGE_PIN
(or PP_SIGNATURE_PIN
). This flag is not supported by the default Microsoft crypto-service-provider (it is intended for use with smartcard-based CSPs).
您可能想尝试设置
prms.Flags = CspProviderFlags.UseUserProtectedKey;
或可替换地产生非持久密钥对,出口并与从自己一个密码派生的密钥进行加密。
or alternatively generating a non-persistent key-pair, exporting it and encrypting it with a key derived from a password yourself.
这篇关于简单使用的RSACryptoServiceProvider KeyPassword失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!