RSA:如何在java中生成私钥并在C#中使用它?
我想在java中生成私钥,将其保存为某个文件中的64基编码字符串 然后使用这个保存的文件加密 C# 中的一些短语。 我知道在java中生成密钥并用64进制对其进行编码。 我的问题是如何在 C# 中使用这个键? 这是一个将私钥保存到文本文件中的 java 代码原型:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
keyGen.initialize(spec);
KeyPair keyPair = keyGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
writeToFile("privateKey", Base64.encode(keyPair.getPrivate().getEncoded()));
我想在 C# 中实现以下函数,但找不到如何创建 RSAParameters 或 来自私钥的 RSACryptoServiceProvider
public static string DecryptData(string privateKey64Base, string data64Base)
{
// create using privateKey64Base
// create RSACryptoServiceProvider rsa using RSAParameters above
// byte[] encryptedData = rsa.Encrypt(Convert.FromBase64String(data64Base);
}
I would like to generate private key in java, save it as a 64 base encoded string in some file
and then encrypt some phrase in C# using this saved file.
I know to generate keys in java and encode it with 64 base.
My question is how do I use this key in C#?
This is a java code prototype to save private key into text file:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4);
keyGen.initialize(spec);
KeyPair keyPair = keyGen.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
writeToFile("privateKey", Base64.encode(keyPair.getPrivate().getEncoded()));
I would like to implement following function in C# but can't find how to create RSAParameters or
RSACryptoServiceProvider from private key
public static string DecryptData(string privateKey64Base, string data64Base)
{
// create using privateKey64Base
// create RSACryptoServiceProvider rsa using RSAParameters above
// byte[] encryptedData = rsa.Encrypt(Convert.FromBase64String(data64Base);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此页面包含针对您情况的建议,因为您正在写出 PKCS#8 密钥(使用keyPair.getPrivate().getEncoded())
使用此方法,您将首先使用 Java 端的实用程序将私钥转换为 PRIVATEKEYBLOB 格式。
或者,您可以使用 BouncyCastle C#,它可以读取密钥(参见例如 Org.BouncyCastle.Security.PrivateKeyFactory.CreateKey - 当然,您需要首先进行 Base64 解码)。
上一个问题提供了从生成的 BC 密钥对象转换为 RSACryptoServiceProvider 的答案: BouncyCastle RSAPrivateKey 到 .NET RSAPrivateKey
第三,您可能需要考虑使用密钥库,例如 PKCS#12,这是一种更标准(且安全)的私钥存储方式。
This page contains advice for your situation, since you are writing out PKCS#8 keys (with keyPair.getPrivate().getEncoded())
Using this approach you would use the utility on the Java side to get the private key into the PRIVATEKEYBLOB format in the first place.
Alternatively, you could use BouncyCastle C# which can read the key in (see e.g. Org.BouncyCastle.Security.PrivateKeyFactory.CreateKey - you'd need to Base64 decode first of course).
This previous question has the answer for converting from the resulting BC key object to RSACryptoServiceProvider: BouncyCastle RSAPrivateKey to .NET RSAPrivateKey
Thirdly, you might want to look at using a keystore, e.g. PKCS#12, which is a more standard (and secure) way for storing private keys.
这是询问者的示例代码:
归功于@peter-dettman
here is a sample code for whom asked:
credit to @peter-dettman