亚洲欧美日韩国产成人|亚洲中文字幕无码中字|HEYZO在线无码综合|久久久不卡精品国产一区二|久久精品亚洲无中文东京热|中文字幕乱码一区三区免费|亚洲国产欧美在线观看片不卡|国产精品久久一区二区三区97

一看就會(huì)(c# md5 加解密)c# 加密解密,C#常用的加密算法:MD5、Base64、SHA1、DES、AES、RSA,明朝潁國(guó)公,

保健品 nanfang 2023-08-04 02:29 160 0

1.c#md5加密怎么用

目錄簡(jiǎn)介需要引用的類一、MD5加密二、Base64加碼解碼三、SHA加密解密四、HmacSHA256 Base64加密五、DES加密解密六、AES加密解密七、RSA加密解密:采用公鑰,私鑰的模式簡(jiǎn)介本文主要講解一下C#常用的那些加密算法,包括MD5、Base64、SHA1、SHA256、HmacSHA256、DES、AES、RSA加密等,有筒音作5指法需要的朋友可以參考下

2.md5加密c語言實(shí)現(xiàn)

需要引用的類using Org.BouncyCastle.Crypto;using Org.BouncyCastle.Crypto.Parameters;using Org.BouncyCastle.Security;

3.c++ md5加密

using System;using System.Collections.Generic;using System.Configuration;using System.IO;using System.Linq;

4.c++ md5加密算法

using System.Security.Cryptography;usi筒音作5指法ng System.Text;using System.Threading.Tasks;還需要安裝 BouncyCastle在菜單欄找到 工具 —> NuGet 包管理器 —> 管理解決方案的NuGet程序包 —> 瀏覽 —> 搜索 “BouncyCastle” —> 安裝

5..net md5加密解密

我自己定義的公共字段;有需要的朋友可以自行定義privatestatic CspParameters Param; ////// SK(Secret Access Key):與訪問密鑰ID結(jié)合使用的密鑰,對(duì)請(qǐng)求進(jìn)行加密簽名,可標(biāo)識(shí)發(fā)送方,并防止請(qǐng)求被修改

6.c#加密代碼

///pu筒音作5指法blicstaticstring _appSecret = ConfigurationManager.AppSettings["appSecret"].ToString();

7.c# 文件md5值

////// AK(Access Key ID):訪問密鑰ID與私有訪問密鑰關(guān)聯(lián)的唯一標(biāo)識(shí)符;訪問密鑰ID和私有訪問密鑰一起使用,對(duì)請(qǐng)求進(jìn)行加密簽名///public。

8.c#數(shù)據(jù)加密解密

staticstring _appKey = ConfigurationManager.AppSettings["appKey"].ToString(); //////

9.c實(shí)現(xiàn)md5加密

DES密鑰筒音作5指法///privatestaticbyte[] _KEY = newbyte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08

10.md5加密算法c語言

}; ////// DES向量///privatestaticbyte[] _IV = newbyte[] { 0x08, 0x07, 0x06

, 0x05, 0x04, 0x03, 0x02, 0x01 }; ////// RSA加密/解密的默認(rèn)公鑰///privatestaticstring

_publicKey = @"5m9m14XH3oqLJ8bNGw筒音作5指法9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=AQAB"

; ////// RSA解密/加密的默認(rèn)私鑰///privatestaticstring _privateKey = @"5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SV筒音作5指法ju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=AQAB

/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==

6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==ga1rHIJro8e/yhxjrKY筒音作5指法o/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZ筒音作5指法aa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30="

; ////// 8位加密密鑰///privatestaticstring keys = "olikjhgb"; ///

/// 16位的加密秘鑰///privatestaticstring Key = "1123uyrlouhd@_Lq"; ////// 16位以上的默認(rèn)向量

///privatestatics筒音作5指法tring vector = "*abcdefghijklmnopqrst@"; 一、MD5加密MD5加密是最常見的加密方式,因?yàn)镸D5是不可逆的,所以很多系統(tǒng)的密碼都是用MD5加密保存的。

雖說MD5加密不可逆,但是我們可以對(duì)明文再次加密,進(jìn)行兩次加密的密文進(jìn)行對(duì)比#region MD5加密解密////// 16位MD5加密//////

