欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET(C#)-JAVA对接RSA加密签名-第四讲

程序员文章站 2022-04-14 10:59:10
这一章呢,不讲如何实现RSA加密解密,直接贴上源码,方便大家使用 ,记住使用了BouncyCastle.Crypto.dll,不然你贴下去发现很多错误报告,别不知道怎么回事: 源...

这一章呢,不讲如何实现RSA加密解密,直接贴上源码,方便大家使用 ,记住使用了BouncyCastle.Crypto.dll,不然你贴下去发现很多错误报告,别不知道怎么回事:

源码附上:

using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml;


namespace Common
{
///


/// 加密帮助类 EncryptionHelper
/// 作者:YoungForYou
/// 时间:2018-01-19
///

public class EncryptionHelper
{
#region =======RSA秘钥=======

///
/// 生成C#格式秘钥对
///

///返回公钥
///返回私钥
public static void RSA_GetKeyForCSharp(out string publicKey, out string privateKey)
{
try
{
using (RSACryptoServiceProvider rsaservice = new RSACryptoServiceProvider())
{
publicKey = rsaservice.ToXmlString(false);
privateKey = rsaservice.ToXmlString(true);
}
}
catch (Exception ex)
{
throw ex;
}
}


///
/// 生成Java格式秘钥对
///

///返回公钥
///返回私钥
public static void RSA_GetKeyForJava(out string publicKey, out string privateKey)
{
RsaKeyPairGenerator rsaservice = new RsaKeyPairGenerator();
rsaservice.Init(new KeyGenerationParameters(new SecureRandom(), 1024));
AsymmetricCipherKeyPair rsakeyservice = rsaservice.GenerateKeyPair();
//写私钥部分
TextWriter textwriterPrivateKey = new StringWriter();
PemWriter pemwriterPrivateKey = new PemWriter(textwriterPrivateKey);
pemwriterPrivateKey.WriteObject(rsakeyservice.Private);
pemwriterPrivateKey.Writer.Flush();
//写公钥部分
TextWriter textwriterPublicKey = new StringWriter();
PemWriter pemwriterPublicKey = new PemWriter(textwriterPublicKey);
pemwriterPublicKey.WriteObject(rsakeyservice.Public);
pemwriterPublicKey.Writer.Flush();


privateKey = textwriterPrivateKey.ToString();
publicKey = textwriterPublicKey.ToString();
}


///
/// RSA私钥格式转换 C#转化为Java
///

///C#格式私钥(xml格式)
///
public static string RSA_KeyPrivateCsharpToJava(string privateKey)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(privateKey);
BigInteger modulus = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
BigInteger publicExponent = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
BigInteger privateExponent = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("D")[0].InnerText));
BigInteger p = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("P")[0].InnerText));
BigInteger q = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
BigInteger dP = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
BigInteger dQ = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
BigInteger QInv = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));
RsaPrivateCrtKeyParameters rsakeyParameters = new RsaPrivateCrtKeyParameters(modulus, publicExponent, privateExponent, p, q, dP, dQ, QInv);
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(rsakeyParameters);
byte[] keyBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
return Convert.ToBase64String(keyBytes);
}
catch (Exception ex)
{
throw ex;
}
}


///
/// RSA公钥格式转换 C#转化为Java
///

///C#格式公钥(xml格式)
///
public static string RSA_KeyPublicCsharpToJava(string publicKey)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(publicKey);
BigInteger modulus = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
BigInteger exponent = new BigInteger(1, Convert.FromBase64String(xmlDocument.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
RsaKeyParameters rsakeyParameters = new RsaKeyParameters(false, modulus, exponent);
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(rsakeyParameters);
byte[] keyBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
return Convert.ToBase64String(keyBytes);
}
catch (Exception ex)
{
throw ex;
}
}


///
/// RSA公钥格式转换 Java转化为C#
///

