问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.IO;usingSystem.Security.Cryptography;namespaceDESTool{publicclassMYDES{publicstaticSystem.Security.Cryptography.DESmydes=newDESCryptoServiceProvider();privatestaticstringm_key="";privatestaticstringm_iv="";#region解密文件返回解密出的字符串stringDecode(stringinFileName,stringkey,stringiv)publicstaticstringDecode(stringinFileName,stringkey,stringiv){m_key=key;m_iv=iv;stringresult="";try{mydes.IV=MYDES.GetLegalIV();mydes.Key=MYDES.GetLegalKey();MemoryStreammStream=newMemoryStream();ICryptoTransformencrypto=mydes.CreateDecryptor();CryptoStreamencStream=newCryptoStream(mStream,encrypto,CryptoStreamMode.Write);byte[]btFile=File.ReadAllBytes(inFileName);encStream.Write(btFile,0,btFile.Length);encStream.FlushFinalBlock();result=Encoding.Default.GetString(mStream.ToArray());encStream.Close();mStream.Close();}catch(Exceptionex){return"解密失败";}returnresult;}#endregion#region解密文件解密后生成新的文件返回bool指示解密成功失败情况boolDecode(stringinFileName,stringoutFileName,stringkey,stringiv)publicstaticboolDecode(stringinFileName,stringoutFileName,stringkey,stringiv){m_key=key;m_iv=iv;try{if(outFileName.Trim().Length==0)outFileName=inFileName+".decry";FileStreamfout=newFileStream(outFileName,FileMode.OpenOrCreate,FileAccess.Write);mydes.IV=MYDES.GetLegalIV();mydes.Key=MYDES.GetLegalKey();ICryptoTransformencrypto=mydes.CreateDecryptor();CryptoStreamencStream=newCryptoStream(fout,encrypto,CryptoStreamMode.Write);byte[]btFile=File.ReadAllBytes(inFileName);encStream.Write(btFile,0,btFile.Length);encStream.FlushFinalBlock();encStream.Close();fout.Close();returntrue;}catch(Exceptionex){returnfalse;}}#endregion#region加密文件返回加密后的字符串stringEncode(stringinFileName,stringkey,stringiv)publicstaticstringEncode(stringinFileName,stringkey,stringiv){m_key=key;m_iv=iv;stringresult="";try{mydes.IV=MYDES.GetLegalIV();mydes.Key=MYDES.GetLegalKey();MemoryStreammStream=newMemoryStream();ICryptoTransformencrypto=mydes.CreateEncryptor();CryptoStreamencStream=newCryptoStream(mStream,encrypto,CryptoStreamMode.Write);byte[]btFile=File.ReadAllBytes(inFileName);encStream.Write(btFile,0,btFile.Length);encStream.FlushFinalBlock();result=Convert.ToBase64String(mStream.ToArray());encStream.Close();mStream.Close();}catch(Exceptionex){return"加密失败!";}returnresult;}#endregion#region加密文件加密后生成新的文件返回bool指示加密成功失败情况boolEncode(stringinFileName,stringoutFileName,stringkey,stringiv)publicstaticboolEncode(stringinFileName,stringoutFileName,stringkey,stringiv){m_key=key;m_iv=iv;try{if(outFileName.Trim().Length==0)outFileName=inFileName+".enc";FileStreamfout=newFileStream(outFileName,FileMode.OpenOrCreate,FileAccess.Write);mydes.IV=MYDES.GetLegalIV();mydes.Key=MYDES.GetLegalKey();ICryptoTransformencrypto=mydes.CreateEncryptor();CryptoStreamencStream=newCryptoStream(fout,encrypto,CryptoStreamMode.Write);byte[]btFile=File.ReadAllBytes(inFileName);encStream.Write(btFile,0,btFile.Length);encStream.FlushFinalBlock();encStream.Close();fout.Close();returntrue;}catch(Exceptionex){returnfalse;}}#endregion#region解密字符串返回解密后的字符串stringDecrypt(stringsrc,stringkey,stringiv)publicstaticstringDecrypt(stringsrc,stringkey,stringiv){m_key=key;m_iv=iv;stringresult="";try{mydes.IV=MYDES.GetLegalIV();mydes.Key=MYDES.GetLegalKey();byte[]btFile=Convert.FromBase64String(src);MemoryStreammStream=newMemoryStream();ICryptoTransformencrypto=mydes.CreateDecryptor();CryptoStreamencStream=newCryptoStream(mStream,encrypto,CryptoStreamMode.Write);encStream.Write(btFile,0,btFile.Length);encStream.FlushFinalBlock();result=Encoding.Default.GetString(mStream.ToArray());encStream.Close();mStream.Close();}catch(Exceptionex){return"解密失败";}returnresult;}#endregion#region解密字符串生成解密文件返回bool值boolDecrypt(stringsrc,stringoutFileName,stringkey,stringiv)publicstaticboolDecrypt(stringsrc,stringoutFileName,stringkey,stringiv){m_key=key;m_iv=iv;try{FileStreamfout=newFileStream(outFileName,FileMode.OpenOrCreate,FileAccess.Write);mydes.IV=MYDES.GetLegalIV();mydes.Key=MYDES.GetLegalKey();ICryptoTransformencrypto=mydes.CreateDecryptor();CryptoStreamencStream=newCryptoStream(fout,encrypto,CryptoStreamMode.Write);byte[]btFile=Convert.FromBase64String(src);encStream.Write(btFile,0,btFile.Length);encStream.FlushFinalBlock();encStream.Close();fout.Close();returntrue;}catch(Exceptionex){returnfalse;}}#endregion#region加密字符串返回加密后的字符串stringEncrypt(stringsrc,stringkey,stringiv)publicstaticstringEncrypt(stringsrc,stringkey,stringiv){m_key=key;m_iv=iv;stringresult="";try{mydes.IV=MYDES.GetLegalIV();mydes.Key=MYDES.GetLegalKey();byte[]btFile=Encoding.Default.GetBytes(src);MemoryStreammStream=newMemoryStream();ICryptoTransformencrypto=mydes.CreateEncryptor();CryptoStreamencStream=newCryptoStream(mStream,encrypto,CryptoStreamMode.Write);encStream.Write(btFile,0,btFile.Length);encStream.FlushFinalBlock();result=Convert.ToBase64String(mStream.ToArray());encStream.Close();mStream.Close();}catch(Exceptionex){return"";}returnresult;}#endregion#region加密字符串生成加密文件返回bool值boolEncrypt(stringsrc,stringoutFileName,stringkey,stringiv)publicstaticboolEncrypt(stringsrc,stringoutFileName,stringkey,stringiv){m_key=key;m_iv=iv;try{FileStreamfout=newFileStream(outFileName,FileMode.OpenOrCreate,FileAccess.Write);mydes.IV=MYDES.GetLegalIV();mydes.Key=MYDES.GetLegalKey();ICryptoTransformencrypto=mydes.CreateEncryptor();CryptoStreamencStream=newCryptoStream(fout,encrypto,CryptoStreamMode.Write);byte[]btFile=Encoding.Default.GetBytes(src);encStream.Write(btFile,0,btFile.Length);encStream.FlushFinalBlock();encStream.Close();fout.Close();returntrue;}catch(Exceptionex){returnfalse;}}#endregionprivatestaticbyte[]GetLegalKey(){stringsTemp=m_key;mydes.GenerateKey();byte[]bytTemp=mydes.Key;intKeyLength=bytTemp.Length;if(sTemp.Length>KeyLength)sTemp=sTemp.Substring(0,KeyLength);elseif(sTemp.Length<KeyLength)sTemp=sTemp.PadRight(KeyLength,'');returnASCIIEncoding.ASCII.GetBytes(sTemp);}privatestaticbyte[]GetLegalIV(){stringsTemp=m_iv;mydes.GenerateIV();byte[]bytTemp=mydes.IV;intIVLength=bytTemp.Length;if(sTemp.Length>IVLength)sTemp=sTemp.Substring(0,IVLength);elseif(sTemp.Length<IVLength)sTemp=sTemp.PadRight(IVLength,'');returnASCIIEncoding.ASCII.GetBytes(sTemp);}}}
解决方案
本帖最后由 caozhy 于 2014-01-14 16:22:06 编辑
解决方案二:
多无聊的人会看这些东西帮你改...
解决方案三:
我也不知道,或许有好心人的
解决方案四:
帖子上加上出错信息可能好点(很少人会复制这些代码运行一次的)
解决方案五:
晕晕