您的位置: 我要加密首页 >> 加密技术 >> 阅读文档:DOTNET的加密技术应用

DOTNET的加密技术应用

[ 作者: | 更新日期:2007-6-29 09:09:38 | 阅读次数: | 评论 0 条 | 我要投稿 ]

本文是向大家介绍下DOTNET的加密技术应用,虽然这种加密技术我们日常生活中用不到,但看看会很有用的,嘿嘿 不过没有相关的知识是很难看懂的 希望有兴趣的研究研究!

字串4

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"

字串2


  /// <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);//得到加密密钥
字串7

   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)

字串8


  {
   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(); 字串1

    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 字串3
     {
      ;
     }
    }
   }
   else
   {
    throw new FileNotFoundException("没有找到指定的文件");
   }
  }
  /// <summary>
  /// 文件加密函数的重载版本,如果不指定输出路径,
  /// 那么原来的文件将被加密后的文件覆盖
  /// </summary>
  /// <param name="filePath"></param>
  public void EncryptFile(string filePath)
  {
   this.EncryptFile(filePath,filePath);
  }

字串3


共2页: 上一页 1 [2] 下一页
Tags:技术应用 加密 /// new byte 文件 public filePath
来源:
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为