重新编码(到 base64)时 base64 字符串的解码值给出不同的输出
Decoded value of base64 string when re-encoded(to base64) gives different output
我真的对这里的某些事情感到困惑。当我解码以下 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)
给我 x
,而 encode(x)
给我 base64_B
,我如何从 x
生成 base64_A
或base64_B
?
您显示的解码文本中包含非 ASCII 字符。 Base64 仅对二进制数据进行编码,因此在 encoding/decoding 文本时必须考虑字符编码。
编码base64时,字符串必须先字符集编码为字节,例如UTF-8,然后这些字节可以用base64编码。
解码base64时,结果是字节,然后可以将其字符集解码为字符串。
如果您使用错误的字符集,您将得到不正确的结果。
我真的对这里的某些事情感到困惑。当我解码以下 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)
给我 x
,而 encode(x)
给我 base64_B
,我如何从 x
生成 base64_A
或base64_B
?
您显示的解码文本中包含非 ASCII 字符。 Base64 仅对二进制数据进行编码,因此在 encoding/decoding 文本时必须考虑字符编码。
编码base64时,字符串必须先字符集编码为字节,例如UTF-8,然后这些字节可以用base64编码。
解码base64时,结果是字节,然后可以将其字符集解码为字符串。
如果您使用错误的字符集,您将得到不正确的结果。