日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2024/4/14 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#实现RSA加密和解密详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C#實現RSA加密和解密詳解 原文:C#實現RSA加密和解密詳解

RSA加密解密源碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace MyRSA
{
publicclass MyRSA
...{

privatestaticstring publicKey =
"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+
"w9YRXiac=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
privatestaticstring privateKey =
"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+
"w9YRXiac=</Modulus><Exponent>AQAB</Exponent>"+
"<P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d"+
"L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd"+
"VgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOle"+
"VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG"+
"lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q>"+
"<DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FU"+
"GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te"+
"zc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArY"+
"gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA"+
"StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>"+
"GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn"+
"cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4"+
"aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2D"+
"Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ"+
"s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40"+
"H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry"+
"oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";

staticpublicstring Decrypt(string base64code)
...{
try
...{

//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter =new UnicodeEncoding();

//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
RSA.FromXmlString(privateKey);

byte[] encryptedData;
byte[] decryptedData;
encryptedData
= Convert.FromBase64String(base64code);

//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(
encryptedData, RSA.ExportParameters(
true), false);

//Display the decrypted plaintext to the console.
return ByteConverter.GetString(decryptedData);
}

catch (Exception exc)
...{
//Exceptions.LogException(exc);
Console.WriteLine(exc.Message);
return"";
}

}


staticpublicstring Encrypt(string toEncryptString)
...{
try
...{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter =new UnicodeEncoding();

//Create byte arrays to hold original, encrypted, and decrypted data.
byte[] dataToEncrypt =
ByteConverter.GetBytes(toEncryptString);
byte[] encryptedData;
byte[] decryptedData;

//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();

RSA.FromXmlString(privateKey);

//Pass the data to ENCRYPT, the public key information
//(using RSACryptoServiceProvider.ExportParameters(false),
//and a boolean flag specifying no OAEP padding.
encryptedData = RSAEncrypt(
dataToEncrypt, RSA.ExportParameters(
false), false);

string base64code = Convert.ToBase64String(encryptedData);
return base64code;
}

catch (Exception exc)
...{
//Catch this exception in case the encryption did
//not succeed.
//Exceptions.LogException(exc);
Console.WriteLine(exc.Message);
return"";
}




}


staticprivatebyte[] RSAEncrypt(
byte[] DataToEncrypt,
RSAParameters RSAKeyInfo,
bool DoOAEPPadding)
...{
try
...{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();

//Import the RSA Key information. This only needs
//toinclude the public key information.
RSA.ImportParameters(RSAKeyInfo);

//Encrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}

//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
...{
//Exceptions.LogException(e);
Console.WriteLine(e.Message);

returnnull;
}


}


staticprivatebyte[] RSADecrypt(
byte[] DataToDecrypt,
RSAParameters RSAKeyInfo,
bool DoOAEPPadding)
...{
try
...{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();

//Import the RSA Key information. This needs
//to include the private key information.
RSA.ImportParameters(RSAKeyInfo);

//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}

//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
...{
//Exceptions.LogException(e);
Console.WriteLine(e.Message);

returnnull;
}

}

}

}

namespace MyRSA

...{

publicclass MyRSA

...{



privatestaticstring publicKey =

???
"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+

???
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+

???
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+

???
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+

???
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+

???
"w9YRXiac=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

privatestaticstring privateKey =

???
"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+

???
"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+

???
"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+

???
"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+

???
"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+

???
"w9YRXiac=</Modulus><Exponent>AQAB</Exponent>"+

???
"<P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d"+

???
"L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd"+

???
"VgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOle"+

???
"VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG"+

???
"lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q>"+

???
"<DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FU"+

???
"GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te"+

???
"zc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArY"+

???
"gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA"+

???
"StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>"+

???
"GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn"+

???
"cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4"+

???
"aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2D"+

???
"Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ"+

???
"s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40"+

???
"H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry"+

???
"oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";



staticpublicstring Decrypt(string base64code)

...{

???
try

???
...{



???????
//Create a UnicodeEncoder to convert between byte array and string.

??????? UnicodeEncoding ByteConverter =new UnicodeEncoding();



???????
//Create a new instance of RSACryptoServiceProvider to generate

???????
//public and private key data.

??????? RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();

??????? RSA.FromXmlString(privateKey);



???????
byte[] encryptedData;

???????
byte[] decryptedData;

??????? encryptedData
= Convert.FromBase64String(base64code);



???????
//Pass the data to DECRYPT, the private key information

???????
//(using RSACryptoServiceProvider.ExportParameters(true),

???????
//and a boolean flag specifying no OAEP padding.

??????? decryptedData = RSADecrypt(

??????????? encryptedData, RSA.ExportParameters(
true), false);



???????
//Display the decrypted plaintext to the console.

??????? return ByteConverter.GetString(decryptedData);

??? }


???
catch (Exception exc)

???
...{

???????
//Exceptions.LogException(exc);

??????? Console.WriteLine(exc.Message);

???????
return"";

??? }


}




staticpublicstring Encrypt(string toEncryptString)

...{

???
try

???
...{

???????
//Create a UnicodeEncoder to convert between byte array and string.

??????? UnicodeEncoding ByteConverter =new UnicodeEncoding();



???????
//Create byte arrays to hold original, encrypted, and decrypted data.

??????? byte[] dataToEncrypt =

??????????? ByteConverter.GetBytes(toEncryptString);

???????
byte[] encryptedData;

???????
byte[] decryptedData;



???????
//Create a new instance of RSACryptoServiceProvider to generate

???????
//public and private key data.

??????? RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();



??????? RSA.FromXmlString(privateKey);



???????
//Pass the data to ENCRYPT, the public key information

???????
//(using RSACryptoServiceProvider.ExportParameters(false),

???????
//and a boolean flag specifying no OAEP padding.

??????? encryptedData = RSAEncrypt(

??????????? dataToEncrypt, RSA.ExportParameters(
false), false);



???????
string base64code = Convert.ToBase64String(encryptedData);

???????
return base64code;

??? }


???
catch (Exception exc)

???
...{

???????
//Catch this exception in case the encryption did

???????
//not succeed.

???????
//Exceptions.LogException(exc);

??????? Console.WriteLine(exc.Message);

???????
return"";

??? }








}




staticprivatebyte[] RSAEncrypt(

???
byte[] DataToEncrypt,

??? RSAParameters RSAKeyInfo,

???
bool DoOAEPPadding)

...{

???
try

???
...{

???????
//Create a new instance of RSACryptoServiceProvider.

??????? RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();



???????
//Import the RSA Key information. This only needs

???????
//toinclude the public key information.

??????? RSA.ImportParameters(RSAKeyInfo);



???????
//Encrypt the passed byte array and specify OAEP padding.?

???????
//OAEP padding is only available on Microsoft Windows XP or

???????
//later.?

??????? return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);

??? }


???
//Catch and display a CryptographicException?

???
//to the console.

??? catch (CryptographicException e)

???
...{

???????
//Exceptions.LogException(e);

??????? Console.WriteLine(e.Message);



???????
returnnull;

??? }




}




staticprivatebyte[] RSADecrypt(

???
byte[] DataToDecrypt,

??? RSAParameters RSAKeyInfo,

???
bool DoOAEPPadding)

...{

???
try

???
...{

???????
//Create a new instance of RSACryptoServiceProvider.

??????? RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();



???????
//Import the RSA Key information. This needs

???????
//to include the private key information.

??????? RSA.ImportParameters(RSAKeyInfo);



???????
//Decrypt the passed byte array and specify OAEP padding.?

???????
//OAEP padding is only available on Microsoft Windows XP or

???????
//later.?

??????? return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);

??? }


???
//Catch and display a CryptographicException?

???
//to the console.

??? catch (CryptographicException e)

???
...{

???????
//Exceptions.LogException(e);

??????? Console.WriteLine(e.Message);



???????
returnnull;

??? }


}


}


}

