在Base64中添加的填充,即使字符串长度是th的倍数

2024-09-27 22:19:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下字符串及其Base64编码的verison:

temp = "Last Star Wars 'not for children'\n\nThe sixth and final Star Wars movie may not be suitable for young children, film-maker George Lucas has said.\n\nHe told US TV show 60 Minutes that Revenge of the Sith would be the darkest and most violent of the series. \"I don't think I would take a five or six-year-old to this,\" he told the CBS programme, to be aired on Sunday. Lucas predicted the film would get a US rating advising parents some scenes may be unsuitable for under-13s. It opens in the UK and US on 19 May. He said he expected the film would be classified PG-13 - roughly equivalent to a British 12A rating.\n\nThe five previous Star Wars films have all carried less restrictive PG - parental guidance - ratings in the US. In the UK, they have all been passed U - suitable for all - with the exception of Attack of The Clones, which got a PG rating in 2002. Revenge of the Sith - the third prequel to the original 1977 Star Wars film - chronicles the transformation of the heroic Anakin Skywalker into the evil Darth Vader as he travels to a Hell-like planet composed of erupting volcanoes and molten lava. \"We're going to watch him make a pact with the devil,\" Lucas said. \"The film is much more dark, more emotional. It's much more of a tragedy.\"\n"

temp_enc = "TGFzdCBTdGFyIFdhcnMgJ25vdCBmb3IgY2hpbGRyZW4nXG5cblRoZSBzaXh0aCBhbmQgZmluYWwgU3RhciBXYXJzIG1vdmllIG1heSBub3QgYmUgc3VpdGFibGUgZm9yIHlvdW5nIGNoaWxkcmVuLCBmaWxtLW1ha2VyIEdlb3JnZSBMdWNhcyBoYXMgc2FpZC5cblxuSGUgdG9sZCBVUyBUViBzaG93IDYwIE1pbnV0ZXMgdGhhdCBSZXZlbmdlIG9mIHRoZSBTaXRoIHdvdWxkIGJlIHRoZSBkYXJrZXN0IGFuZCBtb3N0IHZpb2xlbnQgb2YgdGhlIHNlcmllcy4gXCJJIGRvbid0IHRoaW5rIEkgd291bGQgdGFrZSBhIGZpdmUgb3Igc2l4LXllYXItb2xkIHRvIHRoaXMsXCIgaGUgdG9sZCB0aGUgQ0JTIHByb2dyYW1tZSwgdG8gYmUgYWlyZWQgb24gU3VuZGF5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GhlIGRldmlsLFwiIEx1Y2FzIHNhaWQuIFwiVGhlIGZpbG0gaXMgbXVjaCBtb3JlIGRhcmssIG1vcmUgZW1vdGlvbmFsLiBJdCdzIG11Y2ggbW9yZSBvZiBhIHRyYWdlZHkuXCJcbg=="

>>> len(temp)
1251
>>> len(temp_enc)
1688
>>> len(temp)/3
417
>>> (len(temp)/3)*4
1668

字符串的长度可以被3整除。既然每3个字节我们就有4个字节的编码,那么为什么编码的字符串比预期的要长??为什么要在编码中添加填充?在


Tags: andoftheto字符串编码forlen
1条回答
网友
1楼 · 发布于 2024-09-27 22:19:41

temp_enctemp的base64编码:

In [61]: import base64
In [62]: base64.b64encode(temp) == temp_enc
Out[62]: False

如果解码temp_enc,则解码字符串的长度为1264,而不是1251:

^{pr2}$

尽管temp包含换行符,\ntemp_dec包含后面跟ns的文本反斜杠:

In [67]: temp[:50]
Out[67]: "Last Star Wars 'not for children'\n\nThe sixth and f"

In [66]: temp_dec[:50]
Out[66]: "Last Star Wars 'not for children'\\n\\nThe sixth and"

如果您将temp = base64.b64decode(temp_enc))视为temp,那么

In [56]: math.ceil(len(base64.b64decode(temp_enc))/3.0)*4
Out[56]: 1688.0

等于

In [49]: len(temp_enc)
Out[49]: 1668

这与每3个3字节的temp转换为4个temp_enc字节的声明是一致的。在

相关问题 更多 >

    热门问题