需要加密的明文字符串///publicstaticstringMD5Encrypt16(string laws) {

var md5 = new MD5CryptoServiceProvider(); string 筒音作5指法cipherText = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(laws)),

4, 8); cipherText = cipherText.Replace("-", ""); return cipherText; }

////// 32位MD5加密//////需要加密的明文字符串///32位MD5加密密文字符串

publicstaticstringMD5Encrypt32(string laws) { string plainTe筒音作5指法xt = laws;

string rule = ""; MD5 md5 = MD5.Create(); //實(shí)例化一個(gè)md5對(duì)像// 加密后是一個(gè)字節(jié)類型的數(shù)組,這里要注意編碼UTF8/Unicode等的選擇 

byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(plainText)); // 通過使用循環(huán),將字節(jié)類型的數(shù)組轉(zhuǎn)換為字符串,此字符串是常規(guī)字符格式化所得

for (int i = 0; i < s.Length; i++) { // 將得到的字符串使用十六進(jìn)制類型格式格式后的字筒音作5指法符是小寫的字母,如果使用大寫(X)則格式后的字符是大寫字符 。

rule = rule + s[i].ToString("x2"); } return rule; }

////// 64位MD5加密//////需要加密的明文字符串///64位MD5加密密文字符串

publicstaticstringMD5Encrypt64(string laws) { string rule = laws; MD5 md5 = MD5.Create();

//實(shí)例化一個(gè)md5對(duì)像// 加密后是一個(gè)字節(jié)類型的數(shù)組,這里要注意筒音作5指法編碼UTF8/Unicode等的選擇 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(rule));

return Convert.ToBase64String(s); } ////// 對(duì)字符串進(jìn)MD5加密//////

源類型///加密后字符串publicstaticstringMd5Encrypt(string sourceStr) { MD5 md5 =

new MD5CryptoServiceProvider(); //將要加密的字符串轉(zhuǎn)換成字節(jié)數(shù)組byte[筒音作5指法] palindata = Encoding.Default.GetBytes(sourceStr);

//通過字節(jié)數(shù)組進(jìn)行加密byte[] encryptdata = md5.ComputeHash(palindata); //將加密后的字節(jié)數(shù)組轉(zhuǎn)換成字符串string returnData = Convert.ToBase64String(encryptdata);

return returnData; } ////// Md5密鑰加密//////

要加密的string字符串///publicstaticstringMd5Encrypt_Key(筒音作5指法string pToEncrypt) { DESCryptoServiceProvider des =

new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = Encoding.ASCII.GetBytes(keys); des.IV = Encoding.ASCII.GetBytes(keys); MemoryStream ms =

new Memo筒音作5指法ryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray,

0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder();

foreach (byte b in ms.ToArray()) { ret.A筒音作5指法ppendFormat("{0:X2}", b); }

var s = ret.ToString(); return s; } ////// Md5解密//////

解密string///publicstaticstringMd5Decrypt(string pToDecrypt

) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte

[] inputByteArray = newbyte[pToDec筒音作5指法rypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length /

2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (

byte)i; } des.Key = Encoding.ASCII.GetBytes(_appSecret); des.IV = Encoding.ASCII.GetBytes(_ap筒音作5指法pSecret); MemoryStream ms =

new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray,

0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); 筒音作5指法 }

////// MD5流加密//////輸入流///public

staticstringGenerateMD5(Stream inputStream) { using (MD5 mi = MD5.Create()) {

//開始加密byte[] newBuffer = mi.ComputeHash(inputStream); StringBuilder sb = new StringBuilder();

for (int i = 0; i < newBuffer.Length; i++) { s筒音作5指法b.Append(newBuffer[i].ToString(

"x2")); } return sb.ToString(); } } #endregion二、Base64加碼解碼

嚴(yán)格來說,Base64是一種編碼,而不是加密,通常Base64編碼后字符串會(huì)用于傳輸數(shù)據(jù)不過也正是因?yàn)锽ase64編碼后字符串具有不可讀性;也有人當(dāng)做加密算法來使用#region Base64加碼解碼///

/// Base64編碼,采用utf8編碼//////待編碼的明文///Base64編碼后的字符串

