Base64 编码未产生所需的编码值

Base64 编码未产生所需的编码值

我正在尝试对以下信息进行 Base 64 编码:


{ "expiration": "2015-12-30T12:00:00.000Z",
  "conditions": [
    {"bucket": "sigv4examplebucket"},
    ["starts-with", "$key", "user/user1/"],
    {"acl": "public-read"},
    {"success_action_redirect": "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html"},
    ["starts-with", "$Content-Type", "image/"],
    {"x-amz-meta-uuid": "14365123651274"},
    {"x-amz-server-side-encryption": "AES256"},
    ["starts-with", "$x-amz-meta-tag", ""],

    {"x-amz-credential": "AKIAIOSFODNN7EXAMPLE/20151229/us-east-1/s3/aws4_request"},
    {"x-amz-algorithm": "AWS4-HMAC-SHA256"},
    {"x-amz-date": "20151229T000000Z" }
  ]
}

我将上述文件剪切并粘贴到一个文件中,然后运行以下命令:

base64 data.json

并得到以下结果:

eyAiZXhwaXJhdGlvbiI6ICIyMDE1LTEyLTMwVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25z
IjogWwogICAgeyJidWNrZXQiOiAic2lndjRleGFtcGxlYnVja2V0In0sCiAgICBbInN0YXJ0cy13
aXRoIiwgIiRrZXkiLCAidXNlci91c2VyMS8iXSwKICAgIHsiYWNsIjogInB1YmxpYy1yZWFkIn0s
CiAgICB7InN1Y2Nlc3NfYWN0aW9uX3JlZGlyZWN0IjogImh0dHA6Ly9zaWd2NGV4YW1wbGVidWNr
ZXQuczMuYW1hem9uYXdzLmNvbS9zdWNjZXNzZnVsX3VwbG9hZC5odG1sIn0sCiAgICBbInN0YXJ0
cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiaW1hZ2UvIl0sCiAgICB7IngtYW16LW1ldGEtdXVp
ZCI6ICIxNDM2NTEyMzY1MTI3NCJ9LAogICAgeyJ4LWFtei1zZXJ2ZXItc2lkZS1lbmNyeXB0aW9u
IjogIkFFUzI1NiJ9LAogICAgWyJzdGFydHMtd2l0aCIsICIkeC1hbXotbWV0YS10YWciLCAiIl0s
CgogIC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iOiAiMjAxNTEyMjlUMDAwMDAwWiIgfQ0KICBdDQp9

该网站确实这样说:

当您复制/粘贴上述策略时,它应该包含回车符和新行,以便计算出的哈希值与该值匹配(即 ASCII 文本,带有 CRLF 行终止符)。

我所做的就是 在模式下打开vi 并粘贴 JSON 。insert这是否会引入隐藏角色,或者我只是做错了?

答案1

我所做的就是打开 vi 并以插入模式粘贴 JSON。这是否会引入隐藏角色,或者我只是做错了?

恰恰相反。 Linux 默认只使用“换行”字节。该网站要求您必须先输入“回车”,然后再输入“换行”(CRLF)。所以您没有添加隐藏字节...您无法添加隐藏字节。

此外,他们提供的 base64 字符串显示最后一行末尾没有行终止符。 Vim 会自动添加一个。因此,我建议您不要使用 vim,而只解码它们提供的 base64 字符串:

echo eyAiZXhwaXJhdGlvbiI6ICIyMDE1LTEyLTMwVDEyOjAwOjAwLjAwMFoiLA0KICAiY29uZGl0aW9ucyI6IFsNCiAgICB7ImJ1Y2tldCI6ICJzaWd2NGV4YW1wbGVidWNrZXQifSwNCiAgICBbInN0YXJ0cy13aXRoIiwgIiRrZXkiLCAidXNlci91c2VyMS8iXSwNCiAgICB7ImFjbCI6ICJwdWJsaWMtcmVhZCJ9LA0KICAgIHsic3VjY2Vzc19hY3Rpb25fcmVkaXJlY3QiOiAiaHR0cDovL3NpZ3Y0ZXhhbXBsZWJ1Y2tldC5zMy5hbWF6b25hd3MuY29tL3N1Y2Nlc3NmdWxfdXBsb2FkLmh0bWwifSwNCiAgICBbInN0YXJ0cy13aXRoIiwgIiRDb250ZW50LVR5cGUiLCAiaW1hZ2UvIl0sDQogICAgeyJ4LWFtei1tZXRhLXV1aWQiOiAiMTQzNjUxMjM2NTEyNzQifSwNCiAgICB7IngtYW16LXNlcnZlci1zaWRlLWVuY3J5cHRpb24iOiAiQUVTMjU2In0sDQogICAgWyJzdGFydHMtd2l0aCIsICIkeC1hbXotbWV0YS10YWciLCAiIl0sDQoNCiAgICB7IngtYW16LWNyZWRlbnRpYWwiOiAiQUtJQUlPU0ZPRE5ON0VYQU1QTEUvMjAxNTEyMjkvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJ9LA0KICAgIHsieC1hbXotYWxnb3JpdGhtIjogIkFXUzQtSE1BQy1TSEEyNTYifSwNCiAgICB7IngtYW16LWRhdGUiOiAiMjAxNTEyMjlUMDAwMDAwWiIgfQ0KICBdDQp9 | base64 -d > data.json

阅读您引用的AWS页面,您的base64编码字符串与他们的字符串不匹配实际上并不重要。他们仅将此作为示例。

也就是说,它们不要求您自己构建的 JSON 对象在文本中使用 CRLF 而不是 LF。只要你签字你的base64 编码的字符串并发送应该没问题。


如果您确实想手动获取与 AWS 示例完全匹配的文件,那么您可以使用unix2dos(从 dos2unix 包交换行编码),然后用head.

unix2dos < data.json | head -c -2 > data2.json

相关内容