基於RSA的加密/解密樣本C#代碼

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   re   

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.