C# 对称加密算法类代码
- using System.Text;
- using System.IO;
- using System;
- using System.Text.RegularExpressions;
- using System.Security.Cryptography;
- namespace LHCHIP.Common
- ...{
- /**//// <summary>
- /// 对称加密算法类
- /// </summary>
- public class DecryptEncrypt
- ...{
- /**//// <summary>
- /// 返回自身的一个类
- /// </summary>
- public static DecryptEncrypt MyDecryptEncrypt
- ...{
- get
- ...{
- return new DecryptEncrypt();
- }
- }
- private SymmetricAlgorithm mobjCryptoService;
- private string Key;
- /**//// <summary>
- /// 对称加密类的构造函数
- /// </summary>
- internal DecryptEncrypt()
- ...{
- mobjCryptoService = new RijndaelManaged();
- Key = "rrp(%&h70x89H$jgsfgfsI0456Ftma81&fvHrr&&76*h%(12lJ$lhj!y6&(*jkPer44a";
- }
- /**//// <summary>
- /// 获得密钥
- /// </summary>
- /// <returns>密钥</returns>
- private byte[] GetLegalKey()
- ...{
- string _TempKey = Key;
- mobjCryptoService.GenerateKey();
- byte[] bytTemp = mobjCryptoService.Key;
- int KeyLength = bytTemp.Length;
- if (_TempKey.Length > KeyLength)
- _TempKey = _TempKey.Substring(0, KeyLength);
- else if (_TempKey.Length < KeyLength)
- _TempKey = _TempKey.PadRight(KeyLength, ’ ’);
- return ASCIIEncoding.ASCII.GetBytes(_TempKey);
- }
- /**//// <summary>
- /// 获得初始向量IV
- /// </summary>
- /// <returns>初试向量IV</returns>
- private byte[] GetLegalIV()
- ...{
- string _TempIV = "@afetj*Ghg7!rNIfsgr95GUqd9gsrb#GG7HBh(urjj6HJ($jhWk7&!hjjri%$hjk";
- mobjCryptoService.GenerateIV();
- byte[] bytTemp = mobjCryptoService.IV;
- int IVLength = bytTemp.Length;
- if (_TempIV.Length > IVLength)
- _TempIV = _TempIV.Substring(0, IVLength);
- else if (_TempIV.Length < IVLength)
- _TempIV = _TempIV.PadRight(IVLength, ’ ’);
- return ASCIIEncoding.ASCII.GetBytes(_TempIV);
- }
- /**//// <summary>
- /// 加密方法
- /// </summary>
- /// <param name="Source">待加密的串</param>
- /// <returns>经过加密的串</returns>
- public string Encrypto(string Source)
- ...{
- byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
- MemoryStream ms = new MemoryStream();
- mobjCryptoService.Key = GetLegalKey();
- mobjCryptoService.IV = GetLegalIV();
- //创建对称加密器对象
- ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
- //定义将数据流链接到加密转换的流
- CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
- cs.Write(bytIn, 0, bytIn.Length);
- cs.FlushFinalBlock();
- ms.Close();
- byte[] bytOut = ms.ToArray();
- return Convert.ToBase64String(bytOut);
- }
- &
补充:综合编程 , 安全编程 ,
上一个:编写非安全C#代码例子
下一个:PHP开发安全详解