xowen 发表于 2015-12-10 17:17:21

【已解决】Base64编解码不准确,求解!

本帖最后由 xowen 于 2017-3-29 15:37 编辑

#include <ACN_HASH.au3>
$var='cn=测 试,ou=gsphonebook,dc=sz,dc=com'
ConsoleWrite(_Base64Encode($var))运行完成后输出的字符串为:Y249suIgytQsb3U9Z3NwaG9uZWJvb2ssZGM9c3osZGM9Yw==

通过http://www1.tc711.com/tool/BASE64.htm网站进行Base64编解码时,
cn=测 试,ou=gsphonebook,dc=sz,dc=com输出为:
Y2495rWLIOivlSxvdT1nc3Bob25lYm9vayxkYz1zeixkYz1jb20=
使用网站上的Base64编码后,Openldap可以正常识别,但通过AutoIT生成的Base64无法识别。
另外,将网站成本的Base64通过Autoit解码后为:
cn=娴?璇?ou=gsphonebook,dc=sz,dc=com=>中文乱码。

论坛中看到如下使用方法:
_Base64Encode(StringToBinary($var))
但编码后还是和网上不一致。

AutoIT带的函数库有问题么?如果和网上的同步编解码出同样的字符串?

gto250 发表于 2015-12-10 21:08:31

MsgBox(0,"",base64('cn=测 试,ou=gsphonebook,dc=sz,dc=com'))


