代码:
RSAHelper.cs(RSA加密工具类):
using System;
using System.Security.Cryptography;
using System.Text;
namespace Utils
{
/// <summary>
/// RSA加密工具类
/// </summary>
public class RSAHelper
{
/// <summary>
/// RSA加密
/// </summary>
public static string RSAEncrypt_LongContent(string publicKey, string content)
{
byte[] bytes = Encoding.UTF8.GetBytes(content);
int pageSize = 117;
int pageCount = (bytes.Length - 1) / pageSize + 1;
StringBuilder result = new StringBuilder();
for (int page = 1; page <= pageCount; page++)
{
int start = pageSize * (page - 1);
byte[] subBytes = page == pageCount ? new byte[bytes.Length - start] : new byte[pageSize];
Array.Copy(bytes, start, subBytes, 0, subBytes.Length);
string strEncrypted = RSAEncrypt(publicKey, Encoding.UTF8.GetString(subBytes));
result.Append(strEncrypted);
}
return result.ToString();
}
/// <summary>
/// RSA解密
/// </summary>
public static string RSADecrypt_LongContent(string privateKey, string content)
{
int pageSize = 172;
int pageCount = (content.Length - 1) / pageSize + 1;
StringBuilder result = new StringBuilder();
for (int page = 1; page <= pageCount; page++)
{
int start = pageSize * (page - 1);
string subContent = null;
if (page != pageCount)
{
subContent = content.Substring(start, pageSize);
}
else
{
subContent = content.Substring(start, content.Length - start);
}
string strEncrypted = RSADecrypt(privateKey, subContent);
result.Append(strEncrypted);
}
return result.ToString();
}
/// <summary>
/// RSA加密
/// </summary>
public static string RSAEncrypt(string publicKey, string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicKey);
byte[] cipherBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherBytes);
}
/// <summary>
/// RSA解密
/// </summary>
public static string RSADecrypt(string privateKey, string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privateKey);
byte[] cipherBytes = rsa.Decrypt(Convert.FromBase64String(content), false);
return Encoding.UTF8.GetString(cipherBytes);
}
/// <summary>
/// 创建公钥私钥
/// </summary>
public static RSAKey CreateKey()
{
RSAKey rsaKey = new RSAKey();
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsaKey.PublicKey_NET = rsa.ToXmlString(false); // 公钥
rsaKey.PrivateKey_NET = rsa.ToXmlString(true); // 私钥
rsaKey.PublicKey_Java = RSAKeyConvert.RSAPublicKeyDotNet2Java(rsaKey.PublicKey_NET);
rsaKey.PrivateKey_Java = RSAKeyConvert.RSAPrivateKeyDotNet2Java(rsaKey.PrivateKey_NET);
}
return rsaKey;
}
}
public class RSAKey
{
public string PublicKey_NET { get; set; }
public string PrivateKey_NET { get; set; }
public string PublicKey_Java { get; set; }
public string PrivateKey_Java { get; set; }
}
}
View Code
RSAKeyConvert.cs(Java .NET RSA密钥格式转换)(需要安装NuGet包BouncyCastle):
using System;
using System.Xml;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
namespace Utils
{
/// <summary>
/// RSA密钥格式转换
/// </summary>
public class RSAKeyConvert
{
/// <summary>
/// RSA私钥格式转换,java->.net
/// </summary>
/// <param name="privateKey">java生成的RSA私钥</param>
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()));
}
/// <summary>
/// RSA私钥格式转换,.net->java
/// </summary>
/// <param name="privateKey">.net生成的私钥</param>
public static string RSAPrivateKeyDotNet2Java(string privateKey)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(privateKey);
BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));
RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
return Convert.ToBase64String(serializedPrivateBytes);
}
/// <summary>
/// RSA公钥格式转换,java->.net
/// </summary>
/// <param name="publicKey">java生成的公钥</param>
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公钥格式转换,.net->java
/// </summary>
/// <param name="publicKey">.net生成的公钥</param>
public static string RSAPublicKeyDotNet2Java(string publicKey)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(publicKey);
BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
RsaKeyParameters pub = new RsaKeyParameters(false, m, p);
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
return Convert.ToBase64String(serializedPublicBytes);
}
}
}
View Code