zoukankan      html  css  js  c++  java
  • 基于RSA的加密/解密示例C#代码

    在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;
            }
        }

    } 

  • 相关阅读:
    JVM知识体系
    RabbitMQ学习11死信队列(拒绝消息)
    JUC知识体系
    RabbitMQ学习10死信队列(队列达到最大长度)
    Dropdownlist+objectdatasource设定“请选择”默认选项
    sql DATEPART函数使用
    win7 'IIS APPPOOL\Classic .NET AppPool' 登录失败
    sqlserver2000还原数据库时报设备激活错误的解决方法
    vss和vs2008组合搭建源代码管理器
    在配置win7 IIS浏览网站时 检测到在集成的托管管道模式下不适用的ASP.NET设置 的解决方法
  • 原文地址:https://www.cnblogs.com/wolfocme110/p/3864818.html
Copyright © 2011-2022 走看看