在本篇文章中小编给各位整理的是关于C#RSA对接JAVA中RSA方式代码实例,有需要的朋友们可以参考一下。
C#中通过FromXmlString属性加载的是XML形式,而JAVA中用到的是解析后的PEM格式的字符串,总之读取证书中信息无非是转换方式问题
/// <summary>
/// c# 使用 java 的公钥进行rsa加密 utf8编码 通过解析公钥加密数据
/// </summary>
/// <param name="publickey"></param>
/// <returns></returns>
public static string RSAEncrypts(string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
X509Certificate2 x509Certificate2 = new X509Certificate2("D:\\Config\\dsptest.cer");
//创建并返回当前算法对象的xml字符串表示形式
string publicKeyString = x509Certificate2.PublicKey.Key.ToXmlString(false);
rsa.FromXmlString(publicKeyString);
//rsa.FromXmlString(RSAPublicKeyJava2DotNet(publicKeyString));
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherbytes);
}
/// <summary>
/// 私钥解密 通过解析私钥来解析数据 X509KeyStorageFlags.Exportable这个属性很关键是获取私钥信息的
/// </summary>
/// <param name="privatekey"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string RSADecrypt( string content)
{
byte[] rgb = Convert.FromBase64String(content);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
X509Certificate2 x509Certificate2 = new X509Certificate2("D:\\Config\\test.pfx", "cfca1234", X509KeyStorageFlags.Exportable);
//创建并返回当前算法对象的xml字符串表示形式
//rsa.FromXmlString(RSAPrivateKeyJava2DotNet(Const.privatekey));
rsa.FromXmlString(x509Certificate2.PrivateKey.ToXmlString(true));
cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
return Encoding.UTF8.GetString(cipherbytes);
}
/***************以下是java提供的公私钥转成.net公私钥XML的方式*************************************/
/// <summary>
/// RSA公钥格式转换,java->.net
/// </summary>
/// <param name="publicKey">java生成的公钥</param>
/// <returns></returns>
public static string RSAPublicKeyJava2DotNet(string publicKey)
{
RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
}
/// <summary>
/// RSA私钥格式转换,java->.net
/// </summary>
/// <param name="privateKey">java生成的RSA私钥</param>
/// <returns></returns>
public static string RSAPrivateKeyJava2DotNet(string privateKey)
{
RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
}
以上实例大家可以测试下,感谢大家对编程学习网的支持。
沃梦达教程
本文标题为:C#RSA对接JAVA中RSA方式代码实例
基础教程推荐
猜你喜欢
- C#控制台实现飞行棋小游戏 2023-04-22
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C# List实现行转列的通用方案 2022-11-02
- 一个读写csv文件的C#类 2022-11-06
- C# 调用WebService的方法 2023-03-09
- C# windows语音识别与朗读实例 2023-04-27
- C#类和结构详解 2023-05-30
- unity实现动态排行榜 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- ZooKeeper的安装及部署教程 2023-01-22