【代码】C#实现对字符串使用RSA进行加密解密

2020-03-13 09:01:52  阅读 164 次 评论 0 条

download.jpg

本篇文章介绍如何使用RSA算法对文本进行加密解密

这个据说是目前非对称加密中比较优秀的加密算法了,如银行什么的都在在RSA1024 RSA2048 RSA4096之类,后面的数字,我想,越大,越复杂,破解也越困难吧。。(欢迎来喷)


直接上代码


加密方法

//RSA加密,随机生成公私钥对并作为出参返回
        static public string RSA_Encrypt(string str_Plain_Text, out string str_Public_Key, out string str_Private_Key)
        {
            str_Public_Key = "";
            str_Private_Key = "";
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] DataToEncrypt = ByteConverter.GetBytes(str_Plain_Text);
            try
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
                str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));

                //OAEP padding is only available on Microsoft Windows XP or later. 
                byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
                str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
                str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
                string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
                return str_Cypher_Text;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }


解密算法

//RSA解密
        static public string RSA_Decrypt(string str_Cypher_Text, string str_Private_Key)
        {
            byte[] DataToDecrypt = Convert.FromBase64String(str_Cypher_Text);
            try
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                //RSA.ImportParameters(RSAKeyInfo);
                byte[] bytes_Public_Key = Convert.FromBase64String(str_Private_Key);
                RSA.ImportCspBlob(bytes_Public_Key);

                //OAEP padding is only available on Microsoft Windows XP or later. 
                byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                string str_Plain_Text = ByteConverter.GetString(bytes_Plain_Text);
                return str_Plain_Text;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.ToString());
                return null;
            }
        }


Main方法:调用示例

public static void Main()
        {
            try
            {
                string str_Plain_Text = "Are You OK?";
                Console.WriteLine("明文:" + str_Plain_Text);
                Console.WriteLine("长度:" + str_Plain_Text.Length.ToString());
                Console.WriteLine();

                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

                string str_Public_Key;
                string str_Private_Key;
                string str_Cypher_Text = RSA_Encrypt(str_Plain_Text, out str_Public_Key, out str_Private_Key);
                Console.WriteLine("密文:" + str_Cypher_Text);
                Console.WriteLine("公钥:" + str_Public_Key);
                Console.WriteLine("私钥:" + str_Private_Key);

                string str_Plain_Text2 = RSA_Decrypt(str_Cypher_Text, str_Private_Key);
                Console.WriteLine("解密:" + str_Plain_Text2);

                Console.WriteLine();
            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("Encryption failed.");
            }
        }


本文地址:https://www.yangguangdream.com/?id=2069
版权声明:本文为原创文章,版权归 编辑君 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?