Bouncy Castle,RSA:将密钥转换为字符串格式
我在我的 C# 项目中使用 RSA (Bouncy Castle API)。我用这种方法生成了密钥对:
RsaKeyPairGenerator r = new RsaKeyPairGenerator();
r.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
AsymmetricKeyParameter private_key = keys.Private;
AsymmetricKeyParameter public_key = keys.Public;
现在我想将它们保存在txt文件中,但问题是我无法将它们转换为字符串格式。我在另一篇文章中读到密钥必须使用以下方式序列化:
PrivateKeyInfo k = PrivateKeyInfoFactory.CreatePrivateKeyInfo(private_key);
byte[] serializedKey = k.ToAsn1Object().GetDerEncoded();
这是正确的方法吗?如果是,之后我应该做什么?只是将它们从 byte[] 转换为 String 吗?
I'm using RSA (Bouncy Castle API) in my C# project. I generated the keypair with this method:
RsaKeyPairGenerator r = new RsaKeyPairGenerator();
r.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair keys = r.GenerateKeyPair();
AsymmetricKeyParameter private_key = keys.Private;
AsymmetricKeyParameter public_key = keys.Public;
Now I want to save them in a txt file but the problem is that I can't convert them to a string format. I read in another post that keys must be serialized using:
PrivateKeyInfo k = PrivateKeyInfoFactory.CreatePrivateKeyInfo(private_key);
byte[] serializedKey = k.ToAsn1Object().GetDerEncoded();
Is it the right way? If yes, what should I do after this? Just convert them from byte[] to String?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您还可以使用 PemWriter 将它们存储为 PEM 格式:
现在 privateKey 包含如下内容:
You could also use PemWriter to store them in PEM format:
Now privateKey contain something like this:
好吧,我不知道 RSA 特定的方面,但是一旦你有了一个不透明的二进制字符串(即它可以包含任何任意数据),文本转换的最佳选择是
Convert.ToBase64String(byte[])
您可以使用Convert.FromBase64String(string)
。不要为此使用
Encoding.GetString(byte[])
和Encoding.GetBytes(string)
- 二进制数据不是' t 特定编码中的文本,不应如此对待。如果您尝试这样做,您几乎肯定会丢失数据。Well, I don't know about the RSA-specific side, but once you've got an opaque binary string (i.e. it could contain any arbitrary data) the best bet for text conversion is
Convert.ToBase64String(byte[])
which you can reverse withConvert.FromBase64String(string)
.Do not use
Encoding.GetString(byte[])
andEncoding.GetBytes(string)
for this - the binary data isn't text in a particular encoding, and shouldn't be treated as such. You're almost bound to lose data if you try this.这可能就是您正在寻找的:
http ://www.rahulsingla.com/blog/2011/04/serializing-deserializing-rsa-public-private-keys- generated-using-bounty-castle-library
This might be what you are looking out for:
http://www.rahulsingla.com/blog/2011/04/serializing-deserializing-rsa-public-private-keys-generated-using-bounty-castle-library
尝试以下操作
Try the following
如果将 bouncycastle 证书转换为 .net 证书。执行此操作的函数位于 bouncycastle 库中(我相信它位于名为 DotNetUtilities 的类中)。 RSACryptoServiceProvider 有一个函数:
如果您希望包含单独序列化为 Base64 的所有组件的私钥、指数、模数和 d(私有指数),它会为您提供证书的 x 表示形式。
If you convert the bouncycastle certificate to a .net certificate. The function to do this is in the bouncycastle lib (i believe it's in a class called DotNetUtilities). The RSACryptoServiceProvider has a function:
Which gives you an x representation of a certificate with if you want the private key containing all the components serialized to base64 seperately, exponent, modulus, and d (private exponent).