RSA加密解密

http://blog.163.com/charset@126/blog/static/119249946201031104758814/

RSA中机密过程主要是针对DES对称加密的KEY

 

一般流程用户A用B的公钥对KEY进行加密,B收到信息后用自己的私钥进行解密获得KEY

 

public string RSAEncode(string originString)
  {
    RSAParameters PrvKeyInfo=RSAParamParser.ParseRSAParam(publicFile); //publicFile地址 

   RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

   RSA.ImportParameters(PubKeyInfo);
   byte[] data = System.Text.Encoding.Default.GetBytes(originString);
   return System.Convert.ToBase64String(RSA.Encrypt(data,false));
  }

 

public string RSADecode(byte[] encryptedData)
  {
   RSAParameters PrvKeyInfo=RSAParamParser.ParseRSAParam(privateFile); //privateFile地址   

   RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

   RSA.ImportParameters(PrvKeyInfo);
   return  System.Convert.ToBase64String(RSA.Decrypt(encryptedData,false));
  }

 

如果pubkey为string格式

    byte[] keyData = System.Convert.FromBase64String(pubkey);
    RSAParameters param = RSAParamParser.ParseRSAParam(keyData);

http://www.cnblogs.com/prince3245/archive/2010/04/01/1702405.html

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

 * 基于RSA的加密/解密示例C#代码
 * (采用字符串作为参数)RSA_Demo2
 * 
 * 夏春涛 Email:xChuntao@163.com 
 * Blog:
http://bluesky521.cnblogs.com
 * 运行环境:.net2.0 framework
 * 
 * 备注:
 * 不对称算法通常用于加密少量数据,如加密对称密钥和 IV。通常,
 * 执行不对称加密的个人使用由另一方生成的公钥。.NET Framework 
 * 为此目的而提供了 RSACryptoServiceProvider 类。
 
*/

点此在新窗口浏览图片
点此在新窗口浏览图片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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值