一個基於.NET的加密/解密演算法

來源:互聯網
上載者:User
使用:SymmCrypto de = new SymmCrypto(SymmCrypto.SymmProvEnum.DES);
Response.Write(x.Decrypting(de.Encrypting("ok","yyy"),"yyy"));

C#類:using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace eMeng
{
  /**//// <summary>
  /// SymmCrypto 的摘要說明。
  /// SymmCrypto類實現.NET架構下的加密和解密服務。
  /// 原作者: Frank Fang : fangfrank@hotmail.com
  /// </summary>
  public class SymmCrypto
  {
      public enum SymmProvEnum : int
      {
        DES, RC2, Rijndael
      }

    private SymmetricAlgorithm mobjCryptoService;

    /**//// <remarks>
    /// 使用.Net SymmetricAlgorithm 類的構造器.
    /// </remarks>
    public SymmCrypto(SymmProvEnum NetSelected)
    {
      switch (NetSelected)
      {
        case SymmProvEnum.DES:
          mobjCryptoService = new DESCryptoServiceProvider();
          break;
        case SymmProvEnum.RC2:
          mobjCryptoService = new RC2CryptoServiceProvider();
          break;
        case SymmProvEnum.Rijndael:
          mobjCryptoService = new RijndaelManaged();
          break;
      }
    }

    /**//// <remarks>
    /// 使用自訂SymmetricAlgorithm類的構造器.
    /// </remarks>
    public SymmCrypto(SymmetricAlgorithm ServiceProvider)
    {
      mobjCryptoService = ServiceProvider;
    }

    /**//// <remarks>
    /// Depending on the legal key size limitations of 
    /// a specific CryptoService provider and length of 
    /// the private key provided, padding the secret key 
    /// with space character to meet the legal size of the algorithm.
    /// </remarks>
    private byte[] GetLegalKey(string Key)
    {
      string sTemp;
      if (mobjCryptoService.LegalKeySizes.Length > 0)
      {
        int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
        // key sizes are in bits
        while (Key.Length * 8 > moreSize)
        {
          lessSize = moreSize;
          moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
        }
        sTemp = Key.PadRight(moreSize / 8, ' ');
      }
      else
        sTemp = Key;

      // convert the secret key to byte array
      return ASCIIEncoding.ASCII.GetBytes(sTemp);
    }

    public string Encrypting(string Source, string Key)
    {
      byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
      // create a MemoryStream so that the process can be done without I/O files
      System.IO.MemoryStream ms = new System.IO.MemoryStream();

      byte[] bytKey = GetLegalKey(Key);

      // set the private key
      mobjCryptoService.Key = bytKey;
      mobjCryptoService.IV = bytKey;

      // create an Encryptor from the Provider Service instance
      ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

      // create Crypto Stream that transforms a stream using the encryption
      CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

      // write out encrypted content into MemoryStream
      cs.Write(bytIn, 0, bytIn.Length);
      cs.FlushFinalBlock();
            
      // get the output and trim the '

sr:http://dotnet.aspx.cc/ShowDetail.aspx?id=1AF1179E-43E5-4D4F-61AE-84922B5CBA74

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.