publicstaticstringB筒音作5指法ase64Encrypt(string strPath) { string returnData; Encoding encode = Encoding.UTF8;

byte[] bytedata = encode.GetBytes(strPath); try { returnData = Convert.ToBase64String(bytedata,

0, bytedata.Length); } catch { returnData =筒音作5指法 strPath; }

return returnData; } ////// Base64解碼,采用utf8編碼方式解碼//////

待解碼的密文///Base64解碼的明文字符串publicstaticstringBase64DesEncrypt(string strPath) {

string returnData; byte[] bpath = Convert.FromBase64String(strPath); try { returnData = Encodi筒音作5指法ng.UTF8.GetString(bpath); }

catch { returnData = strPath; } return returnData; }

#endregion三、SHA加密解密SHA1加密算法與MD5加密類似,都是不可逆的,只是算法不同////// SHA1 加密 //////。

需要加密字符串///指定加密編碼///大小寫格式(大寫:X2;小寫:x2)默認(rèn)小寫

publicstaticstringSHA1Encrypt(string content, Encoding encode筒音作5指法, string upperOrLower = "x2") {

try { var buffer = encode.GetBytes(content);//用指定編碼轉(zhuǎn)為bytes數(shù)組var data = SHA1.Create().ComputeHash(buffer);

var sb = new StringBuilder(); foreach (var t in data) { sb.Append(t.ToString(upperOrLower)); }

return sb.ToSt筒音作5指法ring(); } catch (Exception ex) { return

"SHA1加密出錯(cuò):" + ex.Message; } } ////// SHA256加密//////

要加密的string字符串///SHA256加密之后的密文publicstaticstringSHA256Encrypt(string strIN)

{ byte[] tmpByte; SHA256 sha256 = new SHA256Managed(); 筒音作5指法tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN)); StringBuilder rst =

new StringBuilder(); for (int i = 0; i < tmpByte.Length; i++) { rst.Append(tmpByte[i].ToString(

"x2")); } sha256.Clear(); return rst.ToString(); }

////// 獲取要加密的stri筒音作5指法ng字符串字節(jié)數(shù)組//////待加密字符串///加密數(shù)組

privatestaticbyte[] GetKeyByteArray(string strKey) { UTF8Encoding Asc =

new UTF8Encoding(); int tmpStrLen = strKey.Length; byte[] tmpByte = newbyte[tmpStrLen -

1]; tmpByte = Asc.GetBytes(strKey); return tmpByte; } 筒音作5指法 四、HmacSHA256 Base64加密

////// HmacSHA256 Base64算法,返回的結(jié)果始終是32位//////待加密的明文字符串

///HmacSHA256算法加密之后的密文publicstaticstringHmacSHA256(string message) {

byte[] keyByte = Encoding.GetEncoding("utf-8").GetBytes(_appSecret); byte[] messageBytes = Encoding.GetEncoding(

"utf-8").GetBytes(message)筒音作5指法; using (var hmacsha256 = new HMACSHA256(keyByte)) {

byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return Convert.ToBase64String(hashmessage); } }

五、DES加密解密DES(Data Encryption Standard)是目前最為流行的加密算法之一DES是對(duì)稱的,也就是說它使用同一個(gè)密鑰來加密和解密數(shù)據(jù)DES還是一種分組加密算法,該算法每次處理固筒音作5指法定長(zhǎng)度的數(shù)據(jù)段,稱之為分組。

DES加密算法對(duì)密鑰有要求,必須是8個(gè)字符,如12345678這樣的#region DES加密解密////// DES加密操作//////。

需要加密的明文字符串///返回DES加密的密文字符串publicstaticstringDesEncrypt(string normalTxt) { DESCryptoServiceProvider cryptoProvider =

new DESCryptoServiceProvider(); int i = cryptoProvider.KeySize; Memo筒音作5指法ryStream ms =

new MemoryStream(); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(_KEY, _IV), CryptoStreamMode.Write); StreamWriter sw =

new StreamWriter(cst); sw.Write(normalTxt); sw.Flush(); cst.FlushFinalBlock(); sw.Flush();

stri筒音作5指法ng strRet = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); return strRet; }

////// DES解密操作//////需要解密的密文字符串///返回DES解密之后的明文字符串