///Java格式公钥(pem格式)
///
public static string RSA_KeyPublicJavaToCsharp(string publicKey)
{
try
{
//RsaKeyParameters rsakeyParameters = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
//XmlDocument rsaxmlDocument = new XmlDocument();
//XmlElement rsaxmlKeyValue = rsaxmlDocument.CreateElement("RSAKeyValue");
//XmlElement rsaxmlModel = rsaxmlDocument.CreateElement("Modulus");
//XmlElement rsaxmlExponent = rsaxmlDocument.CreateElement("Exponent");
//rsaxmlModel.InnerText = Convert.ToBase64String(rsakeyParameters.Modulus.ToByteArrayUnsigned());
//rsaxmlExponent.InnerText = Convert.ToBase64String(rsakeyParameters.Exponent.ToByteArrayUnsigned());
//rsaxmlKeyValue.AppendChild(rsaxmlModel);
//rsaxmlKeyValue.AppendChild(rsaxmlExponent);
//rsaxmlDocument.AppendChild(rsaxmlKeyValue);
//return rsaxmlDocument.InnerXml;
//=============================更简单的组合方式===========================
RsaKeyParameters rsakeyParameters = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
StringBuilder rsaxmlBuilder = new StringBuilder();
rsaxmlBuilder.AppendFormat("{0}{1}"
, Convert.ToBase64String(rsakeyParameters.Modulus.ToByteArrayUnsigned())
, Convert.ToBase64String(rsakeyParameters.Exponent.ToByteArrayUnsigned()));
return rsaxmlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}

}


///
/// RSA私钥格式转换 Java转化为C#
///

///Java格式私钥(pem格式)
///
public static string RSA_KeyPrivateJavaToCsharp(string privateKey)
{
try
{
//RsaPrivateCrtKeyParameters rsaKeyParameters = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
//XmlDocument rsaxmlDocument = new XmlDocument();
//XmlElement rsaxmlKeyValue = rsaxmlDocument.CreateElement("RSAKeyValue");
//XmlElement rsaxmlModel = rsaxmlDocument.CreateElement("Modulus");
//XmlElement rsaxmlExponent = rsaxmlDocument.CreateElement("Exponent");
//XmlElement rsaxmlP = rsaxmlDocument.CreateElement("P");
//XmlElement rsaxmlQ = rsaxmlDocument.CreateElement("Q");
//XmlElement rsaxmlDP = rsaxmlDocument.CreateElement("DP");
//XmlElement rsaxmlDQ = rsaxmlDocument.CreateElement("DQ");
//XmlElement rsaxmlInverseQ = rsaxmlDocument.CreateElement("InverseQ");
//XmlElement rsaxmlD = rsaxmlDocument.CreateElement("D");
//rsaxmlModel.InnerText = Convert.ToBase64String(rsaKeyParameters.Modulus.ToByteArrayUnsigned());
//rsaxmlExponent.InnerText = Convert.ToBase64String(rsaKeyParameters.PublicExponent.ToByteArrayUnsigned());
//rsaxmlP.InnerText = Convert.ToBase64String(rsaKeyParameters.P.ToByteArrayUnsigned());
//rsaxmlQ.InnerText = Convert.ToBase64String(rsaKeyParameters.Q.ToByteArrayUnsigned());
//rsaxmlDP.InnerText = Convert.ToBase64String(rsaKeyParameters.DP.ToByteArrayUnsigned());
//rsaxmlDQ.InnerText = Convert.ToBase64String(rsaKeyParameters.DQ.ToByteArrayUnsigned());
//rsaxmlInverseQ.InnerText = Convert.ToBase64String(rsaKeyParameters.QInv.ToByteArrayUnsigned());
//rsaxmlD.InnerText = Convert.ToBase64String(rsaKeyParameters.Exponent.ToByteArrayUnsigned());
//rsaxmlKeyValue.AppendChild(rsaxmlModel);
//rsaxmlKeyValue.AppendChild(rsaxmlExponent);
//rsaxmlKeyValue.AppendChild(rsaxmlP);
//rsaxmlKeyValue.AppendChild(rsaxmlQ);
//rsaxmlKeyValue.AppendChild(rsaxmlDP);
//rsaxmlKeyValue.AppendChild(rsaxmlDQ);
//rsaxmlKeyValue.AppendChild(rsaxmlInverseQ);
//rsaxmlKeyValue.AppendChild(rsaxmlD);
//rsaxmlDocument.AppendChild(rsaxmlKeyValue);
//return rsaxmlDocument.InnerXml;
//=============================更简单的组合方式===========================
RsaPrivateCrtKeyParameters rsaKeyParameters = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
StringBuilder rsaxmlBuilder = new StringBuilder();
rsaxmlBuilder.AppendFormat("{0}{1}

{2}

{3}{4}{5}{6}{7}
"
, Convert.ToBase64String(rsaKeyParameters.Modulus.ToByteArrayUnsigned())
, Convert.ToBase64String(rsaKeyParameters.PublicExponent.ToByteArrayUnsigned())
, Convert.ToBase64String(rsaKeyParameters.P.ToByteArrayUnsigned())
, Convert.ToBase64String(rsaKeyParameters.Q.ToByteArrayUnsigned())
, Convert.ToBase64String(rsaKeyParameters.DP.ToByteArrayUnsigned())
, Convert.ToBase64String(rsaKeyParameters.DQ.ToByteArrayUnsigned())
, Convert.ToBase64String(rsaKeyParameters.QInv.ToByteArrayUnsigned())
, Convert.ToBase64String(rsaKeyParameters.Exponent.ToByteArrayUnsigned()));
return rsaxmlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}


///
/// 去除Java(Pem)格式公钥空格及头尾声明
///

///Java(Pem)格式公钥
///是否为Java(Pem)格式公钥 true:是|false:不是
///
public static string RSA_KeyJavaReplaceSpace(string key,bool isPrivateKey = false)
{
key = isPrivateKey
? key.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "")
: key.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "");
return key.Replace("\n", string.Empty).Replace("\r", string.Empty);
}


