简单使用的RSACryptoServiceProvider KeyPassword失败 [英] Simple use of RSACryptoServiceProvider KeyPassword fails

查看:366
本文介绍了简单使用的RSACryptoServiceProvider KeyPassword失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保护我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