作者:佳臭臭_643 | 来源:互联网 | 2023-09-02 13:56
首先有一个枚举用来判断用什么那种MD5加密
/// /// MD5 加密位数/// public enum MD5_Type{/// /// 32位MD5算法/// MD5_32BIT,/// /// 64位MD5算法/// MD5_64BIT}
这里是MD5加密算法
/// /// 加解密工具包/// public sealed class MD5_Cryptography{private const int BITS_TO_A_BYTE = 8;private const int BYTES_TO_A_WORD = 4;private const int BITS_TO_A_WORD = 32;private static long[] m_lOnBits = new long[30 + 1];private static long[] m_l2Power = new long[30 + 1];#region --- MD5 标准加密 32Bit加密 64Bit加密 Begin ---/// /// 标准MD5加密/// 应用于密码相关加密/// /// 需要加密的字符串/// 返回加密后的字符串/// /// /// ///
/// public static string MD5Encrypt(string str){MD5 md5 &#61; new MD5CryptoServiceProvider();//将字符编码为一个字节序列byte[] data &#61; System.Text.Encoding.Default.GetBytes(str);//计算data字节数组的哈希值 byte[] md5data &#61; md5.ComputeHash(data);md5.Clear();string temp &#61; string.Empty;for (int i &#61; 0; i /// 32位 或者 64位 MD5加密/// 应用于密码相关加密 /// /// 需要加密的字符串/// 加密类型/// 返回加密后的字符串public static string MD5Encrypt(string str, MD5_Type type){string MD5 &#61; "";for (int i &#61; 0; i <&#61; 30; i&#43;&#43;){m_lOnBits[i] &#61; Convert.ToInt64(Math.Pow(2, i &#43; 1) - 1);m_l2Power[i] &#61; Convert.ToInt64(Math.Pow(2, i));}long[] x &#61; null;int k &#61; 0;long AA &#61; 0;long BB &#61; 0;long CC &#61; 0;long DD &#61; 0;long a &#61; 0;long b &#61; 0;long c &#61; 0;long d &#61; 0;const int S11 &#61; 7;const int S12 &#61; 12;const int S13 &#61; 17;const int S14 &#61; 22;const int S21 &#61; 5;const int S22 &#61; 9;const int S23 &#61; 14;const int S24 &#61; 20;const int S31 &#61; 4;const int S32 &#61; 11;const int S33 &#61; 16;const int S34 &#61; 23;const int S41 &#61; 6;const int S42 &#61; 10;const int S43 &#61; 15;const int S44 &#61; 21;x &#61; convertToWordArray(str);a &#61; 0x67452301;b &#61; 0xEFCDAB89;c &#61; 0x98BADCFE;d &#61; 0x10325476;for (k &#61; 0; k 31){// Err.Raise 6;}}}if (Convert.ToBoolean((lValue & m_l2Power[31 - iShiftBits]))){LShift &#61; ((lValue & m_lOnBits[31 - (iShiftBits &#43; 1)]) * m_l2Power[iShiftBits]) | 0x80000000;}else{LShift &#61; ((lValue & m_lOnBits[31 - iShiftBits]) * m_l2Power[iShiftBits]);}return LShift;}private static long rShift(long lValue, long iShiftBits){long RShift &#61; 0;if (iShiftBits &#61;&#61; 0){RShift &#61; lValue;return RShift;}else{if (iShiftBits &#61;&#61; 31){if (Convert.ToBoolean(lValue & 0x80000000)){RShift &#61; 1;}else{RShift &#61; 0;}return RShift;}else{if (iShiftBits <0 || iShiftBits > 31){// Err.Raise 6;}}}RShift &#61; (lValue & 0x7FFFFFFE) / m_l2Power[iShiftBits];if (Convert.ToBoolean((lValue & 0x80000000))){RShift &#61; (RShift | (0x40000000 / m_l2Power[iShiftBits - 1]));}return RShift;}private static long rotateLeft(long lValue, long iShiftBits){long RotateLeft &#61; 0;RotateLeft &#61; lShift(lValue, iShiftBits) | rShift(lValue, (32 - iShiftBits));return RotateLeft;}private static long addUnsigned(long lX, long lY){long AddUnsigned &#61; 0;long lX4 &#61; 0;long lY4 &#61; 0;long lX8 &#61; 0;long lY8 &#61; 0;long lResult &#61; 0;lX8 &#61; lX & 0x80000000;lY8 &#61; lY & 0x80000000;lX4 &#61; lX & 0x40000000;lY4 &#61; lY & 0x40000000;lResult &#61; (lX & 0x3FFFFFFF) &#43; (lY & 0x3FFFFFFF);if (Convert.ToBoolean(lX4 & lY4)){lResult &#61; lResult ^ 0x80000000 ^ lX8 ^ lY8;}else if (Convert.ToBoolean(lX4 | lY4)){if (Convert.ToBoolean(lResult & 0x40000000)){lResult &#61; lResult ^ 0xC0000000 ^ lX8 ^ lY8;}else{lResult &#61; lResult ^ 0x40000000 ^ lX8 ^ lY8;}}else{lResult &#61; lResult ^ lX8 ^ lY8;}AddUnsigned &#61; lResult;return AddUnsigned;}private static long md5_F(long x, long y, long z){long md5_F &#61; 0;md5_F &#61; (x & y) | ((~x) & z);return md5_F;}private static long md5_G(long x, long y, long z){long md5_G &#61; 0;md5_G &#61; (x & z) | (y & (~z));return md5_G;}private static long md5_H(long x, long y, long z){long md5_H &#61; 0;md5_H &#61; (x ^ y ^ z);return md5_H;}private static long md5_I(long x, long y, long z){long md5_I &#61; 0;md5_I &#61; (y ^ (x | (~z)));return md5_I;}private static void md5_FF(ref long a, long b, long c, long d, long x, long s, long ac){a &#61; addUnsigned(a, addUnsigned(addUnsigned(md5_F(b, c, d), x), ac));a &#61; rotateLeft(a, s);a &#61; addUnsigned(a, b);}private static void md5_GG(ref long a, long b, long c, long d, long x, long s, long ac){a &#61; addUnsigned(a, addUnsigned(addUnsigned(md5_G(b, c, d), x), ac));a &#61; rotateLeft(a, s);a &#61; addUnsigned(a, b);}private static void md5_HH(ref long a, long b, long c, long d, long x, long s, long ac){a &#61; addUnsigned(a, addUnsigned(addUnsigned(md5_H(b, c, d), x), ac));a &#61; rotateLeft(a, s);a &#61; addUnsigned(a, b);}private static void md5_II(ref long a, long b, long c, long d, long x, long s, long ac){a &#61; addUnsigned(a, addUnsigned(addUnsigned(md5_I(b, c, d), x), ac));a &#61; rotateLeft(a, s);a &#61; addUnsigned(a, b);}private static long[] convertToWordArray(string sMessage){long[] ConvertToWordArray &#61; null;int lMessageLength &#61; 0;int lNumberOfWords &#61; 0;long[] lWordArray &#61; null;int lBytePosition &#61; 0;int lByteCount &#61; 0;int lWordCount &#61; 0;const int MODULUS_BITS &#61; 512;const int CONGRUENT_BITS &#61; 448;lMessageLength &#61; sMessage.Length;lNumberOfWords &#61; (((lMessageLength &#43; ((MODULUS_BITS - CONGRUENT_BITS) / BITS_TO_A_BYTE)) / (MODULUS_BITS / BITS_TO_A_BYTE)) &#43; 1) * (MODULUS_BITS / BITS_TO_A_WORD);lWordArray &#61; new long[lNumberOfWords];lBytePosition &#61; 0;lByteCount &#61; 0;while (lByteCount 0){strhex &#61; tohex(dec % 16) &#43; strhex;dec &#61; dec / 16;}return strhex;}private static string tohex(long hex){string strhex &#61; "";switch (hex){case 10: strhex &#61; "a"; break;case 11: strhex &#61; "b"; break;case 12: strhex &#61; "c"; break;case 13: strhex &#61; "d"; break;case 14: strhex &#61; "e"; break;case 15: strhex &#61; "f"; break;default: strhex &#61; hex.ToString(); break;}return strhex;}#endregion --- MD5 加密算法 End ---#endregion --- MD5 标准加密 32Bit 64Bit End ---}
其中 public static string MD5Encrypt(string str) 函数是ASP.NET 标准的MD5算法 返回30位的加密后的字符串
另外 public static string MD5Encrypt(string str, MD5_Type type) 通过 32位 或者64位的MD5加密算法