熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C# 對稱算法,加密解密類

2022-06-13   來源: .NET編程 

  模塊編號
    包含
    作者
    審核
    編寫日期
    描述提供對稱加密摘要算法

  如果模塊有修改則每次修改添加以下注釋
    Log編號
    修改作者
    修改日期
    修改描述

  using SystemText;
    using SystemIO;
    using System;
    using SystemTextRegularExpressions;
    using SystemSecurityCryptography;

  namespace LHCHIPCommon
    {
        /**//// <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(%&hxH$jgsfgfsIFtma&fvHrr&&*h%(lJ$lhj!y&(*jkPera;
        }

  /**//// <summary>
        /// 獲得密鑰
        /// </summary>
        /// <returns>密鑰</returns>
        private byte[] GetLegalKey()
        {
            string _TempKey = Key;
            mobjCryptoServiceGenerateKey();
            byte[] bytTemp = mobjCryptoServiceKey;
            int KeyLength = bytTempLength;
            if (_TempKeyLength > KeyLength)
           
         _TempKey = _TempKeySubstring( KeyLength);
            else if (_TempKeyLength < KeyLength)
                _TempKey = _TempKeyPadRight(KeyLength );
            return ASCIIEncodingASCIIGetBytes(_TempKey);
        }
        /**//// <summary>
        /// 獲得初始向量IV
        /// </summary>
        /// <returns>初試向量IV</returns>
        private byte[] GetLegalIV()
        {
            string _TempIV = @afetj*Ghg!rNIfsgrGUqdgsrb#GGHBh(urjjHJ($jhWk&!hjjri%$hjk;
            mobjCryptoServiceGenerateIV();
            byte[] bytTemp = mobjCryptoServiceIV;
            int IVLength = bytTempLength;
            if (_TempIVLength > IVLength)
                _TempIV = _TempIVSubstring( IVLength);
            else if (_TempIVLength < IVLength)
                _TempIV = _TempIVPadRight(IVLength );
            return ASCIIEncodingASCIIGetBytes(_TempIV);
        }

  /**//// <summary>
        /// 加密方法
        /// </summary>
        /// <param name=Source>待加密的串</param>
        /// <returns>經過加密的串</returns>
        public string Encrypto(string Source)
        {
            byte[] bytIn = UTFEncodingUTFGetBytes(Source);
            MemoryStream ms = new MemoryStream();
            mobjCryptoServiceKey = GetLegalKey();
            mobjCryptoServiceIV = GetLegalIV();
            //創建對稱加密器對象
        ICryptoTransform encrypto = mobjCryptoServiceCreateEncryptor();
            //定義將數據流鏈接到加密轉換的流
            CryptoStream cs = new CryptoStream(ms encrypto CryptoStreamModeWrite);
            csWrite(bytIn bytInLength);
            csFlushFinalBlock();
            msClose();
            byte[] bytOut = msToArray();
            return ConvertToBaseString(bytOut);
        }

  /**//// <summary>
        /// 解密方法
        /// </summary>
        /// <param name=Source>待解密的串</param>
        /// <returns>經過解密的串</returns>
        public string Decrypto(string Source)
        {
            byte[] bytIn = ConvertFromBaseString(Source);
            MemoryStream ms = new MemoryStream(bytIn bytInLength);
            mobjCryptoServiceKey = GetLegalKey();
            mobjCryptoServiceIV = GetLegalIV();
            //創建對稱解密器對象
            ICryptoTransform encrypto = mobjCryptoServiceCreateDecryptor();
            //定義將數據流鏈接到加密轉換的流
            CryptoStream cs = new CryptoStream(ms encrypto CryptoStreamModeRead);
            StreamReader sr = new StreamReader(cs);
            return srReadToEnd();
            }

  }
    }


From:http://tw.wingwit.com/Article/program/net/201311/11770.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.