VB implementation of 16-bit and 32-bit MD5 encryption code sharing _VB

Source: Internet
Author: User
Tags md5 md5 encryption

In various landing systems, the MD5 encryption of user passwords is essential.

Let's just say that even if you see the user's login password by all means, it is impossible to know what it is and that is protection.

MD5 encryption operation is irreversible, that is, you can not through the string of ancient strange things to calculate its original appearance.

The following is the complete MD5 code, do not understand the algorithm, use the time to take the direct call.

Private Const BITS_TO_A_BYTE = 8 Private Const BYTES_TO_A_WORD = 4 Private Const Bits_to_a_word = Private M_lonbits ( Private M_l2power () Private Function lshift (LValue, ishiftbits) If ishiftbits = 0 Then lshift = lValue Exit functi On ElseIf ishiftbits = to Then If lValue and 1 Then lshift = &h80000000 Else lshift = 0 End If Exit Function ElseIf is Hiftbits < 0 Or ishiftbits > Then err.raise 6 End if (LValue and M_l2power (31-ishiftbits)) Then LShift = ( (LValue and M_lonbits (ishiftbits + 1)) * M_l2power (ishiftbits)) Or &h80000000 Else lshift = ((LValue and M_LONB It (31-ishiftbits)) * M_l2power (ishiftbits)) End-If End Function Private function Rshift (lValue, ishiftbits) If Ishift
Bits = 0 Then rshift = lValue Exit Function ElseIf ishiftbits = Then If lValue and &h80000000 Then rshift = 1 Else Rshift = 0 End If Exit Function ElseIf ishiftbits < 0 Or ishiftbits > to Then err.raise 6 End If Rshift = (lValue and &h7ffffffe) \ M_L2power (ishiftbits) If (LValue and &h80000000) Then Rshift = (rshift Or &h40000000 (m_l2power)) ) End If End Function Private function Rotateleft (lValue, ishiftbits) Rotateleft = LShift (LValue, ishiftbits) Or Rshift ( 
 
LValue, (32-ishiftbits)) End Function Private Function addunsigned (LX, LY) Dim lX4 Dim LY4 Dim LX8 Dim LY8 Dim Lresult LX8 = lx and &h80000000 lY8 = ly and &h80000000 lX4 = lx and &h40000000 lY4 = ly and &h40000000 lresu lt = (LX and &AMP;H3FFFFFFF) + (LY and &AMP;H3FFFFFFF) If lX4 and lY4 Then lresult = Lresult xor &h80000000 xor lX8 XOR lY8 ElseIf lX4 Or lY4 Then If lresult and &h40000000 Then lresult = Lresult xor &hc0000000 xor LX8 xor lY8 E lresult = Lresult xor &h40000000 xor lX8 xor lY8 End If Else lresult = Lresult xor lX8 xor lY8 End If addunsigned = Lresult End Function Private function md5_f (x, Y, z) md5_f = (x and y) Or (not x) and Z) End Function Private funct Ion Md5_g (x, Y, z) md5_g = (x and z) Or (y and not z) End Function Private function md5_h (x, Y, z) md5_h = (x xor y xor z) End Function Priv Ate Function md5_i (x, Y, z) md5_i = (y Xor (x Or (not z)) end Function Private Sub md5_ff (A, B, C, D, X, S, ac) a = Add Unsigned (A, addunsigned (addunsigned (Md5_f (b, C, D), x), ac)) A = Rotateleft (A, s) A = Addunsigned (A, b) end Sub Private
Sub Md5_gg (A, B, C, D, X, S, ac) a = addunsigned (A, addunsigned (addunsigned (Md5_g (b, C, D), x), ac) A = Rotateleft (A, s) A = Addunsigned (A, b) End Sub Private Sub md5_hh (A, B, C, D, X, S, ac) a = addunsigned (A, addunsigned addunsigned (Md5_h ( b, C, D), x), ac)) A = Rotateleft (A, s) A = Addunsigned (A, b) End Sub Private Sub Md5_ii (A, B, C, D, X, S, ac) a = Addun Signed (A, addunsigned (addunsigned (md5_i (b, C, D), x), ac)) A = Rotateleft (A, s) A = Addunsigned (A, b) end Sub Private Fu Nction Converttowordarray (smessage) Dim lmessagelength Dim Lnumberofwords Dim Lwordarray () Dim lbyteposition Dim LByteCou NT Dim Lwordcount Const Modulus_bits = congruent_bits Const = 448 lmessagelength = Len (smessage) lnumberofwords = ((Lmessagelength + ((M 
odulus_bits-congruent_bits) \ Bits_to_a_byte) (modulus_bits \ Bits_to_a_byte)) + 1) * (modulus_bits \ BITS_TO_A_WORD) ReDim Lwordarray (lNumberOfWords-1) lbyteposition = 0 Lbytecount = 0 do Until lbytecount >= lmessagelength  NT = lbytecount \ Bytes_to_a_word lbyteposition = (lbytecount Mod bytes_to_a_word) * bits_to_a_byte LWordArray (lWordCount) 
 
= Lwordarray (Lwordcount) Or LShift (ASC (Mid (smessage, Lbytecount + 1, 1)), lbyteposition) Lbytecount = lbytecount + 1 Loop Lwordcount = lbytecount \ Bytes_to_a_word lbyteposition = (lbytecount Mod bytes_to_a_word) * Bits_to_a_byte LWordArra Y (Lwordcount) = Lwordarray (Lwordcount) Or lshift (&h80, lbyteposition) lwordarray (lNumberOfWords-2) = LShift (Lmessa
 
Gelength, 3) lwordarray (lNumberOfWords-1) = Rshift (Lmessagelength,) Converttowordarray = Lwordarray End Function Private Function WordtohEx (LValue) Dim lbyte Dim Lcount For lcount = 0 to 3 Lbyte = Rshift (LValue, Lcount * bits_to_a_byte) and m_lonbits (bits_t  o_a_byte-1) Wordtohex = Wordtohex & Right ("0" & Hex (Lbyte), 2) Next End Function Public function MD5 (smessage, stype) m_lonbits (0) = CLng (1) m_lonbits (1) = CLng (3) m_lonbits (2) = CLng (7) m_lonbits (3) = CLng (a) m_lonbits (4) = CLng (3 1) m_lonbits (5) = CLng (6) = CLng (127) m_lonbits (7) = CLng (255) m_lonbits (8) = CLng (511) m_lonbits (9) = CLng ( 1023) M_lonbits = CLng (2047) m_lonbits (one) = CLng (4095) m_lonbits (a) = CLng (8191) m_lonbits () = CLng (16383) M_lonbi  ts = CLng (32767) m_lonbits () = CLng (65535) m_lonbits (a) = CLng (131071) m_lonbits (m) = CLng (262143) m_lonbits (18) = CLng (524287) m_lonbits (a) = CLng (1048575) m_lonbits () = CLng (2097151) m_lonbits (a) = CLng (4194303) m_lonbits (=) = CL Ng (8388607) m_lonbits = CLng (16777215) m_lonbits (in) = CLng (33554431) m_lonbits (a) = CLng (67108863) m_lonbits (%) = C Lng (134217727) m_lonbits(m) = CLng (268435455) m_lonbits = CLng (536870911) m_lonbits (MB) = CLng (1073741823) m_lonbits (a) = CLng (2147483647) M_l2power (0) = CLng (1) m_l2power (1) = CLng (2) m_l2power (2) = CLng (4) m_l2power (3) = CLng (8) m_l2power (4) = CLng () m_l2p Ower (5) = CLng (+) M_l2power (6) = CLng ($) m_l2power (7) = CLng (128) m_l2power (8) = CLng (256) m_l2power (9) = CLng (a) m_l2 Power = CLng (1024) m_l2power (one) = CLng (2048) m_l2power (a) = CLng (4096) m_l2power (a) = CLng (8192) m_l2power (km) = CL Ng (16384) m_l2power = CLng (32768) m_l2power (a) = CLng (65536) m_l2power (km) = CLng (131072) m_l2power (a) = CLng (262144 ) M_l2power = CLng (524288) m_l2power (a) = CLng (1048576) m_l2power (a) = CLng (2097152) m_l2power (a) = CLng (4194304) m 
_l2power (g) = CLng (8388608) m_l2power (a) = CLng (16777216) m_l2power (a) = CLng (33554432) m_l2power (num) = CLng (67108864) M_l2power (m) = CLng (134217728) m_l2power (a) = CLng (268435456) m_l2power (MB) = CLng (536870912) m_l2power (a) = CLng (1073 741824) Dim x Dim K DIm AA Dim BB Dim CC Dim DD Dim A Dim B Dim c Dim D Const S11 = 7 Const S12 = Const S13 = Const S14 = Const S21 = 5 Const S22 = 9 Const S23 = S24 = Const S31 = 4 Const S32 = one Const S33 = Const S34 = S41 = 6 C  Onst S42 = Ten Const S43 = Const S44 = x = Converttowordarray (smessage) A = &h67452301 b = &hefcdab89 C =  &h98badcfe d = &h10325476 for k = 0 to UBound (x) Step AA = a BB = b CC = c DD = d md5_ff A, B, C, D, X (k + 
0), S11, &hd76aa478 md5_ff D, a, B, C, x (k + 1), S12, &he8c7b756 md5_ff C, D, a, B, X (k + 2), S13, &h242070db Md5_ff B, C, D, A, X (K + 3), S14, &hc1bdceee md5_ff A, B, C, D, X (K + 4), S11, &AMP;HF57C0FAF md5_ff D, a, B, C, X (k + 5), S12, &h4787c62a md5_ff C, D, a, B, X (k + 6), S13, &ha8304613 md5_ff B, C, D, A, X (k + 7), S14, &hfd46950  1 md5_ff A, B, C, D, X (K + 8), S11, &h698098d8 md5_ff D, a, B, C, X (K + 9), S12, &h8b44f7af md5_ff C, D, a, B, X (k +), S13, &hffff5BB1 md5_ff B, C, D, A, X (k + one), S14, &h895cd7be md5_ff A, B, C, D, X (K +), S11, &h6b901122 md5_ff D, a, B, C, X (k +), S12, &hfd987193 md5_ff C, D, a, B, X (k +), S13, &ha679438e md5_ff B, C, D, A, X (k +), S14, &  h49b40821 Md5_gg A, B, C, D, X (k + 1), S21, &hf61e2562 Md5_gg D, a, B, C, X (K + 6), S22, &hc040b340 Md5_gg C, D, A, B, X (k + one), S23, &h265e5a51 Md5_gg B, C, D, A, X (k + 0), S24, &he9b6c7aa Md5_gg A, B, C, D, X (K + 5), S21, & Amp hd62f105d Md5_gg D, a, B, C, X (K +), S22, &h2441453 Md5_gg C, D, a, B, X (k +), S23, &hd8a1e681 Md5_gg B, C, D, A, X (K + 4), S24, &he7d3fbc8 Md5_gg A, B, C, D, X (K + 9), S21, &h21e1cde6 Md5_gg D, a, B, C, X (K +), S22, &A mp Hc33707d6 Md5_gg C, D, a, B, X (k + 3), S23, &hf4d50d87 Md5_gg B, C, D, A, X (K + 8), S24, &h455a14ed Md5_gg A, B, c , d, X (K +), S21, &ha9e3e905 Md5_gg D, a, B, C, X (K + 2), S22, &hfcefa3f8 Md5_gg C, D, a, B, X (k + 7), S23, &am P H676F02D9 Md5_gG B, C, D, A, X (k +), S24, &h8d2a4c8a md5_hh A, B, C, D, X (K + 5), S31, &hfffa3942 md5_hh D, a, B, C, X (k + 8 
), S32, &h8771f681 md5_hh C, D, a, B, X (k + one), S33, &h6d9d6122 md5_hh B, C, D, A, X (k +), S34, &hfde5380c Md5_hh A, B, C, D, X (k + 1), S31, &ha4beea44 md5_hh D, a, B, C, X (K + 4), S32, &h4bdecfa9 md5_hh C, D, a, B, X (k + 7), S33, &hf6bb4b60 md5_hh B, C, D, A, X (k +), S34, &hbebfbc70 md5_hh A, B, C, D, X (K +), S31, &h289b7 EC6 md5_hh D, a, B, C, x (k + 0), S32, &heaa127fa md5_hh C, D, a, B, X (k + 3), S33, &hd4ef3085 md5_hh B, C, D, a, X (k + 6), S34, &h4881d05 md5_hh A, B, C, D, X (K + 9), S31, &hd9d4d039 md5_hh D, a, B, C, X (K +), S32, &he6db 99E5 md5_hh C, D, a, B, X (k +), S33, &h1fa27cf8 md5_hh B, C, D, A, X (K + 2), S34, &hc4ac5665 Md5_ii A, B, C, D, X (k + 0), S41, &hf4292244 Md5_ii D, a, B, C, X (k + 7), S42, &h432aff97 Md5_ii C, D, a, B, X (k +), S43, & Hab9423a7 Md5_ii B, C, D, A, X (K + 5), S44, &hfc93a039 Md5_ii A, B, C, D, X (K +), S41, &h655b59c3 Md5_ii D, a, B, C, X (K + 3), S42, &am P H8f0ccc92 Md5_ii C, D, a, B, X (k +), S43, &hffeff47d Md5_ii B, C, D, A, X (k + 1), S44, &h85845dd1 Md5_ii A, B, C, D, X (K + 8), S41, &h6fa87e4f Md5_ii D, a, B, C, X (K +), S42, &hfe2ce6e0 Md5_ii C, D, a, B, X (k + 6), S43, &A mp HA3014314 Md5_ii B, C, D, A, X (k +), S44, &h4e0811a1 Md5_ii A, B, C, D, X (K + 4), S41, &hf7537e82 Md5_ii D, A, b, C, X (K + one), S42, &hbd3af235 Md5_ii C, D, a, B, X (k + 2), S43, &AMP;H2AD7D2BB Md5_ii B, C, D, A, X (k + 9), S44, &A mp heb86d391 a = addunsigned (A, AA) b = addunsigned (b, BB) C = addunsigned (c, CC) d = addunsigned (d, DD) Next If stype = Then MD5 = LCase (Wordtohex (a) & Wordtohex (b) & Wordtohex (c) & Wordtohex (d)) Else MD5 = LCase (Wordtohex (b) & Amp Wordtohex (c)) End If End Function Sub Test () MsgBox MD5 ("a", 16) ' 16-bit encryption MsgBox MD5 ("a", 32) ' 32-bit encrypted end Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.