Func base64($str,$p=1)
Local $code
$code&='var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";'&@CRLF
$code&='var base64DecodeChars = new Array('&@CRLF
$code&='-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,'&@CRLF
$code&='-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,'&@CRLF
$code&='-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,'&@CRLF
$code&='52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,'&@CRLF
$code&='-1,0,1,2,3,4,5,6,7,8,9, 10, 11, 12, 13, 14,'&@CRLF
$code&='15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,'&@CRLF
$code&='-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,'&@CRLF
$code&='41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);'&@CRLF
$code&='function base64encode(str) {'&@CRLF
$code&='var out, i, len;'&@CRLF
$code&='var c1, c2, c3;'&@CRLF
$code&='len = str.length;'&@CRLF
$code&='i = 0;'&@CRLF
$code&='out = "";'&@CRLF
$code&='while(i < len) {'&@CRLF
$code&='c1 = str.charCodeAt(i++) & 0xff;'&@CRLF
$code&='if(i == len)'&@CRLF
$code&='{'&@CRLF
$code&='out += base64EncodeChars.charAt(c1 >> 2);'&@CRLF
$code&='out += base64EncodeChars.charAt((c1 & 0x3) << 4);'&@CRLF
$code&='out += "==";'&@CRLF
$code&='break;'&@CRLF
$code&='}'&@CRLF
$code&='c2 = str.charCodeAt(i++);'&@CRLF
$code&='if(i == len)'&@CRLF
$code&='{'&@CRLF
$code&='out += base64EncodeChars.charAt(c1 >> 2);'&@CRLF
$code&='out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));'&@CRLF
$code&='out += base64EncodeChars.charAt((c2 & 0xF) << 2);'&@CRLF
$code&='out += "=";'&@CRLF
$code&='break;'&@CRLF
$code&='}'&@CRLF
$code&='c3 = str.charCodeAt(i++);'&@CRLF
$code&='out += base64EncodeChars.charAt(c1 >> 2);'&@CRLF
$code&='out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));'&@CRLF
$code&='out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));'&@CRLF
$code&='out += base64EncodeChars.charAt(c3 & 0x3F);'&@CRLF
$code&='}'&@CRLF
$code&='return out;'&@CRLF
$code&='}'&@CRLF
$code&='function base64decode(str) {'&@CRLF
$code&='var c1, c2, c3, c4;'&@CRLF
$code&='var i, len, out;'&@CRLF
$code&='len = str.length;'&@CRLF
$code&='i = 0;'&@CRLF
$code&='out = "";'&@CRLF
$code&='while(i < len) {'&@CRLF
$code&='do {'&@CRLF
$code&='c1 = base64DecodeChars;'&@CRLF
$code&='} while(i < len && c1 == -1);'&@CRLF
$code&='if(c1 == -1)'&@CRLF
$code&='break;'&@CRLF
$code&='do {'&@CRLF
$code&='c2 = base64DecodeChars;'&@CRLF
$code&='} while(i < len && c2 == -1);'&@CRLF
$code&='if(c2 == -1)'&@CRLF
$code&='break;'&@CRLF
$code&='out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));'&@CRLF
$code&='do {'&@CRLF
$code&='c3 = str.charCodeAt(i++) & 0xff;'&@CRLF
$code&='if(c3 == 61)'&@CRLF
$code&='return out;'&@CRLF
$code&='c3 = base64DecodeChars;'&@CRLF
$code&='} while(i < len && c3 == -1);'&@CRLF
$code&='if(c3 == -1)'&@CRLF
$code&='break;'&@CRLF
$code&='out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));'&@CRLF
$code&='do {'&@CRLF
$code&='c4 = str.charCodeAt(i++) & 0xff;'&@CRLF
$code&='if(c4 == 61)'&@CRLF
$code&='return out;'&@CRLF
$code&='c4 = base64DecodeChars;'&@CRLF
$code&='} while(i < len && c4 == -1);'&@CRLF
$code&='if(c4 == -1)'&@CRLF
$code&='break;'&@CRLF
$code&='out += String.fromCharCode(((c3 & 0x03) << 6) | c4);'&@CRLF
$code&='}'&@CRLF
$code&='return out;'&@CRLF
$code&='}'&@CRLF
$code&='function utf16to8(str) {'&@CRLF
$code&='var out, i, len, c;'&@CRLF
$code&='out = "";'&@CRLF
$code&='len = str.length;'&@CRLF
$code&='for(i = 0; i < len; i++) {'&@CRLF
$code&='c = str.charCodeAt(i);'&@CRLF
$code&='if ((c >= 0x0001) && (c <= 0x007F)) {'&@CRLF
$code&='out += str.charAt(i);'&@CRLF
$code&='} else if (c > 0x07FF) {'&@CRLF
$code&='out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));'&@CRLF
$code&='out += String.fromCharCode(0x80 | ((c >>6) & 0x3F));'&@CRLF
$code&='out += String.fromCharCode(0x80 | ((c >>0) & 0x3F));'&@CRLF
$code&='} else {'&@CRLF
$code&='out += String.fromCharCode(0xC0 | ((c >>6) & 0x1F));'&@CRLF
$code&='out += String.fromCharCode(0x80 | ((c >>0) & 0x3F));'&@CRLF
$code&='}'&@CRLF
$code&='}'&@CRLF
$code&='return out;'&@CRLF
$code&='}'&@CRLF
$code&='function utf8to16(str) {'&@CRLF
$code&='var out, i, len, c;'&@CRLF
$code&='var char2, char3;'&@CRLF
$code&='out = "";'&@CRLF
$code&='len = str.length;'&@CRLF
$code&='i = 0;'&@CRLF
$code&='while(i < len) {'&@CRLF
$code&='c = str.charCodeAt(i++);'&@CRLF
$code&='switch(c >> 4)'&@CRLF
$code&='{'&@CRLF
$code&='case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:'&@CRLF
$code&='out += str.charAt(i-1);'&@CRLF
$code&='break;'&@CRLF
$code&='case 12: case 13:'&@CRLF
$code&='char2 = str.charCodeAt(i++);'&@CRLF
$code&='out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));'&@CRLF
$code&='break;'&@CRLF
$code&='case 14:'&@CRLF
$code&='char2 = str.charCodeAt(i++);'&@CRLF
$code&='char3 = str.charCodeAt(i++);'&@CRLF
$code&='out += String.fromCharCode(((c & 0x0F) << 12) |'&@CRLF
$code&='((char2 & 0x3F) << 6) |'&@CRLF
$code&='((char3 & 0x3F) << 0));'&@CRLF
$code&='break;'&@CRLF
$code&='}'&@CRLF
$code&='}'&@CRLF
$code&='return out;'&@CRLF
$code&='}'&@CRLF
$VBS = ObjCreate("ScriptControl")
$VBS.language = "jscript"
$VBS.addcode($code)
If $p=1 Then
Return $VBS.Eval("base64encode(utf16to8('"&$str&"'))")
ElseIf $p=0 Then
Return $VBS.Eval("utf8to16(base64decode('"&$str&"'))")
EndIf
EndFunc


其实做一下编码转换就好了,懒得写。直接调用js吧

xowen 发表于 2015-12-11 09:48:12

RE: Base64编解码不准确,求解!【已解决】

回复 2# gto250
明白了,使用StringToBinary时,标志参数使用4即可转码。谢谢!

htmgyv 发表于 2016-5-18 14:44:30

编码不一致吧!
页: [1]
查看完整版本: 【已解决】Base64编解码不准确,求解!