TOP

.NET core RSA帮助类(一)
2017-10-16 18:19:48 】 浏览:10159次 本网站的内容取自网络,仅供学习参考之用,绝无侵犯任何人知识产权之意。如有侵犯请您及时与本人取得联系,万分感谢。
Tags:.NET core RSA 帮助

解决 Operation is not supported on this platform 异常

直接上代码:

public class RSAHelper
    {
        /// <summary>
        /// 私钥签名
        /// </summary>
        /// <param name="signStr"></param>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string RSASign(string signStr, string privateKey)
        {
            try
            {
                //// net
                //var rsa = new RSACryptoServiceProvider();
                //rsa.FromXmlString(privateKey);
                //byte[] signBytes = rsa.SignData(UTF8Encoding.UTF8.GetBytes(signStr), "md5");
                //return Convert.ToBase64String(signBytes);

                // net core 2.0
                var rsa = RSA.Create();
                rsa.FromXmlStringExtensions(privateKey);
                byte[] bytes = rsa.SignData(UTF8Encoding.UTF8.GetBytes(signStr), HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1);
                return Convert.ToBase64String(bytes);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 公钥签名
        /// </summary>
        /// <param name="encrypt_info"></param>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public static string RSAEncrypt(string strEncryptInfo, string publicKey)
        {
            try
            {
                //// net
                //RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                //rsa.FromXmlString(strPublicKey);
                //byte[] bytes = rsa.Encrypt(UTF8Encoding.UTF8.GetBytes(strEncryptInfo), false);
                //return Convert.ToBase64String(bytes);

                // net core 2.0
                var rsa = RSA.Create();
                publicKey = RSAPublicKeyJava2DotNet(publicKey);
                rsa.FromXmlStringExtensions(publicKey);
                byte[] bytes = rsa.Encrypt(UTF8Encoding.UTF8.GetBytes(strEncryptInfo), RSAEncryptionPadding.Pkcs1);
                return Convert.ToBase64String(bytes);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// RSA私钥格式转换
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string RSAPrivateKeyJava2DotNet(string privateKey)
        {
            if (string.IsNullOrEmpty(privateKey))
            {
                return string.Empty;
            }

            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公钥格式转换
        /// </summary>
        /// <param name=&  
		

请关注公众号获取更多资料


.NET core RSA帮助类(一) https://www.cppentry.com/bencandy.php?fid=106&id=164985

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇自己动手实现网络服务器(Web Serv.. 下一篇FCL源码中数组类型的学习及排序函..

评论

验 证 码:
表  情:
内  容: