单向散列算法之MD4,C#版本,绝无仅有的一份

发布时间:2017-2-21 8:48:47 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"单向散列算法之MD4,C#版本,绝无仅有的一份",主要涉及到单向散列算法之MD4,C#版本,绝无仅有的一份方面的内容,对于单向散列算法之MD4,C#版本,绝无仅有的一份感兴趣的同学可以参考一下。

前天有人问我MD4算法怎么写,我在网站查了,确实没有C#版本的,我找到OPENSSL的开源代码,花了2个小时,从C语言修改成C#语言,希望能为C#开发的朋友做点贡献。 using System; /******************************************************************* *概要:md4编码,主要介绍md4加密过程 *作者:田景吉 *日期:2013.09.04 *联系:422909873,tianaiso@163.com *注解:MD4是一种单向散列加密算法, *摘要:它是一种用来测试信息完整性的密码散列函数的实行。其摘要长度为128位,一般128位长的MD4散列被表 *示为32位的十六进制数字。这个算法影响了后来的算法如MD5、SHA 家族和RIPEMD等。 *MD4("") = 31d6cfe0d16ae931b73c59d7e0c089c0 *******************************************************************/ namespace EDED { public class md4 { private UInt32 A; private UInt32 B; private UInt32 C; private UInt32 D; private UInt32 F(UInt32 X, UInt32 Y, UInt32 Z) { return (X & Y) | ((~X) & Z); } private UInt32 G(UInt32 X, UInt32 Y, UInt32 Z) { return (X & Y) | (X & Z) | (Y & Z); } private UInt32 H(UInt32 X, UInt32 Y, UInt32 Z) { return X ^ Y ^ Z; } private UInt32 lshift(UInt32 X, int s) { X &= 0xFFFFFFFF; return ((X << s) & 0xFFFFFFFF) | (X >> (32 - s)); } private void FF(ref UInt32 X, UInt32 Y, UInt32 Z, UInt32 O, UInt32 P, int S) { X = lshift(X + F(Y, Z, O) + P, S); } private void HH(ref UInt32 X, UInt32 Y, UInt32 Z, UInt32 O, UInt32 P, int S) { X = lshift(X + G(Y, Z, O) + P + (UInt32)0x5A827999, S); } private void GG(ref UInt32 X, UInt32 Y, UInt32 Z, UInt32 O, UInt32 P, int S) { X = lshift(X + H(Y, Z, O) + P + (UInt32)0x6ED9EBA1, S); } private void mdfour64(ref UInt32[] M) { int j; UInt32 AA, BB, CC, DD; UInt32[] X = new UInt32[16]; for (j = 0; j < 16; j++) { X[j] = M[j]; } AA = A; BB = B; CC = C; DD = D; FF(ref A, B, C, D, X[0], 3); FF(ref D, A, B, C, X[1], 7); FF(ref C, D, A, B, X[2], 11); FF(ref B, C, D, A, X[3], 19); FF(ref A, B, C, D, X[4], 3); FF(ref D, A, B, C, X[5], 7); FF(ref C, D, A, B, X[6], 11); FF(ref B, C, D, A, X[7], 19); FF(ref A, B, C, D, X[8], 3); FF(ref D, A, B, C, X[9], 7); FF(ref C, D, A, B, X[10], 11); FF(ref B, C, D, A, X[11], 19); FF(ref A, B, C, D, X[12], 3); FF(ref D, A, B, C, X[13], 7); FF(ref C, D, A, B, X[14], 11); FF(ref B, C, D, A, X[15], 19); HH(ref A, B, C, D, X[0], 3); HH(ref D, A, B, C, X[4], 5); HH(ref C, D, A, B, X[8], 9); HH(ref B, C, D, A, X[12], 13); HH(ref A, B, C, D, X[1], 3); HH(ref D, A, B, C, X[5], 5); HH(ref C, D, A, B, X[9], 9); HH(ref B, C, D, A, X[13], 13); HH(ref A, B, C, D, X[2], 3); HH(ref D, A, B, C, X[6], 5); HH(ref C, D, A, B, X[10], 9); HH(ref B, C, D, A, X[14], 13); HH(ref A, B, C, D, X[3], 3); HH(ref D, A, B, C, X[7], 5); HH(ref C, D, A, B, X[11], 9); HH(ref B, C, D, A, X[15], 13); GG(ref A, B, C, D, X[0], 3); GG(ref D, A, B, C, X[8], 9); GG(ref C, D, A, B, X[4], 11); GG(ref B, C, D, A, X[12], 15); GG(ref A, B, C, D, X[2], 3); GG(ref D, A, B, C, X[10], 9); GG(ref C, D, A, B, X[6], 11); GG(ref B, C, D, A, X[14], 15); GG(ref A, B, C, D, X[1], 3); GG(ref D, A, B, C, X[9], 9); GG(ref C, D, A, B, X[5], 11); GG(ref B, C, D, A, X[13], 15); GG(ref A, B, C, D, X[3], 3); GG(ref D, A, B, C, X[11], 9); GG(ref C, D, A, B, X[7], 11); GG(ref B, C, D, A, X[15], 15); A += AA; B += BB; C += CC; D += DD; A &= 0xFFFFFFFF; B &= 0xFFFFFFFF; C &= 0xFFFFFFFF; D &= 0xFFFFFFFF; for (j = 0; j < 16; j++) { X[j] = 0; } } private void copy64(ref UInt32[] M, byte[] input, int left) { int i; for (i = 0; i < 16; i++) { M[i] = (UInt32)((input[i * 4 + left + 3] << 24) | (input[i * 4 + left + 2] << 16) | (input[i * 4 + left + 1] << 8) | (input[i * 4 + left + 0] << 0)); } } private void copy4(ref byte[] output, int left, UInt32 x) { output[left + 0] = (byte)(x & 0xFF); output[left + 1] = (byte)((x >> 8) & 0xFF); output[left + 2] = (byte)((x >> 16) & 0xFF); output[left + 3] = (byte)((x >> 24) & 0xFF); } private byte[] mdfour(byte[] input, int n) { byte[] output = new byte[16]; byte[] buf = new byte[128]; UInt32[] M = new UInt32[16]; UInt32 b = (UInt32)(n * 8); int i; int j = 0; A = 0x67452301; B = 0xefcdab89; C = 0x98badcfe; D = 0x10325476; while (n > 64) { copy64(ref M, input, 0); mdfour64(ref M); j += 64; n -= 64; } for (i = 0; i < 128; i++) { buf[i] = 0; } for (i = 0; i < n; i++) { buf[i] = input[j + i]; } buf[n] = 0x80; if (n <= 55) { copy4(ref buf, 56, b); copy64(ref M, buf, 0); mdfour64(ref M); } else { copy4(ref buf, 120, b); copy64(ref M, buf, 0); mdfour64(ref M); copy64(ref M, buf, 64); mdfour64(ref M); } for (i = 0; i < 128; i++) { buf[i] = 0; } copy64(ref M, buf, 0); copy4(ref output, 0, A); copy4(ref output, 4, B); copy4(ref output, 8, C); copy4(ref output, 12, D); A = B = C = D = 0; return output; } /// <summary> /// Md4加密 /// </summary> /// <param name="str">输入字符串</param> /// <returns>加密的字符串</returns> public string encoder(string str) { return encoder(str, true, 32); } /// <summary> /// Md4加密 /// </summary> /// <param name="str">输入字符串</param> /// <param name="isupper">是否大写</param> /// <returns>加密的字符串</returns> public string encoder(string str, bool isupper) { return encoder(str, isupper, 32); } /// <summary> /// Md4加密 /// </summary> /// <param name="str">输入字符串</param> /// <param name="ishex">16、32位输出</param> /// <returns>加密的字符串</returns> public string encoder(string str, int ishex) { return encoder(str, true, ishex); } /// <summary> /// Md4加密 /// </summary> /// <param name="str">输入字符串</param> /// <param name="isupper">是否大写</param> /// <param name="ishex">16、32位输出</param> /// <returns>加密的字符串</returns> public string encoder(string str, bool isupper, int ishex) { string retstr = string.Empty; byte[] t = System.Text.Encoding.ASCII.GetBytes(str); byte[] temp = mdfour(t,t.Length); ; if (ishex == 16) { retstr = BitConverter.ToString(temp, 4, 8).Replace("-", ""); } if (ishex == 32) { retstr = BitConverter.ToString(temp).Replace("-", ""); } if (isupper) { retstr = retstr.ToUpper(); } else { retstr = retstr.ToLower(); } return retstr; } } }   测试程序: md4 m4 = new md4(); Console .WriteLine( m4.encoder("")); Console.WriteLine("31d6cfe0d16ae931b73c59d7e0c089c0"); Console.WriteLine(m4.encoder("a")); Console .WriteLine("bde52cb31de33e46245e05fbdbd6fb24"); Console .WriteLine (m4.encoder ("abc")); Console .WriteLine ("a448017aaf21d8525fc10ae87aa6729d"); Console .WriteLine (m4.encoder ("message digest")); Console .WriteLine ("d9130a8164549fe818874806e1c7014b"); Console.WriteLine(m4.encoder("abcdefghijklmnopqrstuvwxyz")); Console.WriteLine("d79e1c308aa5bbcdeea8ed63df412da9"); Console.WriteLine(m4.encoder("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")); Console.WriteLine("043f8582f241db351ce627e153e7f0e4"); Console.WriteLine(m4.encoder("12345678901234567890123456789012345678901234567890123456789012345678901234567890")); Console.WriteLine("e33b4ddc9c38f2199c3e7b164fcc0536");   输出结果与原版的MD4一样。

上一篇:LDR伪指令和LDR指令学习
下一篇:用java实现MP3播放器

相关文章

相关评论