码迷,mamicode.com
首页 > Windows程序 > 详细

C#实现RSA加密和解密详解

时间:2014-11-14 21:04:26      阅读:704      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   

原文:C#实现RSA加密和解密详解

RSA加密解密源码:

bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Linq;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
using System.Security.Cryptography;
bubuko.com,布布扣
bubuko.com,布布扣
namespace MyRSA

namespace MyRSA

bubuko.com,布布扣
{

bubuko.com,布布扣
publicclass MyRSA

bubuko.com,布布扣
{

bubuko.com,布布扣

bubuko.com,布布扣
privatestaticstring publicKey =

bubuko.com,布布扣   
"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+

bubuko.com,布布扣   
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+

bubuko.com,布布扣   
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+

bubuko.com,布布扣   
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+

bubuko.com,布布扣   
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+

bubuko.com,布布扣   
"w9YRXiac=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

bubuko.com,布布扣
privatestaticstring privateKey =

bubuko.com,布布扣   
"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+

bubuko.com,布布扣   
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+

bubuko.com,布布扣   
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+

bubuko.com,布布扣   
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+

bubuko.com,布布扣   
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+

bubuko.com,布布扣   
"w9YRXiac=</Modulus><Exponent>AQAB</Exponent>"+

bubuko.com,布布扣   
"<P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d"+

bubuko.com,布布扣   
"L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd"+

bubuko.com,布布扣   
"VgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOle"+

bubuko.com,布布扣   
"VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG"+

bubuko.com,布布扣   
"lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q>"+

bubuko.com,布布扣   
"<DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FU"+

bubuko.com,布布扣   
"GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te"+

bubuko.com,布布扣   
"zc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArY"+

bubuko.com,布布扣   
"gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA"+

bubuko.com,布布扣   
"StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>"+

bubuko.com,布布扣   
"GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn"+

bubuko.com,布布扣   
"cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4"+

bubuko.com,布布扣   
"aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2D"+

bubuko.com,布布扣   
"Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ"+

bubuko.com,布布扣   
"s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40"+

bubuko.com,布布扣   
"H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry"+

bubuko.com,布布扣   
"oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";

bubuko.com,布布扣

bubuko.com,布布扣
staticpublicstring Decrypt(string base64code)

bubuko.com,布布扣
{

bubuko.com,布布扣   
try

bubuko.com,布布扣   
{

bubuko.com,布布扣

bubuko.com,布布扣       
//Create a UnicodeEncoder to convert between byte array and string.

bubuko.com,布布扣
        UnicodeEncoding ByteConverter =new UnicodeEncoding();

bubuko.com,布布扣

bubuko.com,布布扣       
//Create a new instance of RSACryptoServiceProvider to generate

bubuko.com,布布扣       
//public and private key data.

bubuko.com,布布扣
        RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();

bubuko.com,布布扣        RSA.FromXmlString(privateKey);

bubuko.com,布布扣

bubuko.com,布布扣       
byte[] encryptedData;

bubuko.com,布布扣       
byte[] decryptedData;

bubuko.com,布布扣        encryptedData
= Convert.FromBase64String(base64code);

bubuko.com,布布扣

bubuko.com,布布扣       
//Pass the data to DECRYPT, the private key information

bubuko.com,布布扣       
//(using RSACryptoServiceProvider.ExportParameters(true),

bubuko.com,布布扣       
//and a boolean flag specifying no OAEP padding.

bubuko.com,布布扣
        decryptedData = RSADecrypt(

bubuko.com,布布扣            encryptedData, RSA.ExportParameters(
true), false);

bubuko.com,布布扣

bubuko.com,布布扣       
//Display the decrypted plaintext to the console.

bubuko.com,布布扣
        return ByteConverter.GetString(decryptedData);

bubuko.com,布布扣    }


bubuko.com,布布扣   
catch (Exception exc)

bubuko.com,布布扣   
{

bubuko.com,布布扣       
//Exceptions.LogException(exc);

bubuko.com,布布扣
        Console.WriteLine(exc.Message);

bubuko.com,布布扣       
return"";

bubuko.com,布布扣    }


bubuko.com,布布扣}


bubuko.com,布布扣

bubuko.com,布布扣
staticpublicstring Encrypt(string toEncryptString)

bubuko.com,布布扣
{

bubuko.com,布布扣   
try

bubuko.com,布布扣   
{

bubuko.com,布布扣       
//Create a UnicodeEncoder to convert between byte array and string.

bubuko.com,布布扣
        UnicodeEncoding ByteConverter =new UnicodeEncoding();

bubuko.com,布布扣

bubuko.com,布布扣       
//Create byte arrays to hold original, encrypted, and decrypted data.

bubuko.com,布布扣
        byte[] dataToEncrypt =

bubuko.com,布布扣            ByteConverter.GetBytes(toEncryptString);

bubuko.com,布布扣       
byte[] encryptedData;

bubuko.com,布布扣       
byte[] decryptedData;

bubuko.com,布布扣

bubuko.com,布布扣       
//Create a new instance of RSACryptoServiceProvider to generate

bubuko.com,布布扣       
//public and private key data.

bubuko.com,布布扣
        RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();

bubuko.com,布布扣

bubuko.com,布布扣        RSA.FromXmlString(privateKey);

bubuko.com,布布扣

bubuko.com,布布扣       
//Pass the data to ENCRYPT, the public key information

bubuko.com,布布扣       
//(using RSACryptoServiceProvider.ExportParameters(false),

bubuko.com,布布扣       
//and a boolean flag specifying no OAEP padding.

bubuko.com,布布扣
        encryptedData = RSAEncrypt(

bubuko.com,布布扣            dataToEncrypt, RSA.ExportParameters(
false), false);

bubuko.com,布布扣

bubuko.com,布布扣       
string base64code = Convert.ToBase64String(encryptedData);

bubuko.com,布布扣       
return base64code;

bubuko.com,布布扣    }


bubuko.com,布布扣   
catch (Exception exc)

bubuko.com,布布扣   
{

bubuko.com,布布扣       
//Catch this exception in case the encryption did

bubuko.com,布布扣       
//not succeed.

bubuko.com,布布扣       
//Exceptions.LogException(exc);

bubuko.com,布布扣
        Console.WriteLine(exc.Message);

bubuko.com,布布扣       
return"";

bubuko.com,布布扣    }


bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣}


bubuko.com,布布扣

bubuko.com,布布扣
staticprivatebyte[] RSAEncrypt(

bubuko.com,布布扣   
byte[] DataToEncrypt,

bubuko.com,布布扣    RSAParameters RSAKeyInfo,

bubuko.com,布布扣   
bool DoOAEPPadding)

bubuko.com,布布扣
{

bubuko.com,布布扣   
try

bubuko.com,布布扣   
{

bubuko.com,布布扣       
//Create a new instance of RSACryptoServiceProvider.

bubuko.com,布布扣
        RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();

bubuko.com,布布扣

bubuko.com,布布扣       
//Import the RSA Key information. This only needs

bubuko.com,布布扣       
//toinclude the public key information.

bubuko.com,布布扣
        RSA.ImportParameters(RSAKeyInfo);

bubuko.com,布布扣

bubuko.com,布布扣       
//Encrypt the passed byte array and specify OAEP padding. 

bubuko.com,布布扣       
//OAEP padding is only available on Microsoft Windows XP or

bubuko.com,布布扣       
//later. 

bubuko.com,布布扣
        return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);

bubuko.com,布布扣    }


bubuko.com,布布扣   
//Catch and display a CryptographicException 

bubuko.com,布布扣   
//to the console.

bubuko.com,布布扣
    catch (CryptographicException e)

bubuko.com,布布扣   
{

bubuko.com,布布扣       
//Exceptions.LogException(e);

bubuko.com,布布扣
        Console.WriteLine(e.Message);

bubuko.com,布布扣

bubuko.com,布布扣       
returnnull;

bubuko.com,布布扣    }


bubuko.com,布布扣

bubuko.com,布布扣}


bubuko.com,布布扣

bubuko.com,布布扣
staticprivatebyte[] RSADecrypt(

bubuko.com,布布扣   
byte[] DataToDecrypt,

bubuko.com,布布扣    RSAParameters RSAKeyInfo,

bubuko.com,布布扣   
bool DoOAEPPadding)

bubuko.com,布布扣
{

bubuko.com,布布扣   
try

bubuko.com,布布扣   
{

bubuko.com,布布扣       
//Create a new instance of RSACryptoServiceProvider.

bubuko.com,布布扣
        RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();

bubuko.com,布布扣

bubuko.com,布布扣       
//Import the RSA Key information. This needs

bubuko.com,布布扣       
//to include the private key information.

bubuko.com,布布扣
        RSA.ImportParameters(RSAKeyInfo);

bubuko.com,布布扣

bubuko.com,布布扣       
//Decrypt the passed byte array and specify OAEP padding. 

bubuko.com,布布扣       
//OAEP padding is only available on Microsoft Windows XP or

bubuko.com,布布扣       
//later. 

bubuko.com,布布扣
        return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);

bubuko.com,布布扣    }


bubuko.com,布布扣   
//Catch and display a CryptographicException 

bubuko.com,布布扣   
//to the console.

bubuko.com,布布扣
    catch (CryptographicException e)

bubuko.com,布布扣   
{

bubuko.com,布布扣       
//Exceptions.LogException(e);

bubuko.com,布布扣
        Console.WriteLine(e.Message);

bubuko.com,布布扣

bubuko.com,布布扣       
returnnull;

bubuko.com,布布扣    }


bubuko.com,布布扣}


bubuko.com,布布扣}


bubuko.com,布布扣}

 测试代码:

        static void Main(string[] args)
        {
            
string encodeString = MyRSA.Encrypt("1234567");
            Console.WriteLine(encodeString);


            
string decode = MyRSA.Decrypt(encodeString);
            Console.WriteLine(decode);

            Console.ReadLine();
        }

C#实现RSA加密和解密详解

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/lonelyxmas/p/4098069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!