熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

哈希(不可逆)加密通用類庫函數

2022-06-13   來源: Java核心技術 

  using System;
  using SystemIO;
  using SystemSecurityCryptography;
  
  namespace Common
  {
  /// <summary>
  ///Copyright (C) kwklover(邝偉科)
  ///File name:Hashercs
  ///Author:邝偉科 Version: Date:
  ///Description:哈希(不可逆)加密通用類庫函數
  /// </summary>
  public class Hasher
  {
  private byte[] _HashKey; //哈希密鑰存儲變量
  private string _HashText; //待加密的字符串
  public Hasher()
  {
  //
  // TODO: 在此處添加構造函數邏輯
  //
  }
  
  /// <summary>
  /// 哈希密鑰
  /// </summary>
  public byte[] HashKey
  {
  set
  {
  _HashKey=value;
  }
  get
  {
  return _HashKey;
  }
  }
  
  /// <summary>
  /// 需要產生加密哈希的字符串
  /// </summary>
  public string HashText
  {
  set
  {
  _HashText=value;
  }
  get
  {
  return _HashText;
  }
  }
  
  /// <summary>
  /// 使用HMACSHA類產生長度為 字節的哈希序列需提供相應的密鑰接受任何大小的密鑰
  /// </summary>
  /// <returns></returns>
  public string HMACSHAHasher()
  {
  byte[] HmacKey=HashKey;
  byte[] HmacData=SystemTextEncodingUTFGetBytes(HashText);
  
  HMACSHA Hmac = new HMACSHA(HmacKey);
  
  CryptoStream cs = new CryptoStream(StreamNull Hmac CryptoStreamModeWrite);
  csWrite(HmacData HmacDataLength);
  csClose();
  
  byte[] Result=HmacHash;
  
  return ConvertToBaseString(Result); //返回長度為字節字符串
  }
  
  /// <summary>
  /// 使用MACTripleDES類產生長度為 字節的哈希序列需提供相應的密鑰密鑰長度可為 字節的密鑰
  /// </summary>
  /// <returns></returns>
  public string MACTripleDESHasher()
  {
  byte[] MacKey=HashKey;
  byte[] MacData=SystemTextEncodingUTFGetBytes(HashText);
  
  MACTripleDES Mac=new MACTripleDES(MacKey);
  
  byte[] Result=MacComputeHash(MacData);
  
  return ConvertToBaseString(Result); //返回長度為字節字符串
  }
  
  /// <summary>
  /// 使用MDCryptoServiceProvider類產生哈希值不需要提供密鑰
  /// </summary>
  /// <returns></returns>
  public string MDHasher()
  {
  byte[] MDData=SystemTextEncodingUTFGetBytes(HashText);
  
  MD Md=new MDCryptoServiceProvider();
  
  byte[] Result=MdComputeHash(MDData);
  
  return ConvertToBaseString(Result); //返回長度為字節字符串
  }
  
  /// <summary>
  /// 使用SHAManaged類產生長度為位哈希值不需要提供密鑰
  /// </summary>
  /// <returns></returns>
  public string SHAManagedHasher()
  {
  byte[] SHAData=SystemTextEncodingUTFGetBytes(HashText);
  
  SHAManaged Sha=new SHAManaged();
  
  byte[] Result=ShaComputeHash(SHAData);
  
  return ConvertToBaseString(Result); //返回長度為字節的字符串
  }
  
  /// <summary>
  /// 使用SHAManaged類產生長度為位哈希值不需要提供密鑰
  /// </summary>
  /// <returns></returns>
  public string SHAManagedHasher()
  {
  byte[] SHAData=SystemTextEncodingUTFGetBytes(HashText);
  
  SHAManaged Sha=new SHAManaged();
  
  byte[] Result=ShaComputeHash(SHAData);
  
  return ConvertToBaseString(Result); //返回長度為字節的字符串
  }
  
  /// <summary>
  /// 使用SHAManaged類產生長度為位哈希值不需要提供密鑰
  /// </summary>
  /// <returns></returns>
  public string SHAManagedHasher()
  {
  byte[] SHAData=SystemTextEncodingUTFGetBytes(HashText);
  
  SHAManaged Sha=new SHAManaged();
  
  byte[] Result=ShaComputeHash(SHAData);
  
  return ConvertToBaseString(Result); //返回長度為字節的字符串
  }
  
  /// <summary>
  /// 使用SHAManaged類產生長度為位哈希值不需要提供密鑰
  /// </summary>
  /// <returns></returns>
  public string SHAManagedHasher()
  {
  byte[] SHAData=SystemTextEncodingUTFGetBytes(HashText);
  
  SHAManaged Sha=new SHAManaged();
  
  byte[] Result=ShaComputeHash(SHAData);
  
  return ConvertToBaseString(Result); //返回長度為字節的字符串
  }
  }
  }
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26461.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.