`
zendj
  • 浏览: 115767 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

ASP.NET加密技术的应用(加密类代码参考)

阅读更多

ASP.NET加密技术的应用

加密类代码


/**//**//**//**********************CreatedbyChen**************************

*如果你觉得本人的文章好,要引用请尊重著作人的劳动果实,说明

*出处以及原创作者,Thankyou!!!email:aishen944-sohu.com

******************************************************************
*/


usingSystem;
usingSystem.Text;
usingSystem.Security;
usingSystem.Security.Cryptography;
usingSystem.IO;
namespaceEncryptClasses
...{
/**//**//**////<summary>
///此处定义的是DES加密,为了便于今后的管理和维护
///请不要随便改动密码,或者改变了密码后请一定要
///牢记先前的密码,否则将会照成不可预料的损失
///</summary>

publicclassDESEncrypt
...{
"memberfields""memberfields"#region"memberfields"
privatestringiv="12345678";
privatestringkey="12345678";
privateEncodingencoding=newUnicodeEncoding();
privateDESdes;
#endregion

/**//**//**////<summary>
///构造函数
///</summary>

publicDESEncrypt()
...{
des
=newDESCryptoServiceProvider();
}

"propertys""propertys"#region"propertys"
/**//**//**////<summary>
///设置加密密钥
///</summary>

publicstringEncryptKey
...{
get...{returnthis.key;}
set
...{
this.key=value;
}

}

/**//**//**////<summary>
///要加密字符的编码模式
///</summary>

publicEncodingEncodingMode
...{
get...{returnthis.encoding;}
set...{this.encoding=value;}
}

#endregion

"methods""methods"#region"methods"
/**//**//**////<summary>
///加密字符串并返回加密后的结果
///</summary>
///<paramname="str"></param>
///<returns></returns>

publicstringEncryptString(stringstr)
...{
byte[]ivb=Encoding.ASCII.GetBytes(this.iv);
byte[]keyb=Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密密钥
byte[]toEncrypt=this.EncodingMode.GetBytes(str);//得到要加密的内容
byte[]encrypted;
ICryptoTransformencryptor
=des.CreateEncryptor(keyb,ivb);
MemoryStreammsEncrypt
=newMemoryStream();
CryptoStreamcsEncrypt
=newCryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
csEncrypt.Write(toEncrypt,
0,toEncrypt.Length);
csEncrypt.FlushFinalBlock();
encrypted
=msEncrypt.ToArray();
csEncrypt.Close();
msEncrypt.Close();
returnthis.EncodingMode.GetString(encrypted);
}

/**//**//**////<summary>
///加密指定的文件,如果成功返回True,否则false
///</summary>
///<paramname="filePath">要加密的文件路径</param>
///<paramname="outPath">加密后的文件输出路径</param>

publicvoidEncryptFile(stringfilePath,stringoutPath)
...{
boolisExist=File.Exists(filePath);
if(isExist)//如果存在
...{
byte[]ivb=Encoding.ASCII.GetBytes(this.iv);
byte[]keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
//得到要加密文件的字节流
FileStreamfin=newFileStream(filePath,FileMode.Open,FileAccess.Read);
StreamReaderreader
=newStreamReader(fin,this.EncodingMode);
stringdataStr=reader.ReadToEnd();
byte[]toEncrypt=this.EncodingMode.GetBytes(dataStr);
fin.Close();

FileStreamfout
=newFileStream(outPath,FileMode.Create,FileAccess.Write);
ICryptoTransformencryptor
=des.CreateEncryptor(keyb,ivb);
CryptoStreamcsEncrypt
=newCryptoStream(fout,encryptor,CryptoStreamMode.Write);
try
...{
//加密得到的文件字节流
csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
csEncrypt.FlushFinalBlock();
}

catch(Exceptionerr)
...{
thrownewApplicationException(err.Message);
}

finally
...{
try
...{
fout.Close();
csEncrypt.Close();
}

catch
...{
;
}

}

}

else
...{
thrownewFileNotFoundException("没有找到指定的文件");
}

}

/**//**//**////<summary>
///文件加密函数的重载版本,如果不指定输出路径,
///那么原来的文件将被加密后的文件覆盖
///</summary>
///<paramname="filePath"></param>

publicvoidEncryptFile(stringfilePath)
...{
this.EncryptFile(filePath,filePath);
}

/**//**//**////<summary>
///解密给定的字符串
///</summary>
///<paramname="str">要解密的字符</param>
///<returns></returns>

publicstringDecryptString(stringstr)
...{
byte[]ivb=Encoding.ASCII.GetBytes(this.iv);
byte[]keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
byte[]toDecrypt=this.EncodingMode.GetBytes(str);
byte[]deCrypted=newbyte[toDecrypt.Length];
ICryptoTransformdeCryptor
=des.CreateDecryptor(keyb,ivb);
MemoryStreammsDecrypt
=newMemoryStream(toDecrypt);
CryptoStreamcsDecrypt
=newCryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
try
...{
csDecrypt.Read(deCrypted,
0,deCrypted.Length);
}

catch(Exceptionerr)
...{
thrownewApplicationException(err.Message);
}

finally
...{
try
...{
msDecrypt.Close();
csDecrypt.Close();
}

catch...{;}
}

returnthis.EncodingMode.GetString(deCrypted);
}

/**//**//**////<summary>
///解密指定的文件
///</summary>
///<paramname="filePath">要解密的文件路径</param>
///<paramname="outPath">解密后的文件输出路径</param>

publicvoidDecryptFile(stringfilePath,stringoutPath)
...{
boolisExist=File.Exists(filePath);
if(isExist)//如果存在
http://images.csdn.net/syntaxhighlighting/Outlin
分享到:
评论

相关推荐

    .NET加密技术剖析对称加密技术

    .NET加密技术剖析

    Web虎-asp.net软件加密保护-Windows机器码版-永久免费版本

    已知最可靠+方便的asp.net加密保护产品!选择web虎的10个理由 1.已有近600家各类b/s软件源代码加密防盗版成功案例,2+年稳定运行、实用检验,其中包括黑龙江卫生厅、宁夏卫生厅等 2.完全免费,并可提供远程协助 3....

    ASP.NET夜话(内容+源代码).zip

    表单知识、.NET运行机制、Visual Studio 2008技巧、ASP.NET服务器控件的使用、SQL注入防范、单元测试、三层架构、报表、数据加密/解密、GDI+、ADO.NET、分布式开发、AJAX开发及静态页面生成技术、高性能ASP.NET应用...

    ASP.NET网络在线考试系统(源代码+thesis).zip

    ASP.NET MVC是一种成熟的开发框架,它采用模型-视图-控制器的设计模式,将应用程序的逻辑和界面分离,提供了更好的代码组织和可维护性。我们选择ASP.NET MVC框架是因为它具有良好的可扩展性和灵活性,可以轻松地添加...

    ASP.NET基于CS应用程序平台多语种技术应用研究(源代码+thesis).zip

    ASP.NET MVC是一种成熟的开发框架,它采用模型-视图-控制器的设计模式,将应用程序的逻辑和界面分离,提供了更好的代码组织和可维护性。我们选择ASP.NET MVC框架是因为它具有良好的可扩展性和灵活性,可以轻松地添加...

    ASP.NET 4高级程序设计(第4版)源代码

    书中还深入讲述了其他ASP.NET图书遗漏的高级主题,如自定义控件的创建、图像处理、加密等。此外,《ASP.NET 4高级程序设计(第4版)》专门提供了两章的内容来教你如何用Ajax 技术制作快速响应的页面,以及如何使用微软...

    圣殿祭司的ASP.NET 4.0专家技术手册,完整扫描版

    ASP.NET 4.0技术概述、ASP.NET程序的编译模型、将ASP.NET程序开发服务器Port固定的技巧、C# 4.0...ASP.NET 网页授权、Profile配置文件应用、加密Profile配置文件、Web组件框架和Web组件的个性化数据的管理等方面的技术...

    ASP.NET常用技术

    ASP.NET在开发工作中需要使用到的各种基础知识点介绍。

    使用 ASP.NET 加密口令

    当我们在网站上建立数据库时,保护用户的信息安全是非常必要的。...在此我们使用ASP.NET技术对口令加密。简单的讲,就是将用户提供的口令加密之后,然后让它和存放于系统中的数据比较,如果相同,则通过验证。

    计算机专业毕设ASP.NET网络在线考试系统(源代码+论文).rar

    计算机专业毕设ASP.NET网络在线考试系统(源代码+论文)是一个完整的毕业设计项目,旨在帮助计算机专业的学生掌握ASP.NET技术并应用于实际项目中。本项目采用ASP.NET技术开发,基于B/S架构,实现了网络在线考试系统...

    ASP.NET图书馆管理信息系统(源代码+thesis).zip

    ASP.NET MVC是一种成熟的开发框架,它采用模型-视图-控制器的设计模式,将应用程序的逻辑和界面分离,提供了更好的代码组织和可维护性。我们选择ASP.NET MVC框架是因为它具有良好的可扩展性和灵活性,可以轻松地添加...

    asp.net 2.0网络编程自学手册源代码(网站会员注册与登录)

    人民邮电出版社的asp.net 2.0网络编程自学手册配套光盘中第14章网站会员注册与登录的源代码,掌握会员注册功能的实现、了解验证控件的使用、了解MD5加密技术、掌握会员登录功能的实现、了解验证码技术。

    asp.net开发常用整理集合

     Asp.net页面内传参数方法  调用存储过程(两种方法比较)  调用存储过程通用类DBHelper  加密解密  SQL常用DBHelper  Asp.net存储过程无限分类  TreeView无限分类  无限分类MVC  荧光棒效果 获取...

    ASP.NET 2.0动态网站开发基础教程(C#) 第09章 ASP.NET Web服务 传送数据(共9页).ppt

    基于C#语言的ASP.NET 2.0动态网站开发基础教程,课程列表如下: ASP.NET 2.0动态网站开发基础教程...ASP.NET 2.0动态网站开发基础教程(C#) 第11章 提高ASP.NET应用程序的安全性 身份验证和授权 SSL加密(共9页).ppt

    asp.net技术内幕(1)

    21.2.2 申请服务器证书 21.2.3 安装服务器证书 21.3 在ASP.NET页面中使用SSL 21.4 使用.NET加密类 21.4.1 使用散列算法 21.4.2 使用对称加密算法 21.4.3 使用不对称加密 21.5 小结 ...

    ASP.NET4高级程序设计(第4版).zip.001|中文版.完整书签

    此外,《ASP.NET 4高级程序设计(第4版)》专门提供了两章的内容来教你如何用Ajax 技术制作快速响应的页面,以及如何使用微软的ASP.NETAJAX平台。另外,还专门介绍了ASP.NET4 新增的功能,如MVC 和动态数据等。 《ASP...

    ASP.NET 2.0动态网站开发基础教程(C#) 第10章 配置ASP.NET应用程序 缓存技术(共12页).ppt

    基于C#语言的ASP.NET 2.0动态网站开发基础教程,课程列表如下: ASP.NET 2.0动态网站开发基础教程...ASP.NET 2.0动态网站开发基础教程(C#) 第11章 提高ASP.NET应用程序的安全性 身份验证和授权 SSL加密(共9页).ppt

    《asp.net夜话》非常棒的一本书

    表单知识、ASP.NET运行机制、Visual Studio 2008技巧、ASP.NET服务器控件的使用、SQL注入防范、单元测试、三层架构、报表、数据加密/解密、GDI+、ADO.NET、分布式开发、AJAX开发及静态页面生成技术、高性能ASP.NET...

    ASP.NET基础控件-教程

     Asp.net页面内传参数方法  调用存储过程(两种方法比较)  调用存储过程通用类DBHelper  加密解密  SQL常用DBHelper  Asp.net存储过程无限分类  TreeView无限分类  无限分类MVC  荧光棒效果获取控件上全选 ...

    Asp.net在线考勤系统研发(源代码+thesis).zip

    ASP.NET MVC是一种成熟的开发框架,它采用模型-视图-控制器的设计模式,将应用程序的逻辑和界面分离,提供了更好的代码组织和可维护性。我们选择ASP.NET MVC框架是因为它具有良好的可扩展性和灵活性,可以轻松地添加...

Global site tag (gtag.js) - Google Analytics