MD5 Encryption Algorithm ASP version

Source: Internet
Author: User
Tags exit md5 md5 encryption
Encryption | algorithm | encryption | algorithm

<%
Private Const BITS_TO_A_BYTE = 8
Private Const BYTES_TO_A_WORD = 4
Private Const BITS_TO_A_WORD = 32
Private M_lonbits (30)
Private M_l2power (30)

Private Function LShift (Lvalue, Ishiftbits)
If ishiftbits = 0 Then
LShift = Lvalue
Exit function
ElseIf ishiftbits = Then
If Lvalue and 1 then
LShift = &h80000000
Else
LShift = 0
End If
Exit function
ElseIf ishiftbits < 0 or ishiftbits > Then
Err.Raise 6
End If

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_lonbits (31-ishiftbits)) * M_l2power (ishiftbits))
End If
End Function

Private Function Rshift (Lvalue, Ishiftbits)
If ishiftbits = 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 > Then
Err.Raise 6
End If

Rshift = (lvalue and &h7ffffffe) \ M_l2power (ishiftbits)

if (Lvalue and &h80000000) then
Rshift = (rshift or (&h40000000 \ m_l2power (ishiftbits-1))
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

LRESULT = (LX and &H3FFFFFFF) + (ly and &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
Else
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 function 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

Private 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 = addunsigned (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 = addunsigned (A, addunsigned (addunsigned (md5_i (b, C, D), x), AC)
A = Rotateleft (A, s)
A = Addunsigned (A, B)
End Sub

Private Function Converttowordarray (smessage)
Dim lmessagelength
Dim lnumberofwords
Dim Lwordarray ()
Dim lbyteposition
Dim Lbytecount
Dim Lwordcount

Const MODULUS_BITS = 512
Const CONGRUENT_BITS = 448

Lmessagelength = Len (smessage)

Lnumberofwords = ((Lmessagelength + ((modulus_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
Lwordcount = 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

Lwordarray (Lwordcount) = Lwordarray (Lwordcount) or LShift (&h80, lbyteposition)

Lwordarray (lnumberofwords-2) = LShift (Lmessagelength, 3)
Lwordarray (lnumberofwords-1) = Rshift (lmessagelength, 29)

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_to_a_byte-1)
Wordtohe



Related Article

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.