C#加密算法汇总.docx
- 文档编号:24992792
- 上传时间:2023-06-03
- 格式:DOCX
- 页数:28
- 大小:20.61KB
C#加密算法汇总.docx
《C#加密算法汇总.docx》由会员分享,可在线阅读,更多相关《C#加密算法汇总.docx(28页珍藏版)》请在冰豆网上搜索。
C#加密算法汇总
C#加密算法汇总
viewsourceprint?
01
方法一:
02
//须添加对System.Web的引用
03
usingSystem.Web.Security;
04
05
...
06
07
///
08
///SHA1加密字符串
09
///
10
///
11
///
12
publicstringSHA1(stringsource)
13
{
14
returnFormsAuthentication.HashPasswordForStoringInConfigFile(source,"SHA1");
15
}
16
17
18
///
19
///MD5加密字符串
20
///
21
///
22
///
23
publicstringMD5(stringsource)
24
{
25
returnFormsAuthentication.HashPasswordForStoringInConfigFile(source,"MD5");;
26
}
viewsourceprint?
01
方法二(可逆加密解密):
02
usingSystem.Security.Cryptography;
03
04
...
05
06
publicstringEncode(stringdata)
07
{
08
byte[]byKey=System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
09
byte[]byIV=System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
10
11
DESCryptoServiceProvidercryptoProvider=newDESCryptoServiceProvider();
12
inti=cryptoProvider.KeySize;
13
MemoryStreamms=newMemoryStream();
14
CryptoStreamcst=newCryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write);
15
16
StreamWritersw=newStreamWriter(cst);
17
sw.Write(data);
18
sw.Flush();
19
cst.FlushFinalBlock();
20
sw.Flush();
21
returnConvert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length);
22
23
}
24
25
publicstringDecode(stringdata)
26
{
27
byte[]byKey=System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
28
byte[]byIV=System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
29
30
byte[]byEnc;
31
try
32
{
33
byEnc=Convert.FromBase64String(data);
34
}
35
catch
36
{
37
returnnull;
38
}
39
40
DESCryptoServiceProvidercryptoProvider=newDESCryptoServiceProvider();
41
MemoryStreamms=newMemoryStream(byEnc);
42
CryptoStreamcst=newCryptoStream(ms,cryptoProvider.CreateDecryptor(byKey,byIV),CryptoStreamMode.Read);
43
StreamReadersr=newStreamReader(cst);
44
returnsr.ReadToEnd();
45
}
viewsourceprint?
01
方法三(MD5不可逆):
02
usingSystem.Security.Cryptography;
03
04
...
05
06
//MD5不可逆加密
07
08
//32位加密
09
10
publicstringGetMD5_32(strings,string_input_charset)
11
{
12
MD5md5=newMD5CryptoServiceProvider();
13
byte[]t=md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
14
StringBuildersb=newStringBuilder(32);
15
for(inti=0;i 16 { 17 sb.Append(t[i].ToString("x").PadLeft(2,'0')); 18 } 19 returnsb.ToString(); 20 } 21 22 //16位加密 23 publicstaticstringGetMd5_16(stringConvertString) 24 { 25 MD5CryptoServiceProvidermd5=newMD5CryptoServiceProvider(); 26 stringt2=BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)),4,8); 27 t2=t2.Replace("-",""); 28 returnt2; 29 } viewsourceprint? 01 方法四(对称加密): 02 usingSystem.IO; 03 usingSystem.Security.Cryptography; 04 05 ... 06 07 privateSymmetricAlgorithmmobjCryptoService; 08 privatestringKey; 09 /// 10 ///对称加密类的构造函数 11 /// 12 publicSymmetricMethod() 13 { 14 mobjCryptoService=newRijndaelManaged(); 15 Key="Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj! y6&(*jkP87jH7"; 16 } 17 /// 18 ///获得密钥 19 /// 20 /// 21 privatebyte[]GetLegalKey() 22 { 23 stringsTemp=Key; 24 mobjCryptoService.GenerateKey(); 25 byte[]bytTemp=mobjCryptoService.Key; 26 intKeyLength=bytTemp.Length; 27 if(sTemp.Length>KeyLength) 28 sTemp=sTemp.Substring(0,KeyLength); 29 elseif(sTemp.Length 30 sTemp=sTemp.PadRight(KeyLength,''); 31 returnASCIIEncoding.ASCII.GetBytes(sTemp); 32 } 33 /// 34 ///获得初始向量IV 35 /// 36 /// 37 privatebyte[]GetLegalIV() 38 { 39 stringsTemp="E4ghj*Ghg7! rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&! hg4ui%$hjk"; 40 mobjCryptoService.GenerateIV(); 41 byte[]bytTemp=mobjCryptoService.IV; 42 intIVLength=bytTemp.Length; 43 if(sTemp.Length>IVLength) 44 sTemp=sTemp.Substring(0,IVLength); 45 elseif(sTemp.Length 46 sTemp=sTemp.PadRight(IVLength,''); 47 returnASCIIEncoding.ASCII.GetBytes(sTemp); 48 } 49 /// 50 ///加密方法 51 /// 52 /// 53 /// 54 publicstringEncrypto(stringSource) 55 { 56 byte[]bytIn=UTF8Encoding.UTF8.GetBytes(Source); 57 MemoryStreamms=newMemoryStream(); 58 mobjCryptoService.Key=GetLegalKey(); 59 mobjCryptoService.IV=GetLegalIV(); 60 ICryptoTransformencrypto=mobjCryptoService.CreateEncryptor(); 61 CryptoStreamcs=newCryptoStream(ms,encrypto,CryptoStreamMode.Write); 62 cs.Write(bytIn,0,bytIn.Length); 63 cs.FlushFinalBlock(); 64 ms.Close(); 65 byte[]bytOut=ms.ToArray(); 66 returnConvert.ToBase64String(bytOut); 67 } 68 /// 69 ///解密方法 70 /// 71 /// 72 /// 73 publicstringDecrypto(stringSource) 74 { 75 byte[]bytIn=Convert.FromBase64String(Source); 76 MemoryStreamms=newMemoryStream(bytIn,0,bytIn.Length); 77 mobjCryptoService.Key=GetLegalKey(); 78 mobjCryptoService.IV=GetLegalIV(); 79 ICryptoTransformencrypto=mobjCryptoService.CreateDecryptor(); 80 CryptoStreamcs=newCryptoStream(ms,encrypto,CryptoStreamMode.Read); 81 StreamReadersr=newStreamReader(cs); 82 returnsr.ReadToEnd(); 83 } viewsourceprint? 01 方法五: 02 usingSystem.IO; 03 usingSystem.Security.Cryptography; 04 usingSystem.Text; 05 06 ... 07 08 //默认密钥向量 09 privatestaticbyte[]Keys={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF}; 10 /// 11 ///DES加密字符串 12 /// 13 /// 14 /// 15 /// 16 publicstaticstringEncryptDES(stringencryptString,stringencryptKey) 17 { 18 try 19 { 20 byte[]rgbKey=Encoding.UTF8.GetBytes(encryptKey.Substring(0,8)); 21 byte[]rgbIV=Keys; 22 byte[]inputByteArray=Encoding.UTF8.GetBytes(encryptString); 23 DESCryptoServiceProviderdCSP=newDESCryptoServiceProvider(); 24 MemoryStreammStream=newMemoryStream(); 25 CryptoStreamcStream=newCryptoStream(mStream,dCSP.CreateEncryptor(rgbKey,rgbIV),CryptoStreamMode.Write); 26 cStream.Write(inputByteArray,0,inputByteArray.Length); 27 cStream.FlushFinalBlock(); 28 returnConvert.ToBase64String(mStream.ToArray()); 29 } 30 catch 31 { 32 returnencryptString; 33 } 34 } 35 36 /// 37 ///DES解密字符串 38 /// 39 /// 40 /// 41 /// 42 publicstaticstringDecryptDES(stringdecryptString,stringdecryptKey) 43 { 44 try 45 { 46 byte[]rgbKey=Encoding.UTF8.GetBytes(decryptKey); 47 byte[]rgbIV=Keys; 48 byte[]inputByteArray=Convert.FromBase64String(decryptString); 49 DESCryptoServiceProviderDCSP=newDESCryptoServiceProvider(); 50 MemoryStreammStream=newMemoryStream(); 51 CryptoStreamcStream=newCryptoStream(mStream,DCSP.CreateDecryptor(rgbKey,rgbIV),CryptoStreamMode.Write); 52 cStream.Write(inputByteArray,0,inputByteArray.Length); 53 cStream.FlushFinalBlock(); 54 returnEncoding.UTF8.GetString(mStream.ToArray()); 55 } 56 catch 57 { 58 returndecryptString; 59 } 60 } viewsourceprint? 01 方法六(文件加密): 02 usingSystem.IO; 03 usingSystem.Security.Cryptography; 04 usingSystem.Text; 05 06 ... 07 08 //加密文件 09 privatestaticvoidEncryptData(StringinName,StringoutName,byte[]desKey,byte[]desIV) 10 { 11 //Createthefilestreamstohandletheinputandoutputfiles. 12 FileStreamfin=newFileStream(inName,FileMode.Open,FileAccess.Read); 13 FileStreamfout=newFileStream(outName,FileMode.OpenOrCreate,FileAccess.Write); 14 fout.SetLength(0); 15 16 //Createvariablestohelpwithreadandwrite. 17 byte[]bin=newbyte[100];//Thisisintermediatestoragefortheencryption. 18 longrdlen=0;//Thisisthetotalnumberofbyteswritten. 19 longtotlen=fin.Length;//Thisisthetotallengthoftheinputfile. 20 intlen;//Thisisthenumberofbytestobewrittenatatime. 21 22 DESdes=newDESCryptoServiceProvider(); 23 CryptoStreamencStream=newCryptoStream(fout,des.CreateEncryptor(desKey,desIV),CryptoStreamMode.Write); 24 25 //Readfromtheinputfile,thenencryptandwritetotheoutputfile. 26 while(rdlen 27 { 28 len=fin.Read(bin,0,100);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 加密算法 汇总