C# RSA 非对称加密

C# RSA 非对称加密,第1张

概述代码: RSAHelper.cs(RSA加密工具类): using System; using System.Security.Cryptography; using System.Text; nam

代码:

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 { ; }    }}
VIEw Code

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>" 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> 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);        }    }}
VIEw Code

 

总结

以上是内存溢出为你收集整理的C# RSA 非对称加密全部内容,希望文章能够帮你解决C# RSA 非对称加密所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1212648.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存