简体   繁体   English

解码 Base64 字符串时填充不正确

[英]Incorrect padding while decoding a Base64 string

I am trying to decode a Base64 encoded byte string to a valid HTTP URL.我正在尝试将 Base64 编码的字节字符串解码为有效的 HTTP URL。 I have tried appending necessary padding (=).我尝试添加必要的填充(=)。 But it still does not seem to work.但它似乎仍然不起作用。

I have tried the following code.我已经尝试了以下代码。

import base64
encoded = b"aHR0cHM6Ly9mb3Jtcy5nbGUvWU5ZXQ0d2NRWHVLNnNwdjU="
decoded = base64.b64decode(encoded)
print(decoded)

The string encoded has a missing character as a part of noise. encoded的字符串有一个缺失字符作为噪声的一部分。 Is there a way to detect that missing character and then perform the decode operation?有没有办法检测到丢失的字符然后执行解码操作?

So, you have this aHR0cHM6Ly9mb3Jtcy5nbGUvWU5ZXQ0d2NRWHVLNnNwdjU= base64 encoding of an URL with exactly one character missing.所以,你有一个 URL 编码的aHR0cHM6Ly9mb3Jtcy5nbGUvWU5ZXQ0d2NRWHVLNnNwdjU= base64编码,恰好缺少一个字符。

For the missing character, you've 64 choices: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/ (for base64 ) and 48 possible positions to put the missing character in -aHR-0-cHM-6-Ly-9-mb-3-Jtcy-5-nbGUvWU-5-ZXQ-0-d-2-NRWHVLNnNwdjU-=- ( - indicates the possible positions)对于缺少的字符,您有64 个选项: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/ (对于base64 )和48 个可能的位置将缺少的字符放入-aHR-0-cHM-6-Ly-9-mb-3-Jtcy-5-nbGUvWU-5-ZXQ-0-d-2-NRWHVLNnNwdjU-=--表示可能的位置)

So, you've 64 * 48 = 3072 possible encoded strings.所以,你有64 * 48 = 3072 个可能的编码字符串。 Either you can try to generate them by your hand or write some code to do the same.您可以尝试手动生成它们,也可以编写一些代码来做同样的事情。

Once you generate them, you can decode the string to get the URL using some built-in libraries & check whether this URL is valid or not.生成它们后,您可以使用一些内置库解码字符串以获取URL并检查此URL是否有效 If you also need to know whether this URL exists or not, you can make an HTTP request to the URL & check the response StatusCode .如果您还想知道这个URL是否存在,您可以向 URL 发出HTTP请求并检查响应状态码

Code:代码:

package main

import (
    "encoding/base64"
    "fmt"
    "net/http"
)

func main() {
    encodedURL := "aHR0cHM6Ly9mb3Jtcy5nbGUvWU5ZXQ0d2NRWHVLNnNwdjU="
    options := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"

    length := len(encodedURL)

    for i := 0; i <= length; i++ {
        for idx := 0; idx < 64; idx++ {
            tempEncoded := encodedURL[:i] + options[idx:idx+1] + encodedURL[i:]
            decodedURL, _ := base64.URLEncoding.DecodeString(tempEncoded)

            resp, err := http.Get(string(decodedURL))
            if err == nil && resp.StatusCode == http.StatusOK {
                fmt.Println("this URL is valid & exists: ", string(decodedURL))
            }
        }
    }
}

when the length of the unencoded input is not a multiple of three, the encoded output must have padding added so that its length is a multiple of four.当未编码输入的长度不是三的倍数时,编码的 output 必须添加填充,使其长度为四的倍数。

len(encoded) is 47 , it should be 48 , So append another = len(encoded)47 ,应该是48 ,所以 append 另一个=

encoded = b"aHR0cHM6Ly9mb3Jtcy5nbGUvWU5ZXQ0d2NRWHVLNnNwdjU=="
print(decoded)
b'https://forms.gle/YNY]\r\x1d\xd8\xd4V\x1dR\xcd\x9c\xdc\x1d\x8d'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM