本文實例講述了C#獲取哈希加密生成隨機安全碼的類。分享給大家供大家參考。具體分析如下:
這個C#類封裝了一些hash加密的功能,可以用于得到隨機哈希加密字符串使用非常方便
using System;using System.Text;using System.Security.Cryptography;namespace DotNet.Utilities{ /// <summary> /// 得到隨機安全碼(哈希加密)。 /// </summary> public class HashEncode { public HashEncode() { // // TODO: 在此處添加構造函數邏輯 // } /// <summary> /// 得到隨機哈希加密字符串 /// </summary> /// <returns></returns> public static string GetSecurity() { string Security = HashEncoding(GetRandomValue()); return Security; } /// <summary> /// 得到一個隨機數值 /// </summary> /// <returns></returns> public static string GetRandomValue() { Random Seed = new Random(); string RandomVaule = Seed.Next(1, int.MaxValue).ToString(); return RandomVaule; } /// <summary> /// 哈希加密一個字符串,sharejs.com /// </summary> /// <param name="Security"></param> /// <returns></returns> public static string HashEncoding(string Security) { byte[] Value; UnicodeEncoding Code = new UnicodeEncoding(); byte[] Message = Code.GetBytes(Security); SHA512Managed Arithmetic = new SHA512Managed(); Value = Arithmetic.ComputeHash(Message); Security = ""; foreach(byte o in Value) { Security += (int) o + "O"; } return Security; } }}
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答