• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

基于RSA的加密/解密示例C#代码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
在C#程序中,大家可能比较熟悉的方式是md5加密解密方式,对RSA可能并不是很熟悉, 下面就说一下RSA加密和解密的算法:

 

using System;
using System.Security.Cryptography;
using System.Text;
class RSACSPSample
{
    static void Main()
    {
        try
        {
            string str_Plain_Text = "How are you?How are you?How are you?How are you?=-popopolA";
            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.");
        }
    }
    //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;
        }
    }

} 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#语音(二)文字生成WAV文件发布时间:2022-07-13
下一篇:
将C#文档注释生成.chm帮助文档发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap