C# 对称算法,加密解密类

  • 来源: 华军 作者: 若水   2008-04-23/10:19
  • 如果模块有修改,则每次修改添加以下注释:
    Log编号:
    修改作者:
    修改日期:
    修改描述:
    **************************************************************************/
    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();                
                }
            } 


          

    _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(); #p#分页标题#e#
                mobjCryptoService.IV = GetLegalIV();
                //创建对称加密器对象

      

    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)


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多