重新编码时(base64)的base64字符串的解码值给出不同的输出

时间:2016-12-01 01:51:27

标签: utf-8 character-encoding base64 decode utf-16

我真的很困惑这里的事情。当我解码以下base64字符串

RgRaIY6KP0EIASwW7bCCVSJXCGJsYWhibGFoWAQAAAAASB1UcmFuc2FjdGlvbmFsOiBQYXNzd29yZCBSZXNldEIKAASKCT9YVegBjVITZXhhbXBsZTAxQGdtYWlsLmNvbQlRBAAAAABE6mh0dHBzOi8vd3d3LmV4YW1wbGUxLmNvbS9wYXNzd29yZC1yZXNldD9pZD02MkZZUFcmaGFzaD0yYmI0OTIwODBmMjJjNTZnNmNhYzAzNDlmNDNmNmRiYiZ1dG1fY2FtcGFpZ249VHJhbnNhY3Rpb25hbCUzQSUyMFBhc3N3b3JkJTIwUmVzZXQmdXRtX3NvdXJjZT1TcGFya1Bvc3QmdXRtX21lZGl1bT1lbWFpbCZ1dG1fdGVybT1UcmFuc2FjdGlvbmFsJTNBJTIwUGFzc3dvcmQlMjBSZXNldCZyZWZlcmVyPTYyRllQV0dReyJlbWFpbF91dWlkIjoiMTQ4MDUyNDU3ODQxMS4yODgyMDc0MDY3Nzc4OTUyODk5NTUzOTkiLCJiaW5kaW5nIjoibm90aWZpY2F0aW9ucyJ9

输出结果为:

FZ!?A,�U"WwwwwwwwwXHTransactional: Aaaaaaaa Gggggg ?XU恍Rexample01@gmail.com QD駴tps://www.abcdefgh.com/endpoint--name?id=62FYPW&valu=2ffg92080f22c50c6grsd349frtyuio9&utm_campaign=Transactional%3A%20Password%20Reset&utm_source=SparkPost&utm_medium=email&utm_term=Transactional%3A%20Pppp%20Mainn&referer=62FYPWGQ{"email_user":"14785243699632.288207406700815289974144","binding":"notifications"}

但是当我尝试将相同的输出值编码回base64时,它给了我:

RgRaIY4/QQgBLBb9VSJXCHd3d3d3d3d3WARIHVRyYW5zYWN0aW9uYWw6IEFhYWFhYWFhIEdnZ2dnZwoEij9YVU1SE2V4YW1wbGUwMUBnbWFpbC5jb20JUQRE9HRwczovL3d3dy5hYmNkZWZnaC5jb20vZW5kcG9pbnQtLW5hbWU/aWQ9NjJGWVBXJnZhbHU9MmZmZzkyMDgwZjIyYzUwYzZncnNkMzQ5ZnJ0eXVpbzkmdXRtX2NhbXBhaWduPVRyYW5zYWN0aW9uYWwlM0ElMjBQYXNzd29yZCUyMFJlc2V0JnV0bV9zb3VyY2U9U3BhcmtQb3N0JnV0bV9tZWRpdW09ZW1haWwmdXRtX3Rlcm09VHJhbnNhY3Rpb25hbCUzQSUyMFBwcHAlMjBNYWlubiZyZWZlcmVyPTYyRllQV0dReyJlbWFpbF91c2VyIjoiMTQ3ODUyNDM2OTk2MzIuMjg4MjA3NDA2NzAwODE1Mjg5OTc0MTQ0IiwiYmluZGluZyI6Im5vdGlmaWNhdGlvbnMifQ==

这里发生了什么?

如果decode(base64_A)给了我xencode(x)给了我base64_B,我如何从base64_A或{{{{}}生成x 1}}?

1 个答案:

答案 0 :(得分:1)

您显示的已解码文本中包含非ASCII字符。 Base64仅对二进制数据进行编码,因此在编码/解码文本时必须考虑字符编码。

编码base64时,首先必须将字符串字符串编码为字节,例如UTF-8,然后用base64编码这些字节。

解码base64时,结果是字节,然后可以将字符串解码为字符串。

如果使用错误的字符集,则会得到错误的结果。