惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - loveabel

汉鼎咨询:创业板上市企业募投项目变更原因深度分析 [导入]球员各个位置的关键属性问题 好好学习JAVA和.Net hibernate的语法怎么搞 一些关于中文乱码问题的一些解决方案和经验 前台对象的事件一览 复制表结构的通用存储过程 webconfig身份验证票 什么是web.config 两台SqlServer数据同步解决方案 正则表达式 自认为以下文章比较经典,希望对初学者有用^_^ c#中对文件的操作小结 IT人士群聚喝酒的讲究 谁在呐喊软件蓝领,那是他们准备开培训学校,谁在呐喊自主版权,那是他希望民族主义帮助销售,谁在呐喊“核心技术”,这多半是准备骗政府投资。 日历 系统数据库 sql server中扩展存储过程随笔(几个有用的PROCEDURE小总结) 修改数据库(设置数据库选项)
C#中使用DES HMACSHA1 SHA1 RC2 MD5 进行字符串加密的例程
loveabel · 2005-08-19 · via 博客园 - loveabel

以下分别是使用了不同的加密方法加密/解密字符串:DES HMACSHA1 SHA1 RC2 MD5 等,待续 
using System;  
using System.IO;  
using System.Text;  
using System.Security.Cryptography;  

namespace Crypt  
{  
class Crypt  
{  

[STAThread]  
public static void Main(string[] args)  
{  
string Line;  
string Key;  
string IV;  
string Text;  
Console.WriteLine(
"Input DES,SHA1 key:");  
Key
=Console.ReadLine()+" ";  
Key
=Key.Substring(0,8);  
Console.WriteLine(
"Input DES IV:");  
IV
=Console.ReadLine()+" ";  
IV
=Key.Substring(0,8);  
Console.WriteLine(
"Input text:");  
Text
=Console.ReadLine();  
Line
=Crypt.DESEncrypt(Text,Key,IV);  
//DES Crypt  

Console.WriteLine(
"DES Crypt:{0}",Line);  
//MD5 Crypt  

Console.WriteLine(
"MD5 Crypt:{0}",Crypt.MD5Encrypt(Text));  
//HMACSHA1 Crypt  

Console.WriteLine(
"HMACSHA1 Crypt:{0}",Crypt.HMACSHA1Encrypt(Text,Key));  
//SHA1 Crypt  

Console.WriteLine(
"SHA1 Crypt:{0}",Crypt.SHA1Encrypt(Text));  
//RC2 Crypt  

RC2 myRC2
=new RC2(Text);  
Console.WriteLine(
"RC2 Crypt:{0}",myRC2.RC2Encrypt());  
/*  
Start decrypt  
*/
  
Console.WriteLine(
"Press any key to decrypt.");  
Console.ReadLine();  
//DES decrypt  

Console.WriteLine(
"DES decrypt:{0}",Crypt.DESDecrypt(Line,Key,IV));  
//RC2 decrypt  

Console.WriteLine(
"RC2 decrypt:{0}",myRC2.RC2Decrypt());  
Console.WriteLine(
"Press any key to exit.");  
Console.ReadLine();  
}
  

class RC2  
{  
private byte[] Key;  
private byte[] IV;  
private byte[] orgText;  
private byte[] encryptText;  
private byte[] decryptText;  
public RC2(string rc2Text)  
{  
orgText
=Encoding.Default.GetBytes(rc2Text);  
RC2CryptoServiceProvider myRC2
=new RC2CryptoServiceProvider();  
myRC2.GenerateIV();  
myRC2.GenerateKey();  
Key
=myRC2.Key;  
IV
=myRC2.IV;  
}
  
public string RC2Encrypt()  
{  
RC2CryptoServiceProvider myRC2
=new RC2CryptoServiceProvider();  
ICryptoTransform myCryptoTrans
=myRC2.CreateEncryptor(Key,IV);  
MemoryStream MStream
=new MemoryStream();  
CryptoStream CStream
=new CryptoStream(MStream,myCryptoTrans,CryptoStreamMode.Write);  
CStream.Write(orgText,
0,orgText.Length);  
CStream.FlushFinalBlock();  
StringBuilder EnText
=new StringBuilder();  
encryptText
=MStream.ToArray();  
foreach(byte Byte in encryptText)  
{  
EnText.AppendFormat(
"{0:x2}",Byte);  
}
  
CStream.Close();  
return EnText.ToString();  
}
  
public string RC2Decrypt()  
{  
RC2CryptoServiceProvider myRC2
=new RC2CryptoServiceProvider();  
ICryptoTransform myCryptoTrans
=myRC2.CreateDecryptor(Key,IV);  
MemoryStream MStream
=new MemoryStream(encryptText);  
CryptoStream CStream
=new CryptoStream(MStream,myCryptoTrans,CryptoStreamMode.Read);  
decryptText
=new byte[encryptText.Length];  
CStream.Read(decryptText,
0,decryptText.Length);  
StringBuilder EnText
=new StringBuilder();  
CStream.Close();  
ASCIIEncoding myText
=new ASCIIEncoding();  
return myText.GetString(decryptText);  
}
  
}
  
public static string SHA1Encrypt(string EncryptText)  
{  
byte[] StrRes=Encoding.Default.GetBytes(EncryptText);  
HashAlgorithm mySHA
=new SHA1CryptoServiceProvider();  
StrRes
=mySHA.ComputeHash(StrRes);  
StringBuilder EnText
=new StringBuilder();  
foreach(byte Byte in StrRes)  
{  
EnText.AppendFormat(
"{0:x2}",Byte);  
}
  
return EnText.ToString();  
}
  
public static string HMACSHA1Encrypt(string EncryptText,string EncryptKey)  
{  
byte[] StrRes=Encoding.Default.GetBytes(EncryptText);  
HMACSHA1 myHMACSHA1
=new HMACSHA1(Encoding.Default.GetBytes(EncryptKey));  
CryptoStream CStream
=new CryptoStream(Stream.Null,myHMACSHA1,CryptoStreamMode.Write);  
CStream.Write(StrRes,
0,StrRes.Length);  
StringBuilder EnText
=new StringBuilder();  
foreach(byte Byte in StrRes)  
{  
EnText.AppendFormat(
"{0:x2}",Byte);  
}
  
return EnText.ToString();  
}
  
public static string MD5Encrypt(string CryptText)  
{  
MD5 myMD5
=new MD5CryptoServiceProvider();  
byte[] HashCode;  
HashCode
=Encoding.Default.GetBytes(CryptText);  
HashCode
=myMD5.ComputeHash(HashCode);  
StringBuilder EnText
=new StringBuilder();  
foreach(byte Byte in HashCode)  
{  
EnText.AppendFormat(
"{0:x2}",Byte);  
}
  
return EnText.ToString();  
}
  
public static string DESEncrypt(string CryptText, string CryptKey,string CryptIV)  
{  
DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();  
byte[] textOut = Encoding.Default.GetBytes(CryptText);  
byte[] DESKey=ASCIIEncoding.ASCII.GetBytes(CryptKey);  
byte[] DESIV=ASCIIEncoding.ASCII.GetBytes(CryptKey);  
MemoryStream MStream 
= new MemoryStream();  
CryptoStream CStream 
= new CryptoStream(MStream, des.CreateEncryptor(DESKey,DESIV),CryptoStreamMode.Write);  
CStream.Write(textOut, 
0, textOut.Length);  
CStream.FlushFinalBlock();  
StringBuilder StrRes 
= new StringBuilder();  
foreach(byte Byte in MStream.ToArray())  
{  
StrRes.AppendFormat(
"{0:x2}", Byte);  
}
  
return StrRes.ToString();  
}
  

public static string DESDecrypt(string CryptText, string CryptKey,string CryptIV)  
{  
DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();  
byte[] textOut = new byte[CryptText.Length / 2];  
for(int Count = 0; Count < CryptText.Length; Count+=2)  
{  
textOut[Count
/2= (byte)(Convert.ToInt32(CryptText.Substring(Count , 2), 16));  
}
  
byte[] DESKey=ASCIIEncoding.ASCII.GetBytes(CryptKey);  
byte[] DESIV=ASCIIEncoding.ASCII.GetBytes(CryptIV);  
MemoryStream MStream 
= new MemoryStream();  
CryptoStream CStream 
= new CryptoStream(MStream, des.CreateDecryptor(DESKey,DESIV),CryptoStreamMode.Write);  
CStream.Write(textOut, 
0, textOut.Length);  
CStream.FlushFinalBlock();  
return System.Text.Encoding.Default.GetString(MStream.ToArray());  
}
  
}
  
}
  


调试结果  
================  
Input DES,SHA1 key:  
hello  
Input DES IV:  
world  
Input text:  
I am a boy my name 
is DexiangWu  
DES Crypt:9e701e4e8f55714bc68253d2ddf53fb6fd6a64df7f6b78f9491a64df18113642  
MD5 Crypt:f7d88e861602d2e1e0992dd70967314e  
HMACSHA1 Crypt:4920616d206120626f79206d79206e616d652069732044657869616e675775  
SHA1 Crypt:d5b25333f3e01ba96ac281e111af08701a7ccfb0  
RC2 Crypt:82cc5c822272a721db753e15d918c0d20e62322316fdd4a4d881b8eb03ff55e3  
Press any key to decrypt.  

DES decrypt:I am a boy my name 
is DexiangWu  
RC2 decrypt:I am a boy my name 
is DexiangWu