`

.NET加密技术应用

    博客分类:
  • .NET
阅读更多

using System;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.IO;
namespace EncryptClasses
{
 /// <summary>
 /// 此处定义的是DES加密,为了便于今后的管理和维护
 /// 请不要随便改动密码,或者改变了密码后请一定要
 /// 牢记先前的密码,否则将会照成不可预料的损失
 /// </summary>
 public class DESEncrypt
 {
  #region "member fields"
  private string iv="12345678";
  private string key="12345678";
  private Encoding encoding=new UnicodeEncoding();
  private DES des;
  #endregion
  /// <summary>
  /// 构造函数
  /// </summary>
  public DESEncrypt()
  {
   des=new DESCryptoServiceProvider();
  }
  #region "propertys"
  /// <summary>
  /// 设置加密密钥
  /// </summary>
  public string EncryptKey
  {
   get{return this.key;}
   set
   {
      this.key=value;
   }
  }
  /// <summary>
  /// 要加密字符的编码模式
  /// </summary>
  public Encoding EncodingMode
  {
   get{return this.encoding;}
   set{this.encoding=value;}
  }
  #endregion
  #region "methods"
  /// <summary>
  /// 加密字符串并返回加密后的结果
  /// </summary>
  /// <param name="str"></param>
  /// <returns></returns>
  public string EncryptString(string str)
  {
   byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
   byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密密钥
   byte[] toEncrypt=this.EncodingMode.GetBytes(str);//得到要加密的内容
   byte[] encrypted;
   ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
   MemoryStream msEncrypt=new MemoryStream();
   CryptoStream csEncrypt=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
   csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
   csEncrypt.FlushFinalBlock();
   encrypted=msEncrypt.ToArray();
   csEncrypt.Close();
   msEncrypt.Close();
   return this.EncodingMode.GetString(encrypted);
  }
  /// <summary>
  /// 加密指定的文件,如果成功返回True,否则false
  /// </summary>
  /// <param name="filePath">要加密的文件路径</param>
  /// <param name="outPath">加密后的文件输出路径</param>
  public void EncryptFile(string filePath,string outPath)
  {
   bool isExist=File.Exists(filePath);
   if(isExist)//如果存在
   {
    byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
    byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
    //得到要加密文件的字节流
    FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
    StreamReader reader=new StreamReader(fin,this.EncodingMode);
    string dataStr=reader.ReadToEnd();
    byte[] toEncrypt=this.EncodingMode.GetBytes(dataStr);
    fin.Close();
    FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
    ICryptoTransform encryptor=des.CreateEncryptor(keyb,ivb);
    CryptoStream csEncrypt=new CryptoStream(fout,encryptor,CryptoStreamMode.Write);
    try
    {
     //加密得到的文件字节流
     csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
     csEncrypt.FlushFinalBlock();
    }
    catch(Exception err)
    {
     throw new ApplicationException(err.Message);
    }
    finally
    {
     try
     {
      fout.Close();
      csEncrypt.Close();
     }
     catch
     {
      ;
     }
    }
   }
   else
   {
    throw new FileNotFoundException("没有找到指定的文件");
   }
  }
  /// <summary>
  /// 文件加密函数的重载版本,如果不指定输出路径,
  /// 那么原来的文件将被加密后的文件覆盖
  /// </summary>
  /// <param name="filePath"></param>
  public void EncryptFile(string filePath)
  {
   this.EncryptFile(filePath,filePath);
  }
  /// <summary>
  /// 解密给定的字符串
  /// </summary>
  /// <param name="str">要解密的字符</param>
  /// <returns></returns>
  public string DecryptString(string str)
  {
   byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
   byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
   byte[] toDecrypt=this.EncodingMode.GetBytes(str);
   byte[] deCrypted=new byte[toDecrypt.Length];
   ICryptoTransform deCryptor=des.CreateDecryptor(keyb,ivb);
   MemoryStream msDecrypt=new MemoryStream(toDecrypt);
   CryptoStream csDecrypt=new CryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
   try
   {
    csDecrypt.Read(deCrypted,0,deCrypted.Length);
   }
   catch(Exception err)
   {
    throw new ApplicationException(err.Message);
   }
   finally
   {
    try
    {
     msDecrypt.Close();
     csDecrypt.Close();
    }
    catch{;}
   }
   return this.EncodingMode.GetString(deCrypted);
  }
  /// <summary>
  /// 解密指定的文件
  /// </summary>
  /// <param name="filePath">要解密的文件路径</param>
  /// <param name="outPath">解密后的文件输出路径</param>
  public void DecryptFile(string filePath,string outPath)
  {
   bool isExist=File.Exists(filePath);
   if(isExist)//如果存在
   {
    byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
    byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
    FileInfo file=new FileInfo(filePath);
    byte[] deCrypted=new byte[file.Length];
    //得到要解密文件的字节流
    FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
    //解密文件
    try
    {
     ICryptoTransform decryptor=des.CreateDecryptor(keyb,ivb);
     CryptoStream csDecrypt=new CryptoStream(fin,decryptor,CryptoStreamMode.Read);
     csDecrypt.Read(deCrypted,0,deCrypted.Length);
    }
    catch(Exception err)
    {
     throw new ApplicationException(err.Message);
    }
    finally
    {
     try
     {
      fin.Close();
     }
     catch{;}
    }
    FileStream fout=new FileStream(outPath,FileMode.Create,FileAccess.Write);
    fout.Write(deCrypted,0,deCrypted.Length);
    fout.Close();
   }
   else
   {
    throw new FileNotFoundException("指定的解密文件没有找到");
   }
  }
  /// <summary>
  /// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
  /// 则解密后的文件将覆盖先前的文件
  /// </summary>
  /// <param name="filePath"></param>
  public void DecryptFile(string filePath)
  {
   this.DecryptFile(filePath,filePath);
  }
  #endregion
 }
 /// <summary>
 /// MD5加密类,注意经MD5加密过的信息是不能转换回原始数据的
 /// ,请不要在用户敏感的信息中使用此加密技术,比如用户的密码,
 /// 请尽量使用对称加密
 /// </summary>
 public class MD5Encrypt
 {
  private MD5 md5;
  public MD5Encrypt()
  {
   md5=new MD5CryptoServiceProvider();
  }
  /// <summary>
  /// 从字符串中获取散列值
  /// </summary>
  /// <param name="str">要计算散列值的字符串</param>
  /// <returns></returns>
  public string GetMD5FromString(string str)
  {
   byte[] toCompute=Encoding.Unicode.GetBytes(str);
   byte[] hashed=md5.ComputeHash(toCompute,0,toCompute.Length);
   return Encoding.ASCII.GetString(hashed);
  }
  /// <summary>
  /// 根据文件来计算散列值
  /// </summary>
  /// <param name="filePath">要计算散列值的文件路径</param>
  /// <returns></returns>
  public string GetMD5FromFile(string filePath)
  {
   bool isExist=File.Exists(filePath);
   if(isExist)//如果文件存在
   {
    FileStream stream=new FileStream(filePath,FileMode.Open,FileAccess.Read);
    StreamReader reader=new StreamReader(stream,Encoding.Unicode);
    string str=reader.ReadToEnd();
    byte[] toHash=Encoding.Unicode.GetBytes(str);
    byte[] hashed=md5.ComputeHash(toHash,0,toHash.Length);
    stream.Close();
    return Encoding.ASCII.GetString(hashed);
   }
   else//文件不存在
   {
    throw new FileNotFoundException("指定的文件没有找到");
   }
  }
 }
 /// <summary>
 /// 用于数字签名的hash类
 /// </summary>
 public class MACTripleDESEncrypt
 {
  private MACTripleDES mact;
  private string __key="ksn168ch";
  private byte[] __data=null;
  public MACTripleDESEncrypt()
  {
   mact=new MACTripleDES();
  }
  /// <summary>
  /// 获取或设置用于数字签名的密钥
  /// </summary>
  public string Key
  {
   get{return this.__key;}
   set
   {
    int keyLength=value.Length;
    int[] keyAllowLengths=new int[]{8,16,24};
    bool isRight=false;
    foreach(int i in keyAllowLengths)
    {
     if(keyLength==keyAllowLengths[i])
     {
      isRight=true;
      break;
     }
    }
    if(!isRight)
     throw new ApplicationException("用于数字签名的密钥长度必须是8,16,24值之一");
    else
     this.__key=value;
   }
  }
  /// <summary>
  /// 获取或设置用于数字签名的用户数据
  /// </summary>
  public byte[] Data
  {
   get{return this.__data;}
   set{this.__data=value;}
  }
  /// <summary>
  /// 得到签名后的hash值
  /// </summary>
  /// <returns></returns>
  public string GetHashValue()
  {
   if(this.Data==null)
    throw new NotSetSpecialPropertyException("没有设置要进行数字签名的用户"+
                                         "数据(property:Data)");
   byte[] key=Encoding.ASCII.GetBytes(this.Key);
   this.mact.Key=key;
   byte[] hash_b=this.mact.ComputeHash(this.mact.ComputeHash(this.Data));
   return Encoding.ASCII.GetString(hash_b);
  }
 }
}
 
分享到:
评论

相关推荐

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

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

    .NET之美:.NET关键技术深入解析 - 张子阳

    第二部分(6~17章)则对.NET中的关键知识点进行了深入剖析,如程序集、流和序列化、加密与解密、网络编程、.NET Remoting、在.NET中操作XML、.NET应用程序配置、基于角色的安全性、反射、多线程、对象生存期与垃圾...

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

    我们还将使用加密技术来保护用户的敏感信息,如密码和支付信息。 缓存管理:为了提高应用程序的性能,我们将使用ASP.NET提供的缓存机制来缓存常用的数据和页面。这将减少对数据库和服务器的访问次数,提高应用程序的...

    .NET Web安全技术分析及应用实践

    主要在讨论常见WEB应用安全防护技术的基础上,重点研究Web.config配置文件的加密与解密、图文验证技术、防盗链技术

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

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

    MFC应用程序在.NET框架下的扩展

    本书详细介绍了在现有MFC应用程序中综合运用.NET框架的技术。全书共分11章,内容包括正则表达式、文件I/O和注册表、数据加密、XML和DOM、ADO .NET数据库、远程处理、事件日志等。为了让读者透彻理解如何运用.NET框架...

    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 小结 ...

    基于.NET的现代密码技术应用编程(一)(1).PDF

    .net加密,提供安全性的代码,更有利于大家讨论此项技术

    .NET之美:.NET关键技术深入分析

    9.2.NET对加密和解密的支持 9.2.1散列运算 9.2.2对称加密和解密 9.2.3非对称加密 9.3本章小结 第10章网络编程 10.1网络编程基本概念 10.1.1 面向连接的传输协议——TCP 10.1.2 即时通信程序的三种模式 10.2...

    MFC应用程序在.NET框架下的扩展(2-1)

    本书详细介绍了在现有MFC应用程序中综合运用.NET框架的技术。全书共分11章,内容包括正则表达式、文件I/O和注册表、数据加密、XML和DOM、ADO .NET数据库、远程处理、事件日志等。为了让读者透彻理解如何运用.NET框架...

    asp.net知识库

    将 ASP.NET 2.0 应用程序服务配置为使用 SQL Server 2000 或 SQL Server 2005 ASP.NET 2.0 中的数据源控件 使用 ASP.NET 2.0 ObjectDataSource 控件 ASP.NET 2.0 的内部变化 使用SQL Cache Dependency 代替 ...

    ASP.NET《数据库原理及应用技术》课程指导平台的开发(源代码+thesis).zip

    我们还将使用加密技术来保护用户的敏感信息,如密码和支付信息。 缓存管理:为了提高应用程序的性能,我们将使用ASP.NET提供的缓存机制来缓存常用的数据和页面。这将减少对数据库和服务器的访问次数,提高应用程序的...

    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

    Eziriz .Net Reactor v4.2.8.4加密程序

    强大的软件防盗版工具:.NET Reactor ...保护.NET应用程序和.NET库文件 CIL代码与本地代码替换执行额外的保护层,包括混淆,程序集合并,在试用版中加入多种功能锁, 从而让用户更加愿意购买正版。

    .net技术资料大全(语言规范 源码教程 学习笔记 技术资料 .net代码生成器)

    ASP.NET编程技术与交互式网页设计 asp.net亲密接触_带源码 C#学习 01_类.htm.txt 02_构造函数的执行序列.htm.txt 03_抽象类和接口.htm.txt 04_结构类型.htm.txt 05_类成员的定义.htm.txt 06_类成员的其他...

    ASP.net技术内幕

    ASP的最新版本ASP.NET是Microsoft用于建立动态的数据库驱动网站的技术。内容包括:ASP.NET Web表单的使用,高级ASP.NET页面的开发,ADO.NET的使用,ASP.NET应用程序的使用, ASP.NET应用程序的保护,ASP.NET Web服务...

    Asp.net夜话

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

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

    我们还将使用加密技术来保护用户的敏感信息,如密码和支付信息。 缓存管理:为了提高应用程序的性能,我们将使用ASP.NET提供的缓存机制来缓存常用的数据和页面。这将减少对数据库和服务器的访问次数,提高应用程序的...

    构建安全的 ASP.NET 应用程序(中文译作+英文原作)

    • 加密技术、密钥和证书 • 词汇表 返回页首 系统要求 本指南将帮助您使用 .NET Framework 针对 Windows 2000 设计和构建安全的 ASP.NET 应用程序。我们以 .NET Framework 版本 1 (service pack 2) 为目标,虽然...

    ASP.NET注册登录发送邮箱验证功能源码

    如果你想设置发送邮箱可以...学习和讨论有关asp.net mvc ,Ajax ,jquery ,html/css, xml,sqlserver ,wpf,IIS以及服务器的搭建和安全性相关技术的交流和学习。 开发环境为Visual Studio 2010,数据库为SQL Server 2008R2

Global site tag (gtag.js) - Google Analytics