///
/// RSA秘钥格式转换 Java(pem)转化为C#(xml) 需为pem标准格式
///

///RSA秘钥
///是否为RSA私钥:true:是|false:否
///
public static string RSA_KeyJavaToCsharp(string rsakeys, bool isPrivateKey)
{
try
{
using (var memoryStream = new MemoryStream())
using (var streamWriter = new StreamWriter(memoryStream))
using (var streamReader = new StreamReader(memoryStream))
{
streamWriter.Write(rsakeys);
streamWriter.Flush();
memoryStream.Position = 0;
var pemReader = new PemReader(streamReader);
object keys = pemReader.ReadObject();
RSA rsaservice = isPrivateKey ? GetPrivateRSACSharp(keys) : GetPublicRSACsharp(keys);
return rsaservice.ToXmlString(isPrivateKey);
}
}
catch (Exception ex)
{
throw new Exception("Asymmetric cipher key is error");
}
}
#endregion


#region =======RSA加密=======


///
/// RSA C# 验证签名
///

///公钥
///待验证数据
///签名结果
///编码 可传NULL
///签名时加密方式 可传NULL
///
public static bool RSA_GetVerifyCsharp(string publicKey, string body, string signature, Encoding encoding, EncryptType encryptType = EncryptType.MD5)
{
try
{
using (RSACryptoServiceProvider rsaservice = new RSACryptoServiceProvider())
{
rsaservice.FromXmlString(publicKey);//加载公钥
RSAPKCS1SignatureDeformatter rsasignformatter = new RSAPKCS1SignatureDeformatter(rsaservice);
rsasignformatter.SetHashAlgorithm(encryptType.ToString());//设置算法
encoding = encoding ?? Encoding.UTF8;
var dataBytes = GetComputeHash(body, encoding, encryptType);
var signBytes = Convert.FromBase64String(signature);
return rsasignformatter.VerifySignature(dataBytes, signBytes);
}
}
catch (Exception ex)
{
throw ex;
}
}


///
/// RSA C# 填充签名
///

///私钥
///待签名字符串
///编码对象
///加密算法
///
public static string RSA_GetSignCsharp(string privateKey, string body,Encoding encoding, EncryptType encryptType = EncryptType.MD5)
{
try
{
using (RSACryptoServiceProvider rsaservice = new RSACryptoServiceProvider())
{
rsaservice.FromXmlString(privateKey);
RSAPKCS1SignatureFormatter rsasignformatter = new RSAPKCS1SignatureFormatter(rsaservice);
rsasignformatter.SetHashAlgorithm(encryptType.ToString());
encoding = encoding ?? Encoding.UTF8;
byte[] hashBytes = GetComputeHash(body, encoding, encryptType);
byte[] signBytes = rsasignformatter.CreateSignature(hashBytes);
return Convert.ToBase64String(signBytes);
}
}
catch (Exception ex)
{
throw ex;
}
}


///
/// 使用C#格式私钥解密数据
///

