autoit同步php的base64算法

autoit之base64算法,同步php

Local $base64_table[] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', _
		'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', _
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', _
		'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', _
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0']
Local $base64_reverse_table[256] = [-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, _
		 - 1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63, _
		52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2, _
		 - 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, _
		15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2, _
		 - 2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, _
		41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, _
		 - 2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2]
Func _base64_decode($in, ByRef $out, $strict = False)
	Local $inl = StringLen($in)
	Local $ch;
	Local $i = 0, $padding = 0, $j = 0;
	Local $index = 1
	Local $aDecode[$inl]
	While ($inl > 0)
		$inl -= 1
		$ch = StringMid($in, $index, 1) ;
		$index += 1
		If ($ch == '=') Then
			$padding += 1;
			ContinueLoop;
		EndIf
		$ch = Asc($ch)
		$ch = $base64_reverse_table[$ch];
		If (Not $strict) Then
			If ($ch < 0) Then ContinueLoop
		Else
			If ($ch == -1) Then ContinueLoop
			If ($ch == -2 Or $padding) Then Return 0
		EndIf
		Switch Mod($i, 4)
			Case 0
				$aDecode[$j] = BitShift($ch, -2)
			Case 1
				$aDecode[$j] = BitOR($aDecode[$j], BitShift($ch, 4))
				$j += 1
				$aDecode[$j] = BitShift(BitAND($ch, 0x0f), -4)
			Case 2
				$aDecode[$j] = BitOR($aDecode[$j], BitShift($ch, 2))
				$j += 1
				$aDecode[$j] = BitShift(BitAND($ch, 0x03), -6)
			Case 3
				$aDecode[$j] = BitOR($aDecode[$j], $ch)
				$j += 1
		EndSwitch
		$i += 1;
	WEnd
	If ($strict And Mod($i, 4) == 1) Then Return 0
	If ($strict And $padding And ($padding > 2 Or Mod($i + $padding, 4) <> 0)) Then Return 0
	$outl = $j;
	$out = '0x'
	For $i = 0 To $j - 1
		$out &= Hex($aDecode[$i], 2)
	Next
	$out = BinaryToString($out)
	Return 1;
EndFunc   ;==>_base64_decode
Func _base64_encode($in, $flags = False)
	Local $inl = StringLen($in)
	Local $Encode[$inl * 2]
	Local $index = 0
	Local $n = 0
	While ($inl > 2)
		$Encode[$index] = $base64_table[BitShift(Asc(StringMid($in, 1 + $n * 3, 1)), 2)];
		$index += 1
		$Encode[$index] = $base64_table[BitShift(BitAND(Asc(StringMid($in, 1 + $n * 3, 1)), 0x03), -4) + BitShift(Asc(StringMid($in, 2 + $n * 3, 1)), 4)];
		$index += 1
		$Encode[$index] = $base64_table[BitShift(BitAND(Asc(StringMid($in, 2 + $n * 3, 1)), 0x0f), -2) + BitShift(Asc(StringMid($in, 3 + $n * 3, 1)), 6)];
		$index += 1
		$Encode[$index] = $base64_table[BitAND(Asc(StringMid($in, 3 + $n * 3, 1)), 0x3f)];
		$index += 1
		$n += 1;
		$inl -= 3
	WEnd
	If ($inl <> 0) Then
		$Encode[$index] = $base64_table[BitShift(Asc(StringMid($in, 1 + $n * 3, 1)), 2)];
		$index += 1
		If ($inl > 1) Then
			$Encode[$index] = $base64_table[BitShift(BitAND(Asc(StringMid($in, 1 + $n * 3, 1)), 0x03), -4) + BitShift(Asc(StringMid($in, 2 + $n * 3, 1)), 4)];
			$index += 1
			$Encode[$index] = $base64_table[BitShift(BitAND(Asc(StringMid($in, 2 + $n * 3, 1)), 0x0f), -2)];
			$index += 1
			If (BitAND($flags, 1) == 0) Then
				$Encode[$index] = '=';
				$index += 1
			EndIf
		Else
			$Encode[$index] = $base64_table[BitShift(BitAND(Asc(StringMid($in, 1 + $n * 3, 1)), 0x03), -4)];
			$index += 1
			If (BitAND($flags, 1) == 0) Then
				$Encode[$index] = '=';
				$index += 1
				$Encode[$index] = '=';
				$index += 1
			EndIf
		EndIf
	EndIf
	$out = ''
	For $i = 0 To $index
		$out &= $Encode[$i]
	Next
	Return $out;
EndFunc   ;==>_base64_encode

Local $src = 'helloworld'
Local $enstr = _base64_encode($src)
ConsoleWrite($enstr & @CRLF)
Local $outstr = ''
If _base64_decode($enstr, $outstr) Then ConsoleWrite($outstr & @CRLF)


打赏

本篇文章链接 地址:https://wmzos.com/?id=144

相关阅读

添加新评论