An example of ASP implementation of MD5 irreversible encryption algorithm

Source: Internet
Author: User
Tags exit md5
Encryption | Algorithm this is a foreign reprint function, can convert any character to MD5 16 for character encryption form, and for irreversible change.
<%
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 &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
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)



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.