代码:
RSAHelper.cs(RSA加密工具类):
using System; System.Security.Cryptography; System.Text; namespace Utils { /// <summary> /// RSA加密工具类 </summary> public class RSAHelper { <summary> RSA加密 </summary> 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 - ); byte[] subBytes = page == pageCount ? new byte[bytes.Length - start] : byte[pageSize]; Array.copy(bytes,start,subBytes,0,subBytes.Length); string strEncrypted = RSAEncrypt(publicKey,Encoding.UTF8.GetString(subBytes)); result.Append(strEncrypted); } return result.ToString(); } RSA解密 string RSADecrypt_LongContent(string privateKey,1)">172int pageCount = (content.Length - string subContent = null; if (page != pageCount) { subContent = content.Substring(start,pageSize); } else { subContent = content.Substring(start,content.Length - start); } RSADecrypt(privateKey,subContent); result.Append(strEncrypted); } string RSAEncrypt( content) { RSACryptoServiceProvider rsa = RSACryptoServiceProvider(); rsa.FromXmlString(publicKey); byte[] cipherBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content),1)">false); Convert.ToBase64String(cipherBytes); } string RSADecrypt( RSACryptoServiceProvider(); rsa.FromXmlString(privateKey); byte[] cipherBytes = rsa.Decrypt(Convert.FromBase64String(content),1)"> Encoding.UTF8.GetString(cipherBytes); } 创建公钥私钥 static RSAKey CreateKey() { RSAKey rsaKey = RSAKey(); using (RSACryptoServiceProvider rsa = 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); } rsaKey; } } RSAKey { string PublicKey_NET { get; set; } string PrivateKey_NET { string PublicKey_Java { string PrivateKey_Java { ; } } }
RSAKeyConvert.cs(Java .NET RSA密钥格式转换)(需要安装NuGet包BouncyCastle):
System.Xml; Org.BouncyCastle.Asn1.Pkcs; Org.BouncyCastle.Asn1.X509; Org.BouncyCastle.Crypto.Parameters; Org.BouncyCastle.Math; Org.BouncyCastle.Pkcs; Org.BouncyCastle.Security; Org.BouncyCastle.X509; <summary> RSA密钥格式转换 </summary> RSAKeyConvert { <summary> RSA私钥格式转换,java->.net </summary> <param name="privateKey">java生成的RSA私钥</param> string RSAPrivateKeyJava2DotNet( 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>"aram.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())); } RSA私钥格式转换,.net->java .net生成的私钥string RSAPrivateKeyDotNet2Java( privateKey) { XmlDocument doc = XmlDocument(); doc.LoadXml(privateKey); BigInteger m = new BigInteger(1,Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName(Modulus")[].InnerText)); BigInteger exp = Exponent].InnerText)); BigInteger d = D].InnerText)); BigInteger p = P].InnerText)); BigInteger q = Q].InnerText)); BigInteger dp = DP].InnerText)); BigInteger dq = DQ].InnerText)); BigInteger qinv = InverseQ].InnerText)); RsaPrivateCrtKeyParameters privateKeyParam = RsaPrivateCrtKeyParameters(m,exp,d,p,q,dp,dq,qinv); PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam); byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded(); Convert.ToBase64String(serializedPrivateBytes); } RSA公钥格式转换,java->.net <param name="publicKey">java生成的公钥string RSAPublicKeyJava2DotNet( publicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); <RSAkeyvalue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAkeyvalue>aram.Modulus.ToByteArrayUnsigned()),Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); } RSA公钥格式转换,.net->java .net生成的公钥</param> string RSAPublicKeyDotNet2Java( publicKey) { XmlDocument doc = XmlDocument(); doc.LoadXml(publicKey); BigInteger m = ].InnerText)); RsaKeyParameters pub = new RsaKeyParameters( SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub); byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded(); Convert.ToBase64String(serializedPublicBytes); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。