将Java.Security.KeyPair转换为.NET RSACryptoServiceProvider [英] Convert Java.Security.KeyPair to .NET RSACryptoServiceProvider

查看:177
本文介绍了将Java.Security.KeyPair转换为.NET RSACryptoServiceProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对Java生成的RSA私有(和公共)密钥进行编码,以便在.NET中对其进行解码,以便在 RSACryptoServiceProvider 中使用?

How can I encode a Java-produced RSA private (and public) key such that it can be decoded in .NET for use within RSACryptoServiceProvider?

我试过这段代码:

var keyGen = KeyPairGenerator.GetInstance("RSA");
keyGen.Initialize(2048);
KeyPair keyPair = keyGen.GenerateKeyPair();

IPrivateKey privateKey = keyPair.Private;
byte[] nativePrivateKey = privateKey.GetEncoded();

var nativeToRsa = new RSACryptoServiceProvider();
nativeToRsa.ImportCspBlob(nativePrivateKey); // throws a CryptographicException

我正在使用Xamarin.Android来编写C#,但是本机 Java RSA密钥生成器比单声道生成器快。所以我想使用Java,但我仍然需要能够与Windows人员交换公钥和/或私钥,以便与 RSACryptoServiceProvider 一起使用。

I'm using Xamarin.Android to write C#, but the "native" Java RSA key generator is much faster than the mono one. So I want to use the Java one, but I still need to be able to exchange the public and/or private keys with Windows folks to use with RSACryptoServiceProvider.

所以最终我可能需要/需要双向(Java< - > RSACryptoServiceProvider)。有没有人有任何指示?

So ultimately I may want/need to go both ways (Java<->RSACryptoServiceProvider). Does anyone have any pointers?

谢谢。

推荐答案

我找到了我重写的 PvkConvert.java 文件中的答案(在相关部分中)作为C#在Xamarin.Android应用程序中用作 PvkConvert.cs

I found the answer in the PvkConvert.java file, which I've rewritten (in the relevant parts) as C# for use in Xamarin.Android apps as PvkConvert.cs.

我了解到 RSACryptoServiceProvider 的本机文件格式(当你使用ExportCspBlob时)被称为PRIVATEKEYBLOB或PUBLICKEYBLOB(取决于是否包含私钥)。将Java IPrivateKey 编码为PRIVATEKEYBLOB需要一些显然仅在最初生成的 IPrivateKey 实例上可用的字段(一个从存储中反序列化不会转换为必要的 IRSAPrivateCrtKey 接口。)

I learned that RSACryptoServiceProvider's native file format (when you use ExportCspBlob) is called PRIVATEKEYBLOB or PUBLICKEYBLOB (depending on whether the private key is included). Encoding a Java IPrivateKey as a PRIVATEKEYBLOB requires a few fields that evidently are only available on an originally generated IPrivateKey instance (one deserialized from storage does not cast to the necessary IRSAPrivateCrtKey interface.)

这篇关于将Java.Security.KeyPair转换为.NET RSACryptoServiceProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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