当前位置:编程学习 > 网站相关 >>

C# 对称加密算法类代码

   
  1. using System.Text;
  2.     using System.IO;
  3.     using System;
  4.     using System.Text.RegularExpressions;
  5.     using System.Security.Cryptography;
  6.     namespace LHCHIP.Common
  7.     ...{
  8.         /**//// <summary>
  9.         /// 对称加密算法类
  10.         /// </summary>
  11.         public class DecryptEncrypt
  12.         ...{
  13.             /**//// <summary>
  14.             /// 返回自身的一个类
  15.             /// </summary>
  16.             public static DecryptEncrypt MyDecryptEncrypt
  17.             ...{
  18.                 get
  19.                 ...{
  20.                     return new DecryptEncrypt();
  21.                 }
  22.             }
  23.         private SymmetricAlgorithm mobjCryptoService;
  24.         private string Key;
  25.         /**//// <summary>
  26.         /// 对称加密类的构造函数
  27.         /// </summary>
  28.         internal DecryptEncrypt()
  29.         ...{
  30.             mobjCryptoService = new RijndaelManaged();
  31.             Key = "rrp(%&h70x89H$jgsfgfsI0456Ftma81&fvHrr&&76*h%(12lJ$lhj!y6&(*jkPer44a";
  32.         }
  33.         /**//// <summary>
  34.         /// 获得密钥
  35.         /// </summary>
  36.         /// <returns>密钥</returns>
  37.         private byte[] GetLegalKey()
  38.         ...{
  39.             string _TempKey = Key;
  40.             mobjCryptoService.GenerateKey();
  41.             byte[] bytTemp = mobjCryptoService.Key;
  42.             int KeyLength = bytTemp.Length;
  43.             if (_TempKey.Length > KeyLength)
  44.            
  45.          _TempKey = _TempKey.Substring(0, KeyLength);
  46.             else if (_TempKey.Length < KeyLength)
  47.                 _TempKey = _TempKey.PadRight(KeyLength, ’ ’);
  48.             return ASCIIEncoding.ASCII.GetBytes(_TempKey);
  49.         }
  50.         /**//// <summary>
  51.         /// 获得初始向量IV
  52.         /// </summary>
  53.         /// <returns>初试向量IV</returns>
  54.         private byte[] GetLegalIV()
  55.         ...{
  56.             string _TempIV = "@afetj*Ghg7!rNIfsgr95GUqd9gsrb#GG7HBh(urjj6HJ($jhWk7&!hjjri%$hjk";
  57.             mobjCryptoService.GenerateIV();
  58.             byte[] bytTemp = mobjCryptoService.IV;
  59.             int IVLength = bytTemp.Length;
  60.             if (_TempIV.Length > IVLength)
  61.                 _TempIV = _TempIV.Substring(0, IVLength);
  62.             else if (_TempIV.Length < IVLength)
  63.                 _TempIV = _TempIV.PadRight(IVLength, ’ ’);
  64.             return ASCIIEncoding.ASCII.GetBytes(_TempIV);
  65.         }
  66.         /**//// <summary>
  67.         /// 加密方法
  68.         /// </summary>
  69.         /// <param name="Source">待加密的串</param>
  70.         /// <returns>经过加密的串</returns>
  71.         public string Encrypto(string Source)
  72.         ...{
  73.             byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
  74.             MemoryStream ms = new MemoryStream();
  75.             mobjCryptoService.Key = GetLegalKey();
  76.             mobjCryptoService.IV = GetLegalIV();
  77.             //创建对称加密器对象
  78.         ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
  79.             //定义将数据流链接到加密转换的流
  80.             CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
  81.             cs.Write(bytIn, 0, bytIn.Length);
  82.             cs.FlushFinalBlock();
  83.             ms.Close();
  84.             byte[] bytOut = ms.ToArray();
  85.             return Convert.ToBase64String(bytOut);
  86.         }
  87.    &
补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,