?測試代碼:

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


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

????????????Console.ReadLine();
????????}
posted on 2014-11-14 20:00 NET未來之路 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/lonelyxmas/p/4098069.html

總結

以上是生活随笔為你收集整理的C#实现RSA加密和解密详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产成人精品在线 | 久久久久久一区二区 | 玖玖国产精品视频 | 欧美一区免费看 | 久久精品无码专区 | 国产乱淫av一区二区三区 | 永久免费不卡在线观看黄网站 | 男男免费视频 | 911亚洲精品 | 国产精品日韩在线观看 | 久草网视频在线观看 | av中文字幕在线免费观看 | 国产特级黄色片 | 韩国美女黄色片 | 日本丰满少妇一区二区三区 | 国产欧美一区二区三区视频 | 久久久久免费精品视频 | 亚洲系列在线 | 理论片大全免费理伦片 | 黄页网站在线看 | 精品国产午夜福利 | 草久在线 | 五月天婷婷激情网 | 无遮挡边吃摸边吃奶边做 | 5566色| 黄色片在哪里看 | 国产精品电影在线观看 | 美女屁股无遮挡 | 四季av中文字幕 | 日韩欧美视频 | 中文字幕欧美一区 | 日本一区二区三区中文字幕 | 中文字幕免费高清网站 | 在线视频亚洲 | 欧美区二区三区 | 亚洲一区国产 | 精品无码一区二区三区免费 | 日韩精品免费播放 | 亚洲欧洲日韩综合 | 天堂网免费视频 | japan高清日本乱xxxxx | 蜜桃av噜噜 | 麻豆视频一区二区三区 | 日韩精品中文字幕在线观看 | 亚洲jizzjizz| 国产欧美视频在线 | av永久在线 | 一亲二脱三插 | 少妇一级淫片 | 天天色天天射综合网 | 五月天在线观看 | 深夜小视频在线观看 | 姐姐你真棒插曲快来救救我电影 | 婷婷导航| 黄色三级大片 | 就去色综合 | 不卡视频在线观看 | 自拍1区| 男男大尺度 | 欧美三级免费看 | 欧美视频免费 | 久久av片| 久久99热这里只有精品 | 天堂中文字幕在线 | 人人干人人搞 | 国产精品久久婷婷六月丁香 | 欧美成人国产精品高潮 | 成人精品二区 | 国产系列精品av | 亚洲伦理视频 | 国产精品99久 | 好屌妞视频这里只有精品 | 欧美brazzers| 97免费观看视频 | 黑人添美女bbb添高潮了 | 欧美成人三级在线 | 亚洲国产精品成人综合色在线婷婷 | av黄色影院 | 美日韩精品一区二区 | 亚洲成人黄色 | 亚洲第一天堂在线观看 | 韩国三级中文字幕hd浴缸戏 | 色乱码一区二区三在线看 | 中文字幕婷婷 | 精品在线小视频 | 黄色av成人 | 欧美福利在线观看 | av不卡中文字幕 | 欧美久久综合 | 国产三区av| 日本a在线 | 国产传媒视频在线 | 国产黄色片子 | 毛片无遮挡高清免费观看 | 国产又粗又猛又爽又黄 | 日皮视频在线观看 | 97影院| 日日日日干 | 久草综合在线视频 |