Asp generates a prize code that does not require a database

Source: Internet
Author: User
Tags idate

Congratulations, you have won the prize. Your winning code is (please remember, the prize must be received): XXXXXXXXXXX

Then the user enters XXXXXXXXXXX. After simple verification, the user can receive the prize.

Have you ever used such a feature or want to develop it?

One idea is to input a batch of unique winning codes to the database. When winning the prize, extract one and mark it to tell the winning code
User.

But is there any other way to input a batch of winning codes into the database first?

For example: 200807151054281502895e585d7e4b529e, can you: you can confirm the prize if you fail to query and verify the database?
Is the code valid (not forged?

I have a idea. If you want to, please let me know :)

Example: 200807151054281502895e585d7e4b529e

It consists of three parts:
20080715105428 1502 895e585d7e4b529e

20080715105428 is YYYYMMDDhhmmss
1502 is a four-digit random number to prevent the first part from repeating.
The first two parts of 895e585d7e4b529e + the MD5 16-bit value of PrivateKey

One sentence: the first 18 digits of the Prize Code are related to the last 16 digits.

In this way, as long as others cannot guess your PrivateKey, they will not be able to forge your winning code.

Weaknesses :(

The Code is as follows:

Getkey () returns a unique winning code.
Ckkey (s) Verification of Prize CodeCopy codeThe Code is as follows: <%
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 = 31 Then
If lValue And 1 Then
LShift = & H80000000
Else
LShift = 0
End If
Exit Function
ElseIf iShiftBits <0 Or iShiftBits> 31 Then
Err. Raise 6
End If

If (lValue And m_l2Power (31-iShiftBits) Then
LShift = (lValue And m_lOnBits (31-(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 = 31 Then
If lValue And & H80000000 Then
RShift = 1
Else
RShift = 0
End If
Exit Function
ElseIf iShiftBits <0 Or iShiftBits> 31 Then
Err. Raise 6
End If

RShift = (lValue And & effecffffffe) \ 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 (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 (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 (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 (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) \ messages) \ (MODULUS_BITS \ messages) + 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)
WordToHex = WordToHex & Right ("0" & Hex (lByte), 2)
Next
End Function

Public Function MD5 (sMessage)
M_lOnBits (0) = CLng (1)
M_lOnBits (1) = CLng (3)
M_lOnBits (2) = CLng (7)
M_lOnBits (3) = CLng (15)
M_lOnBits (4) = CLng (31)
M_lOnBits (5) = CLng (63)
M_lOnBits (6) = CLng (1, 127)
M_lOnBits (7) = CLng (255)
M_lOnBits (8) = CLng (511)
M_lOnBits (9) = CLng (1, 1023)
M_lOnBits (10) = CLng (2047)
M_lOnBits (11) = CLng (4095)
M_lOnBits (12) = CLng (8191)
M_lOnBits (13) = CLng (1, 16383)
M_lOnBits (14) = CLng (32767)
M_lOnBits (15) = CLng (65535)
M_lOnBits (16) = CLng (131071)
M_lOnBits (17) = CLng (262143)
M_lOnBits (18) = CLng (1, 524287)
M_lOnBits (19) = CLng (1048575)
M_lOnBits (20) = CLng (2097151)
M_lOnBits (21) = CLng (4194303)
M_lOnBits (22) = CLng (1, 8388607)
M_lOnBits (23) = CLng (1, 16777215)
M_lOnBits (24) = CLng (33554431)
M_lOnBits (25) = CLng (67108863)
M_lOnBits (26) = CLng (134217727)
M_lOnBits (27) = CLng (268435455)
M_lOnBits (28) = CLng (536870911)
M_lOnBits (29) = CLng (1073741823)
M_lOnBits (30) = 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 (16)
M_l2Power (5) = CLng (32)
M_l2Power (6) = CLng (64)
M_l2Power (7) = CLng (128)
M_l2Power (8) = CLng (256)
M_l2Power (9) = CLng (512)
M_l2Power (10) = CLng (1024)
M_l2Power (11) = CLng (2048)
M_l2Power (12) = CLng (4096)
M_l2Power (13) = CLng (8192)
M_l2Power (14) = CLng (16384)
M_l2Power (15) = CLng (32768)
M_l2Power (16) = CLng (65536)
M_l2Power (17) = CLng (131072)
M_l2Power (18) = CLng (262144)
M_l2Power (19) = CLng (524288)
M_l2Power (20) = CLng (1048576)
M_l2Power (21) = CLng (1, 2097152)
M_l2Power (22) = CLng (4194304)
M_l2Power (23) = CLng (8388608)
M_l2Power (24) = CLng (16777216)
M_l2Power (25) = CLng (33554432)
M_l2Power (26) = CLng (67108864)
M_l2Power (27) = CLng (134217728)
M_l2Power (28) = CLng (268435456)
M_l2Power (29) = CLng (536870912)
M_l2Power (30) = CLng (1073741824)

Dim x
Dim k
Dim AA
Dim BB
Dim CC
Dim DD
Dim
Dim B
Dim c
Dim d

Const S11 = 7
Const S12 = 12
Const S13 = 17
Const S14 = 22
Const S21 = 5
Const S22 = 9
Const S23 = 14
Const S24 = 20
Const S31 = 4
Const S32 = 11
Const S33 = 16
Const S34 = 23
Const S41 = 6
Const S42 = 10
Const S43 = 15
Const S44 = 21

X = ConvertToWordArray (sMessage)

A = & H67452301
B = & HEFCDAB89
C = & H98BADCFE
D = & H10325476

For k = 0 To UBound (x) Step 16
AA =
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, & 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, & HFD469501
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 + 10), S13, & HFFFF5BB1
Md5_FF B, c, d, a, x (k + 11), S14, & H895CD7BE
Md5_FF a, B, c, d, x (k + 12), S11, & H6B901122
Md5_FF d, a, B, c, x (k + 13), S12, & HFD987193
Md5_FF c, d, a, B, x (k + 14), S13, & HA679438E
Md5_FF B, c, d, a, x (k + 15), 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 + 11), S23, & H265E5A51
Md5_GG B, c, d, a, x (k + 0), S24, & HE9B6C7AA
Md5_GG a, B, c, d, x (k + 5), S21, & HD62F105D
Md5_GG d, a, B, c, x (k + 10), S22, & H2441453
Md5_GG c, d, a, B, x (k + 15), 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 + 14), S22, & 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 + 13), S21, & HA9E3E905
Md5_GG d, a, B, c, x (k + 2), S22, & HFCEFA3F8
Md5_GG c, d, a, B, x (k + 7), S23, & H676F02D9
Md5_GG B, c, d, a, x (k + 12), 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 + 11), S33, & H6D9D6122
Md5_HH B, c, d, a, x (k + 14), 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 + 10), S34, & HBEBFBC70
Md5_HH a, B, c, d, x (k + 13), S31, & H289B7EC6
Md5_HH d, a, B, c, x (k + 0), S32, & heaa1_fa
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 + 12), S32, & HE6DB99E5
Md5_HH c, d, a, B, x (k + 15), 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 + 14), S43, & HAB9423A7
Md5_II B, c, d, a, x (k + 5), S44, & HFC93A039
Md5_II a, B, c, d, x (k + 12), S41, & H655B59C3
Md5_II d, a, B, c, x (k + 3), S42, & H8F0CCC92
Md5_II c, d, a, B, x (k + 10), 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 + 15), S42, & HFE2CE6E0
Md5_II c, d, a, B, x (k + 6), S43, & HA3014314
Md5_II B, c, d, a, x (k + 13), S44, & H4E0811A1
Md5_II a, B, c, d, x (k + 4), S41, & HF7537E82
Md5_II d, a, B, c, x (k + 11), S42, & HBD3AF235
Md5_II c, d, a, B, x (k + 2), S43, & H2AD7D2BB
Md5_II B, c, d, a, x (k + 9), S44, & HEB86D391