///C#格式私钥
///解密数据(base64格式)
///编码 可传NULL
///RSA加密类型枚举
///
public static string RSA_GetDecryptWithCsharp(string privateKey, string body, Encoding encoding, RSAEncryptType rsaencryptType = RSAEncryptType.Pkcs1)
{
try
{
using (RSACryptoServiceProvider rsaservice = new RSACryptoServiceProvider())
{
rsaservice.FromXmlString(privateKey);
var bytes = rsaservice.Decrypt(Convert.FromBase64String(body), GetRSAEncryptType(rsaencryptType));
encoding = encoding ?? Encoding.UTF8;
return encoding.GetString(bytes);
}
}
catch (Exception ex)
{
throw ex;
}
}


///
/// 使用C#格式公钥加密数据(加密结果为base64格式)
///

///C#格式公钥
///加密数据
///编码 可传NULL
///RSA加密类型枚举
///
public static string RSA_GetEncryptWithCsharp(string publicKey, string body, Encoding encoding, RSAEncryptType rsaencryptType = RSAEncryptType.Pkcs1)
{
try
{
using (RSACryptoServiceProvider rsaservice = new RSACryptoServiceProvider())
{
rsaservice.FromXmlString(publicKey);
encoding = encoding ?? Encoding.UTF8;
var bytes = rsaservice.Encrypt(encoding.GetBytes(body), GetRSAEncryptType(rsaencryptType));
return Convert.ToBase64String(bytes);
}
}
catch (Exception ex)
{
throw ex;
}

}


#endregion


#region =======MD5加密=======
///
/// MD5 16位加密(默认UTF8编码)
///

///字符串
///附加在字符串后的串
///编码对象默认UTF8
///
public static string MD5_16(string input, string salt, Encoding encoding = null)
{
return MD5_16(input + salt, encoding);
}


///
/// MD5 16位加密(默认UTF8编码)
///

///字符串
///编码对象默认UTF8
///
public static string MD5_16(string input, Encoding encoding = null)
{
return MD5_32(input, encoding).Substring(8, 16);
}


///
/// MD5 32位加密(默认UTF8编码)
/// 2017-11-28
///

///字符串
///
public static string MD5_32(string input)
{
return MD5_32(input, null);
}


///
/// MD5 32位加密(默认UTF8编码)
/// 2017-11-28
///

///字符串
///附加在字符串后的串
///
public static string MD5_32(string input, string salt, Encoding encoding = null)
{
return MD5_32(input + salt, encoding);
}
///
/// MD5 32位加密(默认UTF8编码)
/// 2017-11-28
///

///字符串
///编码对象默认UTF8
///
public static string MD5_32(string input, Encoding encoding = null)
{
if (encoding == null) encoding = Encoding.UTF8;
StringBuilder builder = new StringBuilder();
MD5 md5 = MD5.Create();
byte[] bytes = md5.ComputeHash(encoding.GetBytes(input));
for (int i = 0; i < bytes.Length; i++)
builder.Append(bytes[i].ToString("x2"));
return builder.ToString();
}
#endregion


#region =======Hash枚举======


///
/// RSA加密类型枚举
///

public enum RSAEncryptType
{
///
/// SHA1加密算法
///

SHA1,
///
/// SHA256加密算法
///

SHA256,
///
/// SHA384加密算法
///

SHA384,
///
/// SHA512加密算法
///

SHA512,
///
/// Pkcs1填充算法
///

Pkcs1
}


///
/// RSA签名类型枚举
///

public enum EncryptType
{
///
/// MD5算法
///

MD5,
///
/// SHA1算法
///

SHA1,
///
/// SHA256算法
///

SHA256,
///
/// SHA384算法
///

SHA384,
///
/// SHA512算法
///

SHA512
}
#endregion


#region =======辅助函数======
///
/// 根据加密类型获取加密对象
///

///RSA加密类型枚举
///
private static RSAEncryptionPadding GetRSAEncryptType(RSAEncryptType rsaencryptType)
{
switch (rsaencryptType)
{
case RSAEncryptType.Pkcs1:
return RSAEncryptionPadding.Pkcs1;
case RSAEncryptType.SHA1:
return RSAEncryptionPadding.OaepSHA1;
case RSAEncryptType.SHA256:
return RSAEncryptionPadding.OaepSHA256;
case RSAEncryptType.SHA384:
return RSAEncryptionPadding.OaepSHA384;
case RSAEncryptType.SHA512:
return RSAEncryptionPadding.OaepSHA512;
default:
return RSAEncryptionPadding.Pkcs1;
}
}


