首页 > Windows开发 > 详细

C# -- RSA加密与解密

时间:2019-08-22 09:56:04      阅读:98      评论:0      收藏:0      [点我收藏+]

1.  RSA加密与解密  --  使用公钥加密、私钥解密

    public class RSATool
    {
        public string Encrypt(string strText, string strPublicKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(strPublicKey);

            byte[] byteText = Encoding.UTF8.GetBytes(strText);
            byte[] byteEntry = rsa.Encrypt(byteText, false);

            return Convert.ToBase64String(byteEntry);
        }


        public string Decrypt(string strEntryText,string strPrivateKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(strPrivateKey);

            byte[] byteEntry = Convert.FromBase64String(strEntryText);
            byte[] byteText = rsa.Decrypt(byteEntry, false);

            return Encoding.UTF8.GetString(byteText);
        }

        public Dictionary<string,string> GetKey()
        {
            Dictionary<string, string> dictKey = new Dictionary<string, string>();
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            dictKey.Add("PublicKey", rsa.ToXmlString(false));
            dictKey.Add("PrivateKey", rsa.ToXmlString(true));

            return dictKey;
        }
    }

测试:

技术分享图片
            RSATool myRSA = new RSATool();
            Dictionary<string, string> dictK = new Dictionary<string, string>();
            dictK = myRSA.GetKey();

            string strText = "123456";
            Console.WriteLine("要加密的字符串是:{0}", strText);

            string str1 = myRSA.Encrypt("123456", dictK["PublicKey"]);
            Console.WriteLine("加密后的字符串:{0}", str1);

            string str2 = myRSA.Decrypt(str1, dictK["PrivateKey"]);
            Console.WriteLine("解密后的字符串:{0}", str2);
View Code

技术分享图片

 

 

2.  RSA加密与解密  --  使用同一个密钥容器进行加密与解密

    public class RSAToolX
    {
        public string Encrypt(string strText)
        {
            CspParameters CSApars = new CspParameters();
            CSApars.KeyContainerName = "Test001";

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(CSApars);

            byte[] byteText = Encoding.UTF8.GetBytes(strText);
            byte[] byteEntry = rsa.Encrypt(byteText, false);

            return Convert.ToBase64String(byteEntry);
        }


        public string Decrypt(string strEntryText)
        {
            CspParameters CSApars = new CspParameters();
            CSApars.KeyContainerName = "Test001";

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(CSApars);

            byte[] byteEntry = Convert.FromBase64String(strEntryText);
            byte[] byteText = rsa.Decrypt(byteEntry, false);

            return Encoding.UTF8.GetString(byteText);
        }
    }

测试 :

技术分享图片
            RSAToolX myRSA = new RSAToolX();

            string strText = "123456";
            Console.WriteLine("要加密的字符串是:{0}", strText);

            string str1 = myRSA.Encrypt("123456");
            Console.WriteLine("加密后的字符串:{0}", str1);

            string str2 = myRSA.Decrypt(str1);
            Console.WriteLine("解密后的字符串:{0}", str2);
View Code

技术分享图片

 

C# -- RSA加密与解密

原文:https://www.cnblogs.com/ChengWenHao/p/CSharpRSA.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!