public void MD5Update(ref MDcontext MD, byte[] inBuf, uint inLen) { uint[] _in = new uint[16]; int mdi = (int)((MD.i[0] >> 3) & 0x3F); uint j; if ((MD.i[0] + ((uint)inLen << 3)) < MD.i[0]) { MD.i[1]++; } MD.i[0] += ((uint)inLen << 3); MD.i[1] += ((uint)inLen >> 29); j = 0; while (Convert.ToBoolean(inLen--)) { MD._in[mdi++] = inBuf[j++]; if (mdi == 0x40) { for (int i = 0, ii = 0; i < 16; i++, ii += 4) { _in[i] = (((uint)MD._in[ii + 3]) << 24) | (((uint)MD._in[ii + 2]) << 16) | (((uint)MD._in[ii + 1]) << 8) | ((uint)MD._in[ii]); } Transform(ref MD.buf, _in); mdi = 0; } } }
public void MD5Final(ref MDcontext MD) { uint[] _in = new uint[16]; int mdi = (int)((MD.i[0] >> 3) & 0x3F); uint padLen; _in[14] = MD.i[0]; _in[15] = MD.i[1]; padLen = (uint)((mdi < 56) ? (56 - mdi) : (120 - mdi)); MD5Update(ref MD, PADDING, padLen); for (uint i = 0, ii = 0; i < 14; i++, ii += 4) { _in[i] = (((uint)MD._in[ii + 3]) << 24) | (((uint)MD._in[ii + 2]) << 16) | (((uint)MD._in[ii + 1]) << 8) | ((uint)MD._in[ii]); } Transform(ref MD.buf, _in); for (uint i = 0, ii = 0; i < 4; i++, ii += 4) { MD.digest[ii] = (byte)(MD.buf[i] & 0xFF); MD.digest[ii + 1] = (byte)((MD.buf[i] >> 8) & 0xFF); MD.digest[ii + 2] = (byte)((MD.buf[i] >> 16) & 0xFF); MD.digest[ii + 3] = (byte)((MD.buf[i] >> 24) & 0xFF); } }
public void MD5Init(ref MDcontext MD) { MD.i[0] = MD.i[0] = (uint)0; MD.buf[0] = (uint)0x67452301; MD.buf[1] = (uint)0xefcdab89; MD.buf[2] = (uint)0x98badcfe; MD.buf[3] = (uint)0x10325476; }
/// <summary>This method encrypt the given string, using MD5 Algorithm</summary> /// <param name="s">The string to be encrypted.</param> /// <returns>The encrypted password.</returns> public string Encrypt(string s) { MDcontext MDp = new MDcontext(); MD5Init(ref MDp); MD5Update(ref MDp, (new ASCIIEncoding()).GetBytes(s), (uint)s.Length); MD5Final(ref MDp); return(MDToString(MDp.digest)); }
/// <summary>This method encrypt the given string with the salt indicated, using MD5 Algorithm</summary> /// <param name="pwd">The password to be encrypted.</param> /// <param name="salt"> The salt.</param> /// <returns>The encrypted password.</returns> public string Encrypt(string pwd, string salt) { string e_pass = ""; MDcontext MDp1 = new MDcontext(); MDcontext MDp2 = new MDcontext(); string magic = "$1$"; if (salt.Length > 19) { salt = salt.Substring(0, 19); } MD5Init(ref MDp1); MD5Update(ref MDp1, (new ASCIIEncoding()).GetBytes(pwd), (uint)pwd.Length); MD5Update(ref MDp1, (new ASCIIEncoding()).GetBytes(magic), (uint)magic.Length); MD5Update(ref MDp1, (new ASCIIEncoding()).GetBytes(salt), (uint)salt.Length); MD5Init(ref MDp2); MD5Update(ref MDp2, (new ASCIIEncoding()).GetBytes(pwd), (uint)pwd.Length); MD5Update(ref MDp2, (new ASCIIEncoding()).GetBytes(salt), (uint)salt.Length); MD5Update(ref MDp2, (new ASCIIEncoding()).GetBytes(pwd), (uint)pwd.Length); MD5Final(ref MDp2); for (int j = 0; j < 16; j++) { MDp1.digest[j] = MDp2.digest[j]; } for (int pl = pwd.Length; pl > 0; pl -= 16) { MD5Update(ref MDp1, MDp1.digest, (uint)((pl > 16) ? 16 : pl)); } for (int i = 0; i < 16; i++) { MDp1.digest[i] = 0x00; } for (int i = pwd.Length; Convert.ToBoolean(i); i >>= 1) { if (Convert.ToBoolean(i & 1)) { MD5Update(ref MDp1, MDp1.digest, 1); } else { MD5Update(ref MDp1, (new ASCIIEncoding()).GetBytes(pwd), 1); } } MD5Final(ref MDp1); for (int i = 0; i < 1000; i++) { MD5Init(ref MDp2); if (Convert.ToBoolean(i & 1)) { MD5Update(ref MDp2, (new ASCIIEncoding()).GetBytes(pwd), (uint)pwd.Length); } else { MD5Update(ref MDp2, MDp1.digest, 16); } if (Convert.ToBoolean(i % 3)) { MD5Update(ref MDp2, (new ASCIIEncoding()).GetBytes(salt), (uint)salt.Length); } if (Convert.ToBoolean(i % 7)) { MD5Update(ref MDp2, (new ASCIIEncoding()).GetBytes(pwd), (uint)pwd.Length); } if (Convert.ToBoolean(i & 1)) { MD5Update(ref MDp2, MDp1.digest, 16); } else { MD5Update(ref MDp2, (new ASCIIEncoding()).GetBytes(pwd), (uint)pwd.Length); } MD5Final(ref MDp2); for (int j = 0; j < 16; j++) { MDp1.digest[j] = MDp2.digest[j]; } } e_pass += magic; e_pass += salt + '$'; ulong l; l = (ulong)((MDp1.digest[0] << 16) | (MDp1.digest[6] << 8) | MDp1.digest[12]); _crypt_to64(ref e_pass, l, 4); l = (ulong)((MDp1.digest[1] << 16) | (MDp1.digest[7] << 8) | MDp1.digest[13]); _crypt_to64(ref e_pass, l, 4); l = (ulong)((MDp1.digest[2] << 16) | (MDp1.digest[8] << 8) | MDp1.digest[14]); _crypt_to64(ref e_pass, l, 4); l = (ulong)((MDp1.digest[3] << 16) | (MDp1.digest[9] << 8) | MDp1.digest[15]); _crypt_to64(ref e_pass, l, 4); l = (ulong)((MDp1.digest[4] << 16) | (MDp1.digest[10] << 8) | MDp1.digest[5]); _crypt_to64(ref e_pass, l, 4); l = (ulong)MDp1.digest[11]; _crypt_to64(ref e_pass, l, 2); return(e_pass); }