///
/// 根据编码及Hash类型计算Hash值
///

///待计算Hash值得字符串
///编码对象
///Hash类型
///
public static byte[] GetComputeHash(string body, Encoding encoding, EncryptType encryptType)
{
encoding = encoding ?? Encoding.UTF8;
byte[] sourceBytes = encoding.GetBytes(body);
return GetComputeHash(sourceBytes, encryptType);
}


///
/// 获取传入对象的Hash计算值
///

///待计算Hash值的字节数组
///Hash类型
///
public static byte[] GetComputeHash(byte[] sourceBytes, EncryptType encryptType)
{
switch (encryptType)
{
case EncryptType.MD5:
return MD5.Create().ComputeHash(sourceBytes);
case EncryptType.SHA1:
return SHA1.Create().ComputeHash(sourceBytes);
case EncryptType.SHA256:
return SHA256.Create().ComputeHash(sourceBytes);
case EncryptType.SHA384:
return SHA384.Create().ComputeHash(sourceBytes);
case EncryptType.SHA512:
return SHA512.Create().ComputeHash(sourceBytes);
}
return null;
}


///
/// 传入RSA秘钥对象返回读取秘钥后的RSA对象
///

///RSA私钥
///
private static RSA GetPrivateRSACSharp(object rsakeys)
{
//根据传入对象 强制转化为需要的对象 提供已经读取私钥的RSA对象
if ((rsakeys as RsaPrivateCrtKeyParameters) != null)
return DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)rsakeys);
var keys = (AsymmetricCipherKeyPair)rsakeys;
return DotNetUtilities.ToRSA((RsaPrivateCrtKeyParameters)keys.Private);
}


///
/// 传入RSA秘钥对象返回读取秘钥后的RSA对象
///

///RSA公钥
///
private static RSA GetPublicRSACsharp(object rsakeys)
{
//提供已经读取公钥钥的RSA对象
var keys = (RsaKeyParameters)rsakeys;
return DotNetUtilities.ToRSA(keys);
}
#endregion
}
}

 

 

下面附上单元测试方法(这个建议你们自己去思考自己去写):

///


/// 单元测试函数
///