publicstaticstringDesDecrypt(string securityTxt)//解密 { byte[] byEnc;

try { securityTxt.Replace("_%_", "/"); se筒音作5指法curityTxt.Replace(

"-%-", "#"); byEnc = Convert.FromBase64String(securityTxt); }

catch { returnnull; } DESCryptoServiceProvider cryptoProvider =

new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(byEnc); CryptoStream cst =

n筒音作5指法ew CryptoStream(ms, cryptoProvider.CreateDecryptor(_KEY, _IV), CryptoStreamMode.Read); StreamReader sr =

new StreamReader(cst); return sr.ReadToEnd(); } #endregion六、AES加密解密高級(jí)加密標(biāo)準(zhǔn)(AES,Advanced Encryption Standard)為最常見的對(duì)稱加密算法(微信小程序加密傳輸就是用這個(gè)加密算法的)

#region AES加密解密////// AES筒音作5指法 base64 加密算法;Key 為16位//////需要加密的字符串

///publicstaticstringRST_AesEncrypt_Base64(string Data) { if (

string.IsNullOrEmpty(Data)) { returnnull; } if (string

.IsNullOrEmpty(Key)) { returnnull; } string Vector = Key.Substring(

0,筒音作5指法 16); Byte[] plainBytes = Encoding.UTF8.GetBytes(Data); Byte[] bKey = new Byte[

32]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); Byte[] bVector =

new Byte[16]; Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length筒音作5指法)), bVector, bVector.Length); Byte[] Cryptograph =

null; // 加密后的密文 Rijndael Aes = Rijndael.Create(); //add Aes.Mode = CipherMode.CBC;

//兼任其他語言的des Aes.BlockSize = 128; Aes.Padding = PaddingMode.PKCS7;

//add endtry { // 開辟一塊內(nèi)存流 using (Memor筒音作5指法yStream Memory = new MemoryStream()) {

// 把內(nèi)存流對(duì)象包裝成加密流對(duì)象 using (CryptoStream Encryptor = new CryptoStream(Memory, Aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write)) {

// 明文數(shù)據(jù)寫入加密流 Encryptor.Write(plainBytes, 0, plainBytes.Length); Encryptor.Flu筒音作5指法shFinalBlock(); Cryptograph = Memory.ToArray(); } } }

catch { Cryptograph = null; } return Convert.ToBase64String(Cryptograph); }

////// AES base64 解密算法;Key為16位//////需要解密的字符串///

Key為16位 密鑰///publicstaticstringRST_AesDecrypt_Base64筒音作5指法(string Data) {

try { if (string.IsNullOrEmpty(Data)) {

returnnull; } if (string.IsNullOrEmpty(Key)) {

returnnull; } string Vector = Key.Substring(0, 16); Byte[] encryptedBytes = Convert.FromBase64String(Data); Byte筒音作5指法[] bKey =

new Byte[32]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); Byte[] bVector =

new Byte[16]; Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] original =

null; // 解密后的明文 筒音作5指法 Rijndael Aes = Rijndael.Create(); //add Aes.Mode = CipherMode.CBC;

//兼任其他語言的des Aes.BlockSize = 128; Aes.Padding = PaddingMode.PKCS7;

//add endtry { // 開辟一塊內(nèi)存流,存儲(chǔ)密文 using (MemoryStream Memory = new MemoryStream(encryptedBytes)) {

//把內(nèi)存流對(duì)象包裝成加密流對(duì)筒音作5指法象 using (CryptoStream Decryptor = new CryptoStream(Memory, Aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read)) {

// 明文存儲(chǔ)區(qū) using (MemoryStream originalMemory = new MemoryStream()) { Byte[] Buffer =

new Byte[1024]; Int32 readBytes = 0;

while ((readB筒音作5指法ytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0) { originalMemory.Write(Buffer,

0, readBytes); } original = originalMemory.ToArray(); } } } }

catch { original = null; } return

Encoding.UTF8.GetString(or筒音作5指法iginal); } catch { returnnull; } }

////// 密鑰16位或者32位的AES base64加密//////需要進(jìn)行加密的明文字符串///