A = AddUnsigned (a, AA)
B = AddUnsigned (B, BB)
C = AddUnsigned (c, CC)
D = AddUnsigned (d, DD)
Next

'Md5 = UCase (WordToHex (a) & WordToHex (B) & WordToHex (c) & WordToHex (d) '32byte
MD5 = LCase (WordToHex (B) & WordToHex (c) 'I crop this to fit 16 byte database password
End Function

Function CFTGetServerDate (d)
Dim strTmp, iYear, iMonth, iDate
IYear = Year (d)
IMonth = Month (d)
IDate = Day (d)

StrTmp = CStr (iYear)
If iMonth <10 Then
StrTmp = strTmp & "0" & Cstr (iMonth)
Else
StrTmp = strTmp & Cstr (iMonth)
End If
If iDate <10 Then
StrTmp = strTmp & "0" & Cstr (iDate)
Else
StrTmp = strTmp & Cstr (iDate)
End If
CFTGetServerDate = strTmp
End Function

Function CFTGetServertime (d)
Dim strTmp, iYear, iMonth, iDate
IYear = hour (d)
IMonth = minute (d)
IDate = second (d)

StrTmp = ""

If iYear <10 Then
StrTmp = strTmp & "0" & Cstr (iYear)
Else
StrTmp = strTmp & Cstr (iYear)
End If

If iMonth <10 Then
StrTmp = strTmp & "0" & Cstr (iMonth)
Else
StrTmp = strTmp & Cstr (iMonth)
End If
If iDate <10 Then
StrTmp = strTmp & "0" & Cstr (iDate)
Else
StrTmp = strTmp & Cstr (iDate)
End If
CFTGetServertime = strTmp
End Function

Function GetRnd (min, max)
Randomize
GetRnd = Int (max-min + 1) * Rnd + min)
End function

Function getrnddatenum ()
Getrnddatenum = CFTGetServerDate (now) & CFTGetServertime (now) & GetRnd (1000,9999)
End function

'Privatekey' should be as complex as possible. It cannot be changed after use.
Dim keyinfokey
Keyinfokey = "51windows.net @ 1234567890! @ # $ % ^ & * () QWERFGHJK"

'Get the prize code
Function getkey ()
Skey = getrnddatenum ()
Skey2 = md5 (skey & keyinfokey)
Getkey = skey & skey2
End function

'Verify the Prize Code
Function ckkey (s)
Strs = s
If len (strs) <> 34 then
Ckkey = false
Exit function
End if
Datenum = left (strs, 18)
Md5oldstr = right (strs, 16)
Md5str = md5 (datenum & keyinfokey)
If lcase (md5str) = lcase (md5oldstr) then
Ckkey = true
Else
Ckkey = false
End if
End function
%>

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.