public void EncryptionHelperTest()
{
try
{
string pub = "";
string pri = "";
EncryptionHelper.RSA_GetKeyForCSharp(out pub, out pri);
string strData = "测试数据00001**";
string strA = EncryptionHelper.RSA_GetEncryptWithCsharp(pub, strData, null);
string strB = EncryptionHelper.RSA_GetDecryptWithCsharp(pri, strA, null);
string result = EncryptionHelper.RSA_GetSignCsharp(pri, strData, Encoding.UTF8, EncryptionHelper.EncryptType.MD5);
bool verify = EncryptionHelper.RSA_GetVerifyCsharp(pub, strData, result, Encoding.UTF8, EncryptionHelper.EncryptType.SHA1);


string javaPubKey = EncryptionHelper.RSA_KeyPublicCsharpToJava(pub);
string javaPriKey = EncryptionHelper.RSA_KeyPrivateCsharpToJava(pri);


string cPub = EncryptionHelper.RSA_KeyPublicJavaToCsharp(javaPubKey);
string cPri = EncryptionHelper.RSA_KeyPrivateJavaToCsharp(javaPriKey);


bool isPubSame = cPub == pub;
bool isPriSame = cPri == pri;




string javaPub = "", javaPri = "";
EncryptionHelper.RSA_GetKeyForJava(out javaPub, out javaPri);


string strKeyPri = EncryptionHelper.RSA_KeyJavaReplaceSpace(javaPri, false);


string strCsharpPri = EncryptionHelper.RSA_KeyJavaToCsharp(javaPri, true);
string strCsharpPub = EncryptionHelper.RSA_KeyJavaToCsharp(javaPub, false);
string strCsharpEn = EncryptionHelper.RSA_GetEncryptWithCsharp(strCsharpPub, strData, Encoding.GetEncoding("GBK"));
string strCsharpDen = EncryptionHelper.RSA_GetDecryptWithCsharp(strCsharpPri, strCsharpEn, Encoding.GetEncoding("GBK"));
string strCsharpresult = EncryptionHelper.RSA_GetSignCsharp(strCsharpPri, strData, Encoding.UTF32, EncryptionHelper.EncryptType.MD5);
bool strCsharpVerify = EncryptionHelper.RSA_GetVerifyCsharp(strCsharpPub, strData, strCsharpresult, Encoding.UTF32, EncryptionHelper.EncryptType.MD5);


string strPcks8Pub = @"-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC5blXLIqSaKHfFAWnrjJWYTK3J
BUJ2pHmj1lKyX9ybiSwyxGCH1ffjNljCoiQXWUqBvIyOYM/rsQVZIic52ojnXW4Z
5i7Em9A9rYsPz8O+etgu/JaqQLfNoySxPUHwUZ06tO6U4HdTE9i/FM1tkl1OLX+S
LgSdS/rI9qhDxSREOwIDAQAB
-----END PUBLIC KEY-----";
string strPcks8Pri = @"-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALluVcsipJood8UB
aeuMlZhMrckFQnakeaPWUrJf3JuJLDLEYIfV9+M2WMKiJBdZSoG8jI5gz+uxBVki
JznaiOddbhnmLsSb0D2tiw/Pw7562C78lqpAt82jJLE9QfBRnTq07pTgd1MT2L8U
zW2SXU4tf5IuBJ1L+sj2qEPFJEQ7AgMBAAECgYEAjFvHFw7u9z1ggz+xtDli1rhb
50hS6clPxcMqL5i+PQEU8oyQ4OEGVPdDMnhWx1GwQwm8ft0rJSWGfdSLSpBvhR7k
WQd/4y1JAazSuN//VYuY2uZBvBToSDvAOpC6p2jg6Lw/H2T+gkZPvsMGLz4IKRKm
rIxql/qGXgPo/NuGw1kCQQDr1Yv8mi3aPC0UzEGkKb9ithjUv1Or9gEeBIfUaskC
htBCzJdDTlT9qrRKgtndSXfXHlfKrqoZBI0zQKQ/67odAkEAyUl0It5F552t3Zxp
aWs5Nn7uzPSKQsOerwmOjog04OMkd8IueTehtAP6YColid2z0cPVxvmixWa9waUf
bBnoNwJAY45IhCBKDwCsP0ViyHxfMLqSjO0f004MdrGO7ZsOtCRZMnnPuTgOJO+1
IGdQtVvYXVWP1h4Z7GnBectmCx3l+QJAY2auv3lctCCd986CD9kvi8XrT7ODg6zu
9CmYCLMiydvh6nIuWRYr+KXuQwcNOqQNZQ18yKIGy80M8FAG0G+KyQJBAM4U/dry
Z+tjwLQ2eHHy4WzEUv4w1yaBDxv8R5yQ3SBYJRy/1C8wpFrjlDtw3lRWR+bfYff9
IL8dfT6ojXyrMww=
-----END PRIVATE KEY-----";
string strPcks8PubCsharp = EncryptionHelper.RSA_KeyJavaToCsharp(strPcks8Pub, false);
string strPcks8PriCsharp = EncryptionHelper.RSA_KeyJavaToCsharp(strPcks8Pri, true);
string strCsharpEnPCKS8 = EncryptionHelper.RSA_GetEncryptWithCsharp(strPcks8PubCsharp, strData, Encoding.GetEncoding("GB2312"));
string strCsharpDenPCKS8 = EncryptionHelper.RSA_GetDecryptWithCsharp(strPcks8PriCsharp, strCsharpEnPCKS8, Encoding.GetEncoding("GB2312"));
string strCsharpresultPCKS8 = EncryptionHelper.RSA_GetSignCsharp(strPcks8PriCsharp, strData, Encoding.ASCII, EncryptionHelper.EncryptType.SHA512);
bool strCsharpVerifyPCKS8 = EncryptionHelper.RSA_GetVerifyCsharp(strPcks8PubCsharp, strData, strCsharpresultPCKS8, Encoding.ASCII, EncryptionHelper.EncryptType.SHA512);


}
catch (Exception ex)
{
string s = ex.Message;
}
}