1. 程式人生 > >C#實現RSA加密解密原始碼

C#實現RSA加密解密原始碼

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

相關推薦

C#實現RSA加密解密原始碼

{ privatestaticstring publicKey = "<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+ "TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+ "wPycZwtNOx3Cfy44/D5M

C++實現RSA加密解密

RSA演算法的描述 1、選取長度相等的兩個大素數p和q,計算其乘積: n = pq 然後隨機選取加密金鑰e,使e和(p–1)(q–1)互素。 最後用歐幾里德擴充套件演算法計算解密金鑰d,以滿足 ed = 1(

C# 實現AES加密--解密

bsp 密文 rem adding gets manage string sys cipher /// <summary> /// AES 加密 /// </summary> ///

C#-java RSA加密解密

過程 data readline utf 解密 param rsa私鑰 convert graph using Org.BouncyCastle.Math;using Org.BouncyCastle.Crypto.Parameters;using Org.BouncyCa

條理清晰的入門:使用Java實現RSA加密解密

條理清晰的入門:使用Java實現RSA加密解密 什麼是RSA 使用Java 需要匯入的標頭檔案 生成公鑰、私鑰 進行加密解密 金鑰的儲存 密文的儲存、讀取 什麼是RSA 翻一下以前的密碼

JAVA實現RSA加密解密

RSA 工具類。提供加密,解密,生成金鑰對等方法。  RSA加密原理概述   : RSA的安全性依賴於大數的分解,公鑰和私鑰都是兩個大素數(大於100的十進位制位)的函式。 據猜測,從一個金鑰和密文推斷出明文的難度等同於分解兩個大素數的積    金鑰的產生:     1.選

Java 實現 RSA加密解密及數字簽名

RSA公鑰加密演算法是1977年由羅納德·李維斯特(Ron Rivest)、阿迪·薩莫爾(Adi Shamir)和倫納德·阿德曼(Leonard Adleman)一起提出的。當時他們三人都在麻省理工學院工作。RSA就是他們三人姓氏開頭字母拼在一起組成的。 RSA是目

C#實現DES加密解密封裝

{            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));            byte[] keyIV = keyBytes;            byte[] inputByteArray = Convert.

C#實現Base64加密解密

Base64編碼的思想是是採用64個基本的ASCII碼字元對資料進行重新編碼。它將需要編碼的資料拆分成位元組陣列。以3個位元組為一組。按順序排列24 位資料,再把這24位資料分成4組,即每組6位。再在每組的的最高位前補兩個0湊足一個位元組。這樣就把一個3位元組為一組的資料重新

安全不安全002:C#實現RSA算法加密解密

RSA C#通過前面的文章我們學會了如何生成公鑰和私鑰,詳見這篇文章:https://blog.csdn.net/yysyangyangyangshan/article/details/80368397。那麽,我們來看在C#中如何實現RSA加密解密。直接上代碼,如下類是RSA算法實現的加密,加解密,簽名以及簽

C#實現RSA公鑰加密私鑰解密、私鑰加密公鑰解密以及Pcks12、X509證書加解密、簽名驗籤

RSA的私鑰簽名公鑰驗籤可以見http://blog.csdn.net/starfd/article/details/51917916,所以這裡就沒提供對應程式碼,具體程式碼如下: using Org.BouncyCastle.Asn1; using Org.B

RSA加密解密C++實現

實現過程: 1 隨意選擇兩個大的質數p和q,p不等於q,計算N=p*q。 2 根據尤拉函式,求得r = (p-1)(q-1) 3 選擇一個小於 r 的整數 e,求得 e 關於模 r 的模反元素,命名為d。(模反元素存在,當且僅當e與r互質) 將 p 和

C# 與JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle來實現私鑰加密,公鑰解密的方法

cipher process [] var class mar tor als get 因為C#的RSA加密解密只有公鑰加密,私鑰解密,沒有私鑰加密,公鑰解密。在網上查了很久也沒有很好的實現。BouncyCastle的文檔少之又少。很多人可能會說,C#也是可以的,通過Big

C# Java間進行RSA加密解密交互

exc add 交互 長度限制 orm keys 什麽 highlight arr 引用:http://blog.csdn.net/dslinmy/article/details/37362661 這裏,講一下RSA算法加解密在C#和Java之間交互的問題,這兩天糾結了很久

C#自定義RSA加密解密RSA簽名和驗證類實例

狀態 share normal evel thumb weight encrypt security clas 本文實例講述了C#自定義RSA加密解密及RSA簽名和驗證類。分享給大家供大家參考。具體分析如下: 這個C#類自定義RSA加密解密及RSA簽名和驗證,包含了RSA

RSA加密解密及數字簽名Java實現

cto 包括 sign object misc 數據 factory 了解 對稱密鑰 RSA公鑰加密算法是1977年由羅納德·李維斯特(Ron Rivest)、阿迪·薩莫爾(Adi Shamir)和倫納德·阿德曼(Leonard Adleman)一起提出的。當時他們三人都在

RSA加密解密實現(JAVA)

1.關於RSA演算法的原理解析參考:http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html 2.RSA金鑰長度、明文長度和密文長度參考:https://blog.csdn.net/liuhuabai100/article/deta

go語言實現非對稱加密——RSA加密解密實現

版權宣告:本文為作者原創,如需轉載,請註明出處 https://blog.csdn.net/weixin_42940826 非對稱加密簡介 什麼是非對稱加密 非對稱加密,顧名思義,是相對於對稱加密的一種加密方法,對稱加密是指加密與解密使用的是同一把祕鑰,而非對稱加

使用python實現RSA解密演算法(包含讀取檔案操作),檔案內容為16進位制字串,同時實現對學號姓名的加密——(SCU應用密碼學實驗)

#-*- coding:UTF-8 -*- ''' time: 2018-5-30 content:RSA python 3.6 mac os ''' from random import randint import random im

golang實現RSA加密解密

package main import ( "crypto/rsa" "crypto/rand" "crypto/x509" "encoding/pem" "os" "fmt" ) func rsaGenKey(bits int) error { priva