加密解密常用函数Word下载.docx

上传人:b****3 文档编号:14862149 上传时间:2022-10-25 格式:DOCX 页数:9 大小:16.18KB
下载 相关 举报
加密解密常用函数Word下载.docx_第1页
第1页 / 共9页
加密解密常用函数Word下载.docx_第2页
第2页 / 共9页
加密解密常用函数Word下载.docx_第3页
第3页 / 共9页
加密解密常用函数Word下载.docx_第4页
第4页 / 共9页
加密解密常用函数Word下载.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

加密解密常用函数Word下载.docx

《加密解密常用函数Word下载.docx》由会员分享,可在线阅读,更多相关《加密解密常用函数Word下载.docx(9页珍藏版)》请在冰豆网上搜索。

加密解密常用函数Word下载.docx

/summary>

paramname="

strSource"

>

待加密字串<

/param>

key"

32位Key值<

returns>

加密后的字符串<

/returns>

publicstringDESEncrypt(stringstrSource)

{

returnDESEncrypt(strSource,DESKey);

}

publicstringDESEncrypt(stringstrSource,byte[]key)

SymmetricAlgorithmsa=Rijndael.Create();

sa.Key=key;

sa.Mode=CipherMode.ECB;

sa.Padding=PaddingMode.Zeros;

MemoryStreamms=newMemoryStream();

CryptoStreamcs=newCryptoStream(ms,sa.CreateEncryptor(),CryptoStreamMode.Write);

byte[]byt=Encoding.Unicode.GetBytes(strSource);

cs.Write(byt,0,byt.Length);

cs.FlushFinalBlock();

cs.Close();

returnConvert.ToBase64String(ms.ToArray());

///DES解密

待解密的字串<

解密后的字符串<

publicstringDESDecrypt(stringstrSource)

returnDESDecrypt(strSource,DESKey);

publicstringDESDecrypt(stringstrSource,byte[]key)

ICryptoTransformct=sa.CreateDecryptor();

byte[]byt=Convert.FromBase64String(strSource);

MemoryStreamms=newMemoryStream(byt);

CryptoStreamcs=newCryptoStream(ms,ct,CryptoStreamMode.Read);

StreamReadersr=newStreamReader(cs,Encoding.Unicode);

returnsr.ReadToEnd();

#endregion

#region一个用hash实现的加密解密方法

///加密

src"

<

publicstaticstringEncryptStrByHash(stringsrc)

if(src.Length==0)

return"

"

;

byte[]HaKey=System.Text.Encoding.ASCII.GetBytes((src+"

Test"

).ToCharArray());

byte[]HaData=newbyte[20];

HMACSHA1Hmac=newHMACSHA1(HaKey);

CryptoStreamcs=newCryptoStream(Stream.Null,Hmac,CryptoStreamMode.Write);

try

cs.Write(HaData,0,HaData.Length);

finally

cs.Close();

stringHaResult=System.Convert.ToBase64String(Hmac.Hash).Substring(0,16);

byte[]RiKey=System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray());

byte[]RiDataBuf=System.Text.Encoding.ASCII.GetBytes(src.ToCharArray());

byte[]EncodedBytes= 

{};

RijndaelManagedrv=newRijndaelManaged();

cs=newCryptoStream(ms,rv.CreateEncryptor(RiKey,RiKey),CryptoStreamMode.Write);

cs.Write(RiDataBuf,0,RiDataBuf.Length);

cs.FlushFinalBlock();

EncodedBytes=ms.ToArray();

ms.Close();

returnHaResult+System.Convert.ToBase64String(EncodedBytes);

///解密

publicstaticstringDecrypStrByHash(stringsrc)

if(src.Length<

40)return"

byte[]SrcBytes=System.Convert.FromBase64String(src.Substring(16));

byte[]RiKey=System.Text.Encoding.ASCII.GetBytes(src.Substring(0,16).ToCharArray());

byte[]InitialText=newbyte[SrcBytes.Length];

MemoryStreamms=newMemoryStream(SrcBytes);

CryptoStreamcs=newCryptoStream(ms,rv.CreateDecryptor(RiKey,RiKey),CryptoStreamMode.Read);

cs.Read(InitialText,0,InitialText.Length);

System.Text.StringBuilderResult=newSystem.Text.StringBuilder();

for(inti=0;

i<

InitialText.Length;

++i)if(InitialText 

0)Result.Append((char)InitialText);

returnResult.ToString();

///对加密后的密文重新编码,如果密文长>

16,则去掉前16个字符,如果长度小于16,返回空字符串

para

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1