16位或者32位的密鑰///16位以上的向量;默認(rèn)為:"*Gc_Yy_Cq_@_Ztl_99*"http:///AES加密之后的密文

publicstaticstringAesEncrypt(stringvalue, string key, string iv = "") {

if (string.IsNullOrEmpty(value)) returnstring.Empty筒音作5指法; if (string.IsNullOrEmpty(key)) { key = Key; }

if (key.Length 32) thrownew Exception(

"指定的密鑰長(zhǎng)度不能多于32位"); if (key.Length != 16 && key.Length != 24 && key.Length != 32) thrownew。

Exception("指定的密鑰長(zhǎng)度不明確"); if (string.IsNullOrEmpty(iv)) { iv =筒音作5指法 vector; } 。

if (!string.IsNullOrEmpty(iv)) { if (iv.Length < 16) thrownew Exception("指定的向量長(zhǎng)度不能少于16位。

"); } var _keyByte = Encoding.UTF8.GetBytes(key); var _valueByte = Encoding.UTF8.GetBytes(

value); using (var aes = new RijndaelManaged(筒音作5指法)) { aes.IV = !string

.IsNullOrEmpty(iv) ? Encoding.UTF8.GetBytes(iv) : Encoding.UTF8.GetBytes(key.Substring(0, 16)); aes.Key = _keyByte; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7;

var cryptoTransform = aes.CreateEncryptor(); var resultA筒音作5指法rray = cryptoTransform.TransformFinalBlock(_valueByte,

0, _valueByte.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } }

////// 密鑰16位或者32位的AES base64解密//////需要解密的密文///

16位或者32位的密鑰需和加密時(shí)的密鑰保持一致///16位以上的向量需和加密時(shí)的向量保持一致;默認(rèn)為:"*Gc_Yy_Cq_@_Ztl_99*"

///AES解密之后的筒音作5指法明文publicstaticstringAesDecrypt(stringvalue, string key, string iv = "")

{ if (string.IsNullOrEmpty(value)) returnstring.Empty; if (string.IsNullOrEmpty(key)) { key = Key; }

if (key.Length 32) thrownew Exception(

"指定的密鑰長(zhǎng)度不能多于32位"); if (key.Length != 16 && 筒音作5指法key.Length != 24 && key.Length != 32) thrownew。

Exception("指定的密鑰長(zhǎng)度不明確"); if (string.IsNullOrEmpty(iv)) { iv = vector; } 。

if (!string.IsNullOrEmpty(iv)) { if (iv.Length < 16) thrownew Exception("指定的向量長(zhǎng)度不能少于16位。

"); } var _keyByte 筒音作5指法= Encoding.UTF8.GetBytes(key); var _valueByte = Convert.FromBase64String(

value); using (var aes = new RijndaelManaged()) { aes.IV = !string

.IsNullOrEmpty(iv) ? Encoding.UTF8.GetBytes(iv) : Encoding.UTF8.GetBytes(key.Substring(0, 16)); aes.Key = _keyByte; 筒音作5指法 aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7;

var cryptoTransform = aes.CreateDecryptor(); var resultArray = cryptoTransform.TransformFinalBlock(_valueByte,

0, _valueByte.Length); return Encoding.UTF8.GetString(resultArray); } }

#endregion七、RSA加密筒音作5指法解密:采用公鑰,私鑰的模式RSA加密采用公鑰加密,私鑰解密的模式或者私鑰加密,公鑰解密的模式Https的數(shù)字證書也是使用這種模式加密的但是RSA加密有個(gè)特點(diǎn),就是他對(duì)被加密的字符串有長(zhǎng)度限制;待加密的字節(jié)數(shù)不能超過密鑰的長(zhǎng)度值除以8再減去11(即:RSACryptoServiceProvider.KeySize / 8 - 11,而加密后得到密文的字節(jié)數(shù),正好是密鑰的長(zhǎng)度值除以 8(即:RSACryptoServiceProvider.KeySize / 8)。

注:該長(zhǎng)度指的是byte[]數(shù)組的長(zhǎng)度,而不是字符串的長(zhǎng)度,簡(jiǎn)單來說,就是被加密字符串不能太長(zhǎng)#region RSA加密解密:采用公鑰筒音作5指法,私鑰的模式#region 私鑰加密,公鑰解密///

/// RSA私鑰加密//////Java格式的RSA私鑰 base64格式///

待加密的數(shù)據(jù);調(diào)用方法Encoding.GetEncoding("UTF-8").GetBytes(contentData)///加密算法

///RSA私鑰加密之后的密文publicstaticstringEncryptWithPrivateKey(string privateKey,

byte[] contentData, string algorithm = "RSA/ECB/PKCS1Padding") { return Con筒音作5指法vert.ToBase64String(EncryptWithPrivateKey(Convert.FromBase64String(privateKey), contentData, algorithm)); }

privatestaticbyte[] Transform(AsymmetricKeyParameter key, byte[] contentData, string algorithm, bool forEncryption

) { var c = CipherUtilities.GetCipher(algorithm); 筒音作5指法 c.Init(forEncryption,

new ParametersWithRandom(key)); return c.DoFinal(contentData); } ///

/// RSA私鑰加密//////Java格式的RSA私鑰///

待加密的數(shù)據(jù);調(diào)用方法Encoding.GetEncoding("UTF-8").GetBytes(contentData)///加密算法

///RSA私鑰加密之后的密文publicstaticbyte[] EncryptWithPrivateKey(byte[] privateKey,

byte[] c筒音作5指法ontentData, string algorithm = "RSA/ECB/PKCS1Padding") { RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(privateKey);

return Transform(privateKeyParam, contentData, algorithm, true); } ////// RSA公鑰解密

//////Java格式的RSA筒音作5指法公鑰 base64格式///待解密數(shù)據(jù) base64格式

///解密出來的數(shù)據(jù)編碼格式,默認(rèn)UTF-8///加密算法///

RSA私鑰解密之后的明文publicstaticstringDecryptWithPublicKey(string publicKey, string content,

string encoding = "UTF-8", string algorithm = "RSA/ECB/PKCS1Padding") { return Encoding.GetEncoding(encoding).GetString(DecryptWithPublicKey筒音作5指法(Convert.FromBase64String(publicKey), Convert.FromBase64String(content), algorithm)); }

////// RSA公鑰解密//////Java格式的RSA公鑰///

待解密數(shù)據(jù)///加密算法///RSA私鑰解密之后的明文publicstaticbyte

[] DecryptWithPublicKey(byte[] publicKey, byte[] contentData, string algorithm = "RSA/ECB/PKCS1Padding"

) { RsaK筒音作5指法eyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(publicKey);

return Transform(publicKeyParam, contentData, algorithm, false); } #endregion#region 公鑰加密,私鑰解密

////// RSA公鑰加密//////加密公鑰;為空則默認(rèn)系統(tǒng)公鑰///

需要加密的明文字符串///編碼格式;默認(rèn):UTF-8///RSA公鑰加密的密文public

staticstringRSAEncrypt筒音作5指法_Public(string xmlPublicKey, string enptStr, string encoding = "UTF-8") {

if (string.IsNullOrEmpty(xmlPublicKey)) { xmlPublicKey = _publicKey; }

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { byte

[] cipherbytes; rsa.FromX筒音作5指法mlString(xmlPublicKey); cipherbytes = rsa.Encrypt(Encoding.GetEncoding(encoding).GetBytes(enptStr),

false); return Convert.ToBase64String(cipherbytes); } }

////// RSA私鑰解密//////解密私鑰;為空則默認(rèn)系統(tǒng)公鑰///

需要加密的明文字符串///編碼格式;默認(rèn):UTF-8///RSA私鑰解密的明文public

staticstringRSADecrypt_Private(s筒音作5指法tring xmlPrivateKey, string enptStr, string encoding = "UTF-8") {

if (string.IsNullOrEmpty(xmlPrivateKey)) { xmlPrivateKey = _privateKey; }

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { byte

[] cipherbytes; rsa.FromXmlStr筒音作5指法ing(xmlPrivateKey); cipherbytes = rsa.Decrypt(Convert.FromBase64String(enptStr),

false); return Encoding.GetEncoding(encoding).GetString(cipherbytes); } }

#endregion#region 使用同一容器的名稱進(jìn)行RSA加密解密////// 進(jìn)行 RSA 加密//////

源字符串///加密后字符串publicstaticstringRsaEncrypt(string sourceSt筒音作5指法r) { Param =

new CspParameters(); //密匙容器的名稱,保持加密解密一致才能解密成功 Param.KeyContainerName = "Navis"

; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(Param)) {

//將要加密的字符串轉(zhuǎn)換成字節(jié)數(shù)組byte[] plaindata = Encoding.Default.GetBytes(sourceStr); //筒音作5指法通過字節(jié)數(shù)組進(jìn)行加密

byte[] encryptdata = rsa.Encrypt(plaindata, false); //將加密后的字節(jié)數(shù)組轉(zhuǎn)換成字符串return Convert.ToBase64String(encryptdata); } }

////// 通過RSA 加密方式進(jìn)行解密//////加密字符串///解密后字符串

publicstaticstringRsaDesEncrypt(string codingStr) { Param = new CspParameters(); Para筒音作5指法m.KeyContainerName =

"Navis"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(Param)) {

byte[] encryptdata = Convert.FromBase64String(codingStr); byte[] decryptdata = rsa.Decrypt(encryptdata,

false); return Encoding.Default.GetString(decryptdata); 筒音作5指法 } }

