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

别再手动管理AssetBundle了!用GameFramework的ResourceManager实现高效资源加载(附完整流程图解析) 本文深入解析GameFramework的ResourceManager如何通过任务池、对象池和引用计数等核心机制,实现高效的资源加载与管理。告别手动管理AssetBundle的痛点,提升Unity项目开发效率,避免内存泄漏和加载卡顿问题,适用于中大型游戏项目开发。 阅读详情

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);
  }
 }
}

.Net加密解密方法 asp.net自定义加密解密方法,可以根据用户自己设定的秘钥加密解密。 立即下载

相关推荐

C#(.net)的几种加密解密方式

这是C#的几种加密和解密的方法,里面包含Base64的加密和解密,MD5的加密,SHA1的加密和解密,文件夹的加密和解密

X-Bogus流程分析

X-Bogus流程分析

weixin_47115747的博客 8031

asp.net 加密类

整理了一个asp.net 加密类,地址传参也可以加密

.net中常用的几种加密解密

一、MD5加密算法    : MD5全称是message-digest algorithm 5,简单的说就是单向的加密,即是说无法根据密文推导出明文。     MD5主要用途:        1、对一段信息生成信息摘要,该摘要对该信息具有唯一性,可以作为数字签名。        2、用于验证文件的有效性(是否有丢失或损坏的数据),        3、对用户密码的加密,        4、

学习思考的家园 3105

C# .net MD5加密解密工具及加密解密类

C# .net MD5加密解密工具及加密解密类。使用方便,可直接下载加密解密代码在项目中使用。

.NET实现文本加密解密

.NET实现文本加密解密.NET实现文本加密解密.NET实现文本加密解密.NET实现文本加密解密.NET实现文本加密解密.NET实现文本加密解密.NET实现文本加密解密(Q群号:152088818 本群只为有经验的.NET开发者开放提供一个技术交流平)

C#.NET密码加密解密源码Demo

C#.NET密码加密解密Demo,代码完整,简单易懂,功能实用。 获取源码方式: 第一种:打开微信,搜一搜"别打我女儿的主意"打开微信小程序,找到菜单栏,点击源码,进去就可以获得链接 第二种:可以给本文点赞、好评,然后发邮件到792166417@qq.com,如果有学习资料视频可以分享的话,可以捎带分享给我。 DESEncrypt.cs: using System; using Sy...

IT_0802的博客 2239

【计算机视觉】pyrealsense2:Intel RealSense 深度相机的 Python 接口实战指南

pyrealsense2:Intel RealSense 深度相机的 Python 接口实战

weixin_43988131的博客 4352

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

加密类代码 /**//**********************Created by Chen***************************如果你觉得本人的文章好,要引用请尊重著作人的劳动果实,说明 *出处以及原创作者,Thank you!!!   email:aishen944-sohu.com**********************************************...

weixin_34370347的博客 84

VBA常见错误 - “错误3706,未找到提供程序,该程序可能未正确安装”解决方法

1、问题描述 在利用VBA编程时,引用其它表格数据,出现以下问题: (1)运行“错误3706,未找到提供程序,该程序可能未正确安装”。 (2)引用数据失败 2、解决方法 下载“Access 2010 数据库引擎”,并安装即可。 https://www.microsoft.com/en-us/download/details.aspx?id=54920 AccessDatabaseEngine 2010 官方安装包,官网下载不了的可以来这里下载。(1)运行“错误3706,未找到提..

Nemuel的博客 3万+

【低轨】Starlink星链-Ku波段OFDM信号同步机制与PNT应用潜力

本文深入解析了Starlink星链Ku波段OFDM信号的同步机制及其在定位、导航与授时(PNT)领域的应用潜力。研究发现,其信号帧内嵌的同步序列具有相位恒定特性,支持超长相干积分,可在极低信噪比下实现高精度伪距测量,为利用低轨卫星通信信号实现高精度PNT服务提供了新的技术路径。

weixin_29250403的博客 730
上一篇: 在Web DataGrid 中当鼠标移到某行与离开时行的颜色发生改变(结合javascript)
下一篇: 【关于ASP.NET在IIS一些问题的经验总结】
wang8712
博客等级 码龄23年 11粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值