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

导读:
  *如果你觉得本人的文章好,要引用请尊重著作人的劳动果实,说明
  *出处以及原创作者,Thank you!!! email:aishen944-sohu.com
  *******************************************************************/
  using System;
  using System.Text;
  using System.Security;
  using System.Security.Cryptography;
  using System.IO;
  namespace EncryptClasses
  {
  ///
  /// 此处定义的是DES加密,为了便于今后的管理和维护
  /// 请不要随便改动密码,或者改变了密码后请一定要
  /// 牢记先前的密码,否则将会照成不可预料的损失
  ///

  public class DESEncrypt
  {
  #region "member fields"
  private string iv="12345678";
  private string key="12345678";
  private Encoding encoding=new UnicodeEncoding();
  private DES des;
  #endregion
  ///
  /// 构造函数
  ///

  public DESEncrypt()
  {
  des=new DESCryptoServiceProvider();
  }
  #region "propertys"
  ///
  /// 设置加密密钥
  ///

  public string EncryptKey
  {
  get{return this.key;}
  set
  {
  this.key=value;
  }
  }
  ///
  /// 要加密字符的编码模式
  ///

  public Encoding EncodingMode
  {
  get{return this.encoding;}
  set{this.encoding=value;}
  }
  #endregion
  #region "methods"
  ///
  /// 加密字符串并返回加密后的结果
  ///

  ///
  ///
  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);
  }
  ///
  /// 加密指定的文件,如果成功返回True,否则false
  ///

  /// 要加密的文件路径
  /// 加密后的文件输出路径
  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("没有找到指定的文件");
  }
  }
  ///
  /// 文件加密函数的重载版本,如果不指定输出路径,
  /// 那么原来的文件将被加密后的文件覆盖
  ///

  ///
  public void EncryptFile(string filePath)
  {
  this.EncryptFile(filePath,filePath);
  }
  ///
  /// 解密给定的字符串
  ///

  /// 要解密的字符
  ///
  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);
  }
  ///
  /// 解密指定的文件
  ///

  /// 要解密的文件路径
  /// 解密后的文件输出路径
  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("指定的解密文件没有找到");
  }
  }
  ///
  /// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
  /// 则解密后的文件将覆盖先前的文件
  ///

  ///
  public void DecryptFile(string filePath)
  {
  this.DecryptFile(filePath,filePath);
  }
  #endregion
  }
  ///
  /// MD5加密类,注意经MD5加密过的信息是不能转换回原始数据的
  /// ,请不要在用户敏感的信息中使用此加密技术,比如用户的密码,
  /// 请尽量使用对称加密
  ///

  public class MD5Encrypt
  {
  private MD5 md5;
  public MD5Encrypt()
  {
  md5=new MD5CryptoServiceProvider();
  }
  ///
  /// 从字符串中获取散列值
  ///

  /// 要计算散列值的字符串
  ///
  public string GetMD5FromString(string str)
  {
  byte[] toCompute=Encoding.Unicode.GetBytes(str);
  byte[] hashed=md5.ComputeHash(toCompute,0,toCompute.Length);
  return Encoding.ASCII.GetString(hashed);
  }
  ///
  /// 根据文件来计算散列值
  ///

  /// 要计算散列值的文件路径
  ///
  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("指定的文件没有找到");
  }
  }
  }
  ///
  /// 用于数字签名的hash类
  ///

  public class MACTripleDESEncrypt
  {
  private MACTripleDES mact;
  private string __key="ksn168ch";
  private byte[] __data=null;
  public MACTripleDESEncrypt()
  {
  mact=new MACTripleDES();
  }
  ///
  /// 获取或设置用于数字签名的密钥
  ///

  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;
  }
  }
  ///
  /// 获取或设置用于数字签名的用户数据
  ///

  public byte[] Data
  {
  get{return this.__data;}
  set{this.__data=value;}
  }
  ///
  /// 得到签名后的hash值
  ///

  ///
  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);
  }
  }
  }
  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=381361

本文转自
http://blog.csdn.net/xyjnzy/archive/2005/05/26/381361.aspx
以下是对提供的参考资料的总结,按照要求结构化多个要点分条输出: 4G/5G无线网络优化与网规案例分析: NSA站点下终端掉4G问题:部分用户反馈NSA终端频繁掉4G,主要因终端主动发起SCGfail导致。分析显示,在信号较好的环境下,终端可能因节能、过热保护等原因主动释放连接。解决方案建议终端侧进行分析处理,尝试关闭节电开关等。 RSSI算法识别天馈遮挡:通过计算RSSI平均值及差值识别天馈遮挡,差值大于3dB则认定有遮挡。不同设备分组规则不同,如64T和32T。此方法可有效帮助现场人员识别因环境变化引起的网络问题。 5G 160M组网小区CA不生效:某5G站点开启100M+60M CA功能后,测试发现UE无法正常使用CA功能。问题原因在于CA频点集标识配置错误,修正后测试正常。 5G网络优化与策略: CCE映射方式优化:针对诺基亚站点覆盖农村区域,通过优化CCE资源映射方式(交织、非交织),提升RRC连接建立成功率和无线接通率。非交织方式相比交织方式有显著提升。 5G AAU两扇区组网:与三扇区组网相比,AAU两扇区组网在RSRP、SINR、下载速率和上传速率上表现不同,需根据具体场景选择适合的组网方式。 5G语音解决方案:包括沿用4G语音解决方案、EPS Fallback方案和VoNR方案。不同方案适用于不同的5G组网策略,如NSA和SA,并影响语音连续性和网络覆盖。 4G网络优化与资源利用: 4G室分设备利旧:面对4G网络投资压减与资源需求矛盾,提出利旧多维度调优策略,包括资源整合、统筹调配既有资源,以满足新增需求和提质增效。 宏站RRU设备1托N射灯:针对5G深度覆盖需求,研究使用宏站AAU结合1托N射灯方案,快速便捷地开通5G站点,提升深度覆盖能力。 基站与流程管理: 爱立信LTE基站邻区添加流程:未提供具体内容,但通常涉及邻区规划、参数配置、测试验证等步骤,以确保基站间顺畅切换和覆盖连续性。 网络规划与策略: 新高铁跨海大桥覆盖方案试点:虽未提供详细内容,但可推测涉及高铁跨海大桥区域的4G/5G网络覆盖规划,需考虑信号穿透、移动性管理、网络容量等因素。 总结: 提供的参考资料涵盖了4G/5G无线网络优化、网规案例分析、网络优化策略、资源利用、基站管理等多个方面。 通过具体案例分析,展示了无线网络优化中的常见问题及解决方案,如NSA终端掉4G、RSSI识别天馈遮挡、CA不生效等。 强调了5G网络优化与策略的重要性,包括CCE映射方式优化、5G语音解决方案、AAU扇区组网选择等。 提出了4G网络优化与资源利用的策略,如室分设备利旧、宏站RRU设备1托N射灯等。 基站与流程管理方面,提到了爱立信LTE基站邻区添加流程,但未给出具体细节。 新高铁跨海大桥覆盖方案试点展示了特殊场景下的网络规划需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值