#endregion#region RSA分段加密:待加密的字符串拆開,每段長(zhǎng)度都小于等于限制長(zhǎng)度,然后分段加密////// RSA分段加密//////

RSA C#公鑰///需要進(jìn)行RSA加密的長(zhǎng)字符串///返回RSA加密后的密文public

static String SubRSAEncrypt(string xmlPublicKey, string enptStr) { RSACryptoServiceProvider provider =

new RSACryptoServiceProvider(); provid筒音作5指法er.FromXmlString(xmlPublicKey); Byte[] bytes = Encoding.Default.GetBytes(enptStr);

int MaxBlockSize = provider.KeySize / 8 - 11; //加密塊最大長(zhǎng)度限制if (bytes.Length <= MaxBlockSize)

return Convert.ToBase64String(provider.Encrypt(bytes, false)); using (MemoryStream PlaiStream =

new MemoryStream筒音作5指法(bytes)) using (MemoryStream CrypStream = new MemoryStream()) { Byte[] Buffer =

new Byte[MaxBlockSize]; int BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);

while (BlockSize > 0) { Byte[] ToEncrypt = new Byte[BlockSize]; Array.Copy(Buffer,

0,筒音作5指法 ToEncrypt, 0, BlockSize); Byte[] Cryptograph = provider.Encrypt(ToEncrypt, false

); CrypStream.Write(Cryptograph, 0, Cryptograph.Length); BlockSize = PlaiStream.Read(Buffer,

0, MaxBlockSize); } return Convert.ToBase64String(CrypStream.ToArray(), Base64Format筒音作5指法tingOptions.None); } }

