<!--#include file="php_MD5.asp"-->
<!--#include file="php_base64.asp"-->
<%
'///////////////////////////////////////////////////////////////////////////////
'//              UC-Client For ASP
'// 作    者:    舞者/LOVE
'// 技术支持:    QQ:48673641
'// 官方网站:    www.a8kk.com
'// 程序名称:    UC-Client For ASP
'// 单元名称:    uc_authcode.asp
'// 开始时间:    2010.01.05
'// 备    注:    整合UCenter所用的ASP版_authcode加密解密函数uc_authcode
'///////////////////////////////////////////////////////////////////////////////

'================================================
'函数名: uc_authcode
'作  用:  加/解密字符串
'参  数: str	    : 要加密或解密的字符串
'        operation	: 操作：等于DECODE时是解密操作，否则是加密操作
'        key		:  加密密匙，UC与应用程序所设置的相同的加密密匙字符串
'返回值: 加/解密后的字符串
'================================================
Function uc_authcode(ByVal str, ByVal operation, ByVal key)
    uc_authcode = ""
    operation = UCase(operation)
    If Len(str)<4 Then Exit Function
    Const ckey_length = 4
    Dim keya, keyb, keyc, md5MT, cryptkey, key_length, string_length, box, rndkey, i, j, tmp, iTmp, tmpResult, a, x1, x2
    key = Php_MD5(key)
    keya = Php_MD5(Left(key, 16))
    keyb = Php_MD5(Right(key, 16))
    md5MT = Php_MD5(microtime())
	keyc = ""
    If ckey_length > 0 Then
        If operation="DECODE" Then
            keyc = Left(str, ckey_length)
        Else
            keyc = Right(md5MT, ckey_length)
        End If
	End If 
    cryptkey = keya & Php_MD5(keya & keyc)
    key_length = Len(cryptkey)
	Redim box(255)
    For i = 0 To 255
        box(i) = i
    Next
    Redim rndkey(255)
    For i = 0 To 255
        rndkey(i) = Asc(Mid(cryptkey, i Mod key_length + 1, 1))
    Next
	j = 0
    For i = 0 To 255
        j = (j + box(i) + rndkey(i)) Mod 256
        tmp = box(i)
        box(i) = box(j)
        box(j) = tmp
    Next
	If operation="DECODE" Then
        tmp = Right(str, Len(str)-ckey_length)
        tmpResult = php_Base64Decode(tmp)
        string_length = UBound(tmpResult)+1
        a = 0
        j = 0
        For i = 0 To string_length-1
            a = (a + 1) Mod 256
            j = (j + box(a)) Mod 256
            tmp = box(a)
            box(a) = box(j)
            box(j) = tmp
            x1 = tmpResult(i)
            iTmp = (box(a) + box(j)) Mod 256
            x2 = box(iTmp)
            tmpResult(i) = x1 Xor x2
        Next
		tmp = ""
		For i=0 To 9
			tmp = tmp & ChrB(tmpResult(i))
		Next
		If IsNumeric(tmp) Then iTmp = CLng(tmp) Else iTmp = 0
        x1 = ""
        For i = 10 To 25
    	    x1 = x1 & Chr(tmpResult(i))
        Next
        tmp = ""
        For i = 26 To UBound(tmpResult)
    	    tmp = tmp & ChrB(tmpResult(i))
        Next
        tmp = strAnsi2Unicode(tmp)
        x2 = Left(Php_MD5(tmp & keyb), 16)
		uc_authcode = ""
        If (iTmp = 0 Or iTmp > php_time()) And (x1=x2) Then uc_authcode = tmp      
    Else
  	    str = "0000000000" & Left(Php_MD5(str + keyb), 16) & str
  	    str = strUnicode2Ansi(str)
  	    string_length = LenB(str)
  	    Redim tmpResult(string_length-1)
        a = 0
        j = 0
        For i = 0 To string_length-1
	        a = (a + 1) Mod 256
	        j = (j + box(a)) Mod 256
	        tmp = box(a)
	        box(a) = box(j)
	        box(j) = tmp
	        x1 = AscB(MidB(str, i+1, 1))
	        iTmp = (box(a) + box(j)) Mod 256
	        x2 = box(iTmp)
	        tmpResult(i) = x1 Xor x2
        Next
        uc_authcode = keyc & Replace(php_Base64Encode(tmpResult),"=","")
    End If
End Function


Const TimeZone=8	'服务器所在时区
Function php_time()
	php_time = dateadd("h", TimeZone*-1, now())
	php_time = datediff("s", "1970-01-01 00:00:00", php_time)
End Function

Function microtime()
  Dim sec, msec, i, s
  sec = php_time()
  msec = timer()*1000 Mod 1000
  i = Max(0, 8-Len(Cstr(msec)))
  s = String(i,"0")
  microtime = "0." & msec & s & " " & sec
End Function

Private Function Min(x,y)
  if x<y then Min=x Else Min=y
End Function

Private Function Max(x,y)
  if x>y then Max=x Else Max=y
End Function
%>