基于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;
????????}
????}

}?

轉載于:https://www.cnblogs.com/wolfocme110/p/3864818.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375721.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375721.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375721.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

iOS GorupBy

轉自: IOS 數組分組 Grouped NSArray 12345678NSMutableSet *set[NSMutableSet set];[_list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {[set addObject:obj["MeasureType"]];//利用set不重復的特性,得到有多少組,根據數組中的…

android通過adb shell播放音樂

am start -n com.android.music/com.android.music.MediaPlaybackActivity -d /sdcard/timian.mp3拓展閱讀 input keyevent 24 #增加音量 input keyevent 25 #降低音量 input keyevent 85 #暫停/播放 input keyevent 126 #恢復播放 input keyevent 127 #停止播放關閉音樂播放器…

NetBpm 安裝篇(1)

尊重別人勞動成果 轉載注明出處:http://www.cnblogs.com/anbylau2130/p/3875718.html 官方主頁 http://www.netbpm.org/docs/install.html 文件目錄 Netbpm的兩種服務器配置 1,CassiniWebServer CassiniWebServer.exe是輕量級的web服務器,相…

python將文本中的數據處理成圖像(matplotlib)

使用Python的matplotlib模塊可以很方便的將數據處理成圖表,使數據更加形象、直觀。 #!/usr/bin/env pythonimport matplotlib.pyplot as plt import numpy as np from mpl_toolkits.axes_grid.anchored_artists import AnchoredTexty1np.loadtxt(ReadDataCostTime.…

string 中的 length函數 和size函數 返回值問題

string 中的 length函數 和 size函數 的返回值 ( 還有 char [ ] 中 測量字符串的 strlen 函數 ) 應該是 unsigned int 類型的 不可以 和 -1 比較。 應盡量避免 unsigned int 類型 和 int類型 數據 的比較 。當unsigned int 類型 和 int類型 數據 比較 時 ,會 把…

交叉編譯android版htop

編這個東西賊煩人。 話不多說,直接上教程 源代碼版本:htop-2.2.0、ncurses-6.1 編譯之前要確認自己有ndk,從【官網】直接下載,下載下來解壓一下就能用。 先編ncurses 編譯過程 ./configure CCarm-linux-androideabi-gcc-4.9 \-…

今天的一點點收獲

今天怎么說呢,還是有點收獲的,上午寫了一上午的前端,然后就是下午又是一下午的c#,好特么酸爽啊,但是有一件特別蛋疼的事情發生了,我 天天叫的學長竟然不是學長而是學校的而老師,但是他們都不叫他…

jquery動態添加刪除div--事件綁定,對象克隆

我想做一個可以動態添加刪除div的功能。中間遇到一個問題,最后在manong123.com開發文摘 版主的熱心幫助下解答了(答案在最后) 使用到的jquery方法和思想就是:事件的綁定和銷毀(unbind),另外還可以使用clone,通過克隆可以很好的解決這個問…

編程知識大雜燴

以下資料完全是隨手記錄,沒有任何順序或關聯,需要用直接^F找就行了。 1. ps aux指令詳解 http://blog.csdn.net/hanner_cheung/article/details/6081440 2. Linux下配置Apache php http://lelong.iteye.com/blog/904125 3. shell定義變量 http://see.xid…

最長公共前綴

2、最長公共前綴 編寫一個函數來查找字符串數組中的最長公共前綴。 如果不存在公共前綴,返回空字符串 “”。 示例1 輸入: ["flower","flow","flight"] 輸出: "fl"示例2 輸入: ["dog","racecar",…

devexpress中gridcontrol頭部添加垂直線(右邊框)

winform開發,用devexpress中的gridcontrol控件,頭部默認是3D樣式,當客戶希望像內容一樣扁平化顯示且需要添加垂直線(右邊框)時惡夢開始了。。經過一陣摸索發現可以這樣解決: 1.設置GridControl的GridView控件的PaintStyleName屬性…

UITableView知識梳理須知—(一)

1、UITableView掌握 1> 設置UITableView的dataSource、delegate 2> UITableView多組數據和單組數據的展示 3> UITableViewCell的常見屬性 4> UITableView的性能優化(cell的循環利用) 5> 自定義Cell 2、什么是UITableView 在i…

Yarn中的幾種狀態機

1 概述 為了增大并發性,Yarn采用事件驅動的并發模型,將各種處理邏輯抽象成事件和調度器,將事件的處理過程用狀態機表示。什么是狀態機? 如果一個對象,其構成為若干個狀態,以及觸發這些狀態發生相互轉移的事…

反轉字符串里的單詞

4、反轉字符串里的單詞 給定一個字符串,逐個反轉字符串中的單詞 示例1: 輸入: "the sky is blue", 輸出: "blue is sky the".說明: 無空格字符構成一個單詞。 輸入字符串可以在前面或者后面包含多余的空格&#xff0…

正整數

題目鏈接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid84077#problem/A 題目: Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the s…

360 webscan中防注入跨站攻擊的核心

//get攔截規則 $getfilter "\\<.javascript:window\\[.{1}\\\\x|<.*(&#\\d?;?)?>|<.*(data|src)data:text\\/html.*>|\\b(alert\\(|confirm\\(|expression\\(|prompt\\(|benchmark\s*?\\(\d?|sleep\s*?\\([\d\.]?\\)|load_file\s*?\\()|<[…

POJ 2115 C Looooops(擴展歐幾里得)

輾轉相除法&#xff08;歐幾里得算法&#xff09; 時間復雜度&#xff1a;在O(logmax(a, b))以內 int gcd(int a, int b) {if (b 0) return a;return gcd(b, a % b); }擴展歐幾里得算法 時間復雜度和歐幾里得算法相同 int extgcd(int a, int b, int& x, int& y) {int …

分支管理(轉載)

轉自&#xff1a;http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743862006503a1c5bf5a783434581661a3cc2084efa000 分支就是科幻電影里面的平行宇宙&#xff0c;當你正在電腦前努力學習Git的時候&#xff0c;另一個你正在另一個平行…

匹配括號

輸入&#xff1a; 僅包含{,},(,),[,]的字符串輸出&#xff1a; 如果括號匹配輸出&#xff1a;YES 否則輸出&#xff1a;NOSolution&#xff1a; #include<iostream> #include<string> #include<stack> using namespace std;bool check(const string&)…

總線接口與計算機通信

微機中總線一般有內部總線、系統總線和外部總線。 內部總線是微機內部各外圍芯片與處理器之間的總線&#xff0c;用于芯片一級的互連&#xff1b; 系統總線是微機中各插件板與系統板之間的總線&#xff0c;用于插件板一級的互連&#xff1b; 外部總線則是微機和外部設備之間的總…