////// RSA分段解密,應(yīng)對(duì)長(zhǎng)字符串//////RSA C#私鑰///

需要解密的長(zhǎng)字符串///返回RSA分段解密的明文publicstatic String SubRSADecrypt(string xmlPrivateKey,

string enptStr) { RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(xmlPrivateK筒音作5指法ey); Byte[] bytes = Convert.FromBase64String(enptStr);

int MaxBlockSize = provider.KeySize / 8; //解密塊最大長(zhǎng)度限制if (bytes.Length <= MaxBlockSize)

return Encoding.Default.GetString(provider.Decrypt(bytes, false)); using (MemoryStream CrypStream =

new MemoryStream(bytes)) using (Memor筒音作5指法yStream PlaiStream = new MemoryStream()) { Byte[] Buffer =

new Byte[MaxBlockSize]; int BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);

while (BlockSize > 0) { Byte[] ToDecrypt = new Byte[BlockSize]; Array.Copy(Buffer,

0, ToDecrypt, 0, BlockSize); 筒音作5指法 Byte[] Plaintext = provider.Decrypt(ToDecrypt, false

); PlaiStream.Write(Plaintext, 0, Plaintext.Length); BlockSize = CrypStream.Read(Buffer,

0, MaxBlockSize); } return Encoding.Default.GetString(PlaiStream.ToArray()); } }

#endregion#endregion

標(biāo)簽列表