JavaScript的基地64解码二进制数据不起作用 [英] JavaScript Base 64 Decoding Binary Data Doesn't Work
问题描述
我有一个从我的服务器,加载的base64文件连接codeS,并呼应了出来。一个简单的PHP文件
然后我有使用jQuery获取该文件中,德的base64 code,并做一个校验和测试一个简单的HTML页面。校验测试是行不通的。
我md5'd在PHP文件编码后,并md5'd它在JavaScript中对其进行解码之前匹配校验(所以没事就去运输过程中错误的)。然而,pre编码和解码后校验和不匹配。
我使用 webtoolkit.base64.js JavaScript中对其进行解码。该文件是一个二进制文件(ZIP文件)。
是否存在与解码库或别的东西,我不知道,可能导致此问题的问题?难道是与我使用MD5库的问题( http://pajhome.org .UK /地穴/ MD5 / md5.html )
摘要
您的MD5库是OK,你的base64库被打破了。
块引用>双方你的JavaScript库的Base64和MD5库中无法正常工作。
- 我创建并核实的15097字节的ZIP文件。
MD5校验:a9de6b8e5a9173140cb46d4b3b31b67c
- 我的base64恩codeD文件: http://pastebin.com/2rfdTzYT ( 20132字节)。
- 验证在引擎收录为Base64文件的长度,使用下面的JavaScript片段:
<$ C $Ç> document.querySelector('DE 1。')textContent.replace(/ \\ s /克,'')的长度; $ C C> 锂>Base64的德code文件的正确使用的
ATOB
,并验证大小:window.b64_str = document.querySelector('DE 1')textContent.replace(/ \\ s /克,'');
的console.log(ATOB(window.b64_str)。长度); / * * 15097 /
- 我验证这两个文件是完全相等使用
hexdump都
JavaScript库,而XXD
UNIX命令(如提供的对于Windows EXE文件)。锂>使用您的Base64德codeR,我得到的8094.大小的字符串的这不是15097!
在我的测试中,我发现ATOB
方法返回一定字节序列后不正确字节,包括回车。我还没有找到一个解决的办法。您MD5库就可以了。
I have a simple PHP file which loads a file from my server, base64 encodes it and echoes it out.
Then I have a simple HTML page that uses jQuery to fetch this file, base64 decode it and do a checksum test. The checksum test is not working.
I md5'd the file in PHP after encoding it and md5'd it in javascript before decoding it and the checksums matched (So nothing went wrong during transit). However, the pre encoding and post decoding checksums do NOT match.
I am using webtoolkit.base64.js for decoding it in JavaScript. The file is a binary file (A ZIP archive).
Is there a problem with the decoding library or something else I'm not aware of that could cause this issue? Could it be a problem with the MD5 library I'm using (http://pajhome.org.uk/crypt/md5/md5.html)
解决方案Summary
Your MD5 library is OK, your base64 library is broken.Both your JavaScript Base64 library and MD5 library are not working correctly.
- I have created and verified a ZIP file of 15097 bytes.
MD5 sum:a9de6b8e5a9173140cb46d4b3b31b67c
- I have base64-encoded this file: http://pastebin.com/2rfdTzYT (20132 bytes).
- Verify the length of the base64 file at pastebin, using the following JavaScript snippet:
document.querySelector('.de1').textContent.replace(/\s/g,'').length;
Base64-decode the file properly using
atob
, and verify the size:window.b64_str = document.querySelector('.de1').textContent.replace(/\s/g,''); console.log( atob(window.b64_str).length ); /* 15097 */
- I verified that both files were exactly equal using the
Hexdump
JavaScript library, and thexxd
UNIX command (available as EXE file for Windows).Using your Base64 decoder, I get a string with the size of 8094. That is not 15097! During my tests, I discovered that the
atob
method returned incorrect bytes after certain byte sequences, including carriage returns. I have not yet found a solution to this.Your MD5 library is OK.
这篇关于JavaScript的基地64解码二进制数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!