2月252017
Php-authcode算法之AU3版
<code class="language-autoit hljs">php的authcode加解密算法,翻译成AU3了。
<code class="language-autoit hljs">#NoTrayIcon
#PRE_UseX64=n
#QQ交流群:477266297
#include <Date.au3>
#include <array.au3>
Opt('MustDeclareVars', 1)
Local $OriginalStr = '1234567890abcdefghijklmnopqrstuvwxyz中英文测试'
Local $CryptText = authcode($OriginalStr, 'ENCODE', '123')
MsgBox(0, 'Decode' & ':' & $OriginalStr, 'CryptText:' & @CRLF & $CryptText _
& @CRLF & @CRLF & _
'Original:' & @CRLF & authcode($CryptText, 'DECODE', '123'))
; #FUNCTION# ============================================================
; Modified by haijie1223 and zldfsz in February 13, 2017
; QQ交流群:477266297
;// 参数解释
;// $string: 明文 或 密文
;// $operation:DECODE表示解密,其它表示加密
;// $key: 密匙
;// $expiry:密文有效期
;// $ckey_length: 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙
; ========================================================================
Func authcode($string, $operation = 'DECODE', $key = 'AU3_authcode', $expiry = 0, $CodingFlag = 4, $ckey_length = 4)
$key = _MD5($key);;// 密匙
Local $keya = _MD5(StringMid($key, 0 + 1, 16));;// 密匙a会参与加解密
Local $keyb = _MD5(StringMid($key, 16 + 1, 16));;// 密匙b会用来做数据完整性验证
Local $keyc = ($ckey_length ? ($operation == 'DECODE' ? StringMid($string, 0 + 1, $ckey_length) : StringRight(_MD5(time()), $ckey_length)) : '');;// 密匙c用于变化生成的密文
Local $cryptkey = $keya & _MD5($keya & $keyc);;// 参与运算的密匙
Local $key_length = StringLen($cryptkey);
If $operation == 'DECODE' Then ;// 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确
$string = _Base64Decode(StringMid($string, $ckey_length + 1))
Local $Tmpstring = ""
For $i = 3 To StringLen($string) Step 2
$Tmpstring &= ChrW(Dec(StringMid($string, $i, 2)))
Next
$string = $Tmpstring
Else ;// 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b),解密时会通过这个密匙验证数据完整性
$string = StringFormat('%010d', $expiry ? $expiry + time() : 0) & StringMid(_MD5($string & $keyb, $CodingFlag), 0 + 1, 16) & $string;
EndIf
Local $tmp = ''
For $i = 1 To StringLen($string)
If StringRegExp(StringMid($string, $i, 1), '[^\x00-\xff]') Then ;对字符串中的双字节字符进行编码,与php标准进行统一
Local $strTmp = StringToBinary(StringMid($string, $i, 1), $CodingFlag)
For $j = 3 To StringLen($strTmp) Step 2
$tmp &= ChrW(Dec(StringMid($strTmp, $j, 2)))
Next
Else
$tmp &= StringMid($string, $i, 1)
EndIf
Next
$string = $tmp
Local $box[256]
For $i = 0 To 255
$box[$i] = $i
Next
;// 产生密匙簿
Local $rndkey[256]
For $i = 0 To 255
$rndkey[$i] = AscW(StringMid($cryptkey, Mod($i, $key_length) + 1, 1));
Next
;// 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度
Local $j = 0
For $i = 0 To 255
$j = Mod(($j + $box[$i] + $rndkey[$i]), 256);
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
Next
;// 核心加解密部分
Local $a = 0, $j = 0, $result = '', $string_length = StringLen($string);
Local $resultC = ''
For $i = 0 To $string_length - 1
$a = Mod($a + 1, 256);
$j = Mod($j + $box[$a], 256);
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
;// 从密匙簿得出密匙进行异或,再转成字符
$result &= Hex(BitXOR( _
AscW(StringMid($string, $i + 1, 1)), $box[Mod($box[$a] + $box[$j], 256)] _
), 2);
Next
$result = '0x' & $result
If $operation == 'DECODE' Then
$result = BinaryToString($result, $CodingFlag)
;// StringMid($result, 0, 10) == 0 验证数据有效性
;// StringMid($result, 0, 10) - time() > 0 验证数据有效性
;// StringMid($result, 10, 16) == StringMid(md5(StringMid($result, 26).$keyb), 0, 16) 验证数据完整性
;// 验证数据有效性,请看未加密明文的格式
If (StringMid($result, 0 + 1, 10) = 0 Or StringMid($result, 0 + 1, 10) - time() > 0) And StringMid($result, 10 + 1, 16) == StringMid(_MD5(StringMid($result, 26 + 1) & $keyb, $CodingFlag), 0 + 1, 16) Then
$result = StringMid($result, 26 + 1)
Return $result
Else
Return '';
EndIf
Else
;// 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因
;// 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码
;~ Local $result1 = StringRegExpReplace($keyc & StringReplace(_Base64Encode($result), '=', ''), '\v', '')
$result = StringStripWS($keyc & StringReplace(_Base64Encode($result), '=', ''),8)
Return $result
EndIf
EndFunc ;==>authcode
Func time()
Return _DateDiff('s', "1970/01/01 08:00:00", _NowCalc())
EndFunc ;==>time
Func _MD5($Data, $Flag = 1)
$Data = StringToBinary($Data, $Flag)
Local $_MD5Opcode = '0xC85800005356576A006A006A008D45A850E8280000006A00FF750CFF75088D45A850E8440000006A006A008D45A850FF7510E8710700005F5E5BC9C210005589E58B4D0831C0894114894110C70101234567C7410489ABCDEFC74108FEDCBA98C7410C765432105DC21000C80C0000538B5D088B4310C1E80383E03F8945F88B4510C1E0030143103943107303FF43148B4510C1E81D0143146A40582B45F88945F4394510724550FF750C8B45F88D44031850E8A00700008D43185053E84E0000008B45F48945FC8B45FC83C03F39451076138B450C0345FC5053E8300000008345FC40EBE28365F800EB048365FC008B45102B45FC508B450C0345FC508B45F88D44031850E84D0700005BC9C21000C84000005356576A40FF750C8D45C050E8330700008B45088B088B50048B70088B780C89D021F089D3F7D321FB09D801C1034DC081C178A46AD7C1C10701D189C821D089CBF7D321F309D801C7037DC481C756B7C7E8C1C70C01CF89F821C889FBF7D321D309D801C60375C881C6DB702024C1C61101FE89F021F889F3F7D321CB09D801C20355CC81C2EECEBDC1C1C21601F289D021F089D3F7D321FB09D801C1034DD081C1AF0F7CF5C1C10701D189C821D089CBF7D321F309D801C7037DD481C72AC68747C1C70C01CF89F821C889FBF7D321D309D801C60375D881C6134630A8C1C61101FE89F021F889F3F7D321CB09D801C20355DC81C2019546FDC1C21601F289D021F089D3F7D321FB09D801C1034DE081C1D8988069C1C10701D189C821D089CBF7D321F309D801C7037DE481C7AFF7448BC1C70C01CF89F821C889FBF7D321D309D801C60375E881C6B15BFFFFC1C61101FE89F021F889F3F7D321CB09D801C20355EC81C2BED75C89C1C21601F289D021F089D3F7D321FB09D801C1034DF081C12211906BC1C10701D189C821D089CBF7D321F309D801C7037DF481C7937198FDC1C70C01CF89F821C889FBF7D321D309D801C60375F881C68E4379A6C1C61101FE89F021F889F3F7D321CB09D801C20355FC81C22108B449C1C21601F289D021F889FBF7D321F309D801C1034DC481C162251EF6C1C10501D189C821F089F3F7D321D309D801C7037DD881C740B340C0C1C70901CF89F821D089D3F7D321CB09D801C60375EC81C6515A5E26C1C60E01FE89F021C889CBF7D321FB09D801C20355C081C2AAC7B6E9C1C21401F289D021F889FBF7D321F309D801C1034DD481C15D102FD6C1C10501D189C821F089F3F7D321D309D801C7037DE881C753144402C1C70901CF89F821D089D3F7D321CB09D801C60375FC81C681E6A1D8C1C60E01FE89F021C889CBF7D321FB09D801C20355D081C2C8FBD3E7C1C21401F289D021F889FBF7D321F309D801C1034DE481C1E6CDE121C1C10501D189C821F089F3F7D321D309D801C7037D'
$_MD5Opcode &= 'F881C7D60737C3C1C70901CF89F821D089D3F7D321CB09D801C60375CC81C6870DD5F4C1C60E01FE89F021C889CBF7D321FB09D801C20355E081C2ED145A45C1C21401F289D021F889FBF7D321F309D801C1034DF481C105E9E3A9C1C10501D189C821F089F3F7D321D309D801C7037DC881C7F8A3EFFCC1C70901CF89F821D089D3F7D321CB09D801C60375DC81C6D9026F67C1C60E01FE89F021C889CBF7D321FB09D801C20355F081C28A4C2A8DC1C21401F289D031F031F801C1034DD481C14239FAFFC1C10401D189C831D031F001C7037DE081C781F67187C1C70B01CF89F831C831D001C60375EC81C622619D6DC1C61001FE89F031F831C801C20355F881C20C38E5FDC1C21701F289D031F031F801C1034DC481C144EABEA4C1C10401D189C831D031F001C7037DD081C7A9CFDE4BC1C70B01CF89F831C831D001C60375DC81C6604BBBF6C1C61001FE89F031F831C801C20355E881C270BCBFBEC1C21701F289D031F031F801C1034DF481C1C67E9B28C1C10401D189C831D031F001C7037DC081C7FA27A1EAC1C70B01CF89F831C831D001C60375CC81C68530EFD4C1C61001FE89F031F831C801C20355D881C2051D8804C1C21701F289D031F031F801C1034DE481C139D0D4D9C1C10401D189C831D031F001C7037DF081C7E599DBE6C1C70B01CF89F831C831D001C60375FC81C6F87CA21FC1C61001FE89F031F831C801C20355C881C26556ACC4C1C21701F289F8F7D009D031F001C1034DC081C1442229F4C1C10601D189F0F7D009C831D001C7037DDC81C797FF2A43C1C70A01CF89D0F7D009F831C801C60375F881C6A72394ABC1C60F01FE89C8F7D009F031F801C20355D481C239A093FCC1C21501F289F8F7D009D031F001C1034DF081C1C3595B65C1C10601D189F0F7D009C831D001C7037DCC81C792CC0C8FC1C70A01CF89D0F7D009F831C801C60375E881C67DF4EFFFC1C60F01FE89C8F7D009F031F801C20355C481C2D15D8485C1C21501F289F8F7D009D031F001C1034DE081C14F7EA86FC1C10601D189F0F7D009C831D001C7037DFC81C7E0E62CFEC1C70A01CF89D0F7D009F831C801C60375D881C6144301A3C1C60F01FE89C8F7D009F031F801C20355F481C2A111084EC1C21501F289F8F7D009D031F001C1034DD081C1827E53F7C1C10601D189F0F7D009C831D001C7037DEC81C735F23ABDC1C70A01CF89D0F7D009F831C801C60375C881C6BBD2D72AC1C60F01FE89C8F7D009F031F801C20355E481C291D386EBC1C21501F28B4508010801500401700801780C5F5E5BC9C20800C814000053E840000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008F45EC8B5D0C6A088D4310508D'
$_MD5Opcode &= '45F850E8510000008B4310C1E80383E03F8945F483F838730B6A38582B45F48945F0EB096A78582B45F48945F0FF75F0FF75ECFF750CE831F8FFFF6A088D45F850FF750CE823F8FFFF6A1053FF7508E8050000005BC9C210005589E55156578B7D088B750C8B4D10FCF3A45F5E595DC20C00'
Local $CodeBuffer = DllStructCreate("byte[" & BinaryLen($_MD5Opcode) & "]")
DllStructSetData($CodeBuffer, 1, $_MD5Opcode)
Local $Input = DllStructCreate("byte[" & BinaryLen($Data) & "]")
DllStructSetData($Input, 1, $Data)
Local $Digest = DllStructCreate("byte[16]")
DllCall("user32.dll", "none", "CallWindowProc", "ptr", DllStructGetPtr($CodeBuffer), _
"ptr", DllStructGetPtr($Input), _
"int", BinaryLen($Data), _
"ptr", DllStructGetPtr($Digest), _
"int", 0)
Local $Ret = DllStructGetData($Digest, 1)
$Input = 0
$Digest = 0
$CodeBuffer = 0
Return StringLower(StringTrimLeft($Ret, 2))
Return $Ret
EndFunc ;==>_MD5
Func _Base64Decode($Data)
Local $Opcode = "0xC81000005356578365F800E8500000003EFFFFFF3F3435363738393A3B3C3DFFFFFF00FFFFFF000102030405060708090A0B0C0D0E0F10111213141516171819FFFFFFFFFFFF1A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132338F45F08B7D0C8B5D0831D2E9910000008365FC00837DFC047D548A034384C0750383EA033C3D75094A803B3D75014AB00084C0751A837DFC047D0D8B75FCC64435F400FF45FCEBED6A018F45F8EB1F3C2B72193C7A77150FB6F083EE2B0375F08A068B75FC884435F4FF45FCEBA68D75F4668B06C0E002C0EC0408E08807668B4601C0E004C0EC0208E08847018A4602C0E00624C00A46038847028D7F038D5203837DF8000F8465FFFFFF89D05F5E5BC9C21000"
Local $CodeBuffer = DllStructCreate("byte[" & BinaryLen($Opcode) & "]")
DllStructSetData($CodeBuffer, 1, $Opcode)
Local $Ouput = DllStructCreate("byte[" & BinaryLen($Data) & "]")
Local $Ret = DllCall("user32.dll", "int", "CallWindowProcW", "ptr", DllStructGetPtr($CodeBuffer), _
"str", $Data, _
"ptr", DllStructGetPtr($Ouput), _
"int", 0, _
"int", 0)
Local $str = BinaryMid(DllStructGetData($Ouput, 1), 1)
Local $sRet = ''
For $i = StringLen($str) - 1 To 1 Step -2
If StringMid($str, $i, 2) <> '00' Then
$sRet = StringMid($str, 1, $i + 1)
ExitLoop
EndIf
Next
Return $sRet
EndFunc ;==>_Base64Decode
Func _Base64Encode($Data, $LineBreak = 76)
Local $Opcode = "0x5589E5FF7514535657E8410000004142434445464748494A4B4C4D4E4F505152535455565758595A6162636465666768696A6B6C6D6E6F707172737475767778797A303132333435363738392B2F005A8B5D088B7D108B4D0CE98F0000000FB633C1EE0201D68A06880731C083F901760C0FB6430125F0000000C1E8040FB63383E603C1E60409C601D68A0688470183F90176210FB6430225C0000000C1E8060FB6730183E60FC1E60209C601D68A06884702EB04C647023D83F90276100FB6730283E63F01D68A06884703EB04C647033D8D5B038D7F0483E903836DFC04750C8B45148945FC66B80D0A66AB85C90F8F69FFFFFFC607005F5E5BC9C21000"
Local $CodeBuffer = DllStructCreate("byte[" & BinaryLen($Opcode) & "]")
DllStructSetData($CodeBuffer, 1, $Opcode)
$Data = Binary($Data)
Local $Input = DllStructCreate("byte[" & BinaryLen($Data) & "]")
DllStructSetData($Input, 1, $Data)
$LineBreak = Floor($LineBreak / 4) * 4
Local $OputputSize = Ceiling(BinaryLen($Data) * 4 / 3)
$OputputSize = $OputputSize + Ceiling($OputputSize / $LineBreak) * 2 + 4
Local $Ouput = DllStructCreate("char[" & $OputputSize & "]")
DllCall("user32.dll", "none", "CallWindowProc", "ptr", DllStructGetPtr($CodeBuffer), _
"ptr", DllStructGetPtr($Input), _
"int", BinaryLen($Data), _
"ptr", DllStructGetPtr($Ouput), _
"uint", $LineBreak)
Return DllStructGetData($Ouput, 1)
EndFunc ;==>_Base64Encode```
<br />
扫描二维码,在手机上阅读
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!