Error decoding incorrect paddling with decode64, with padding present?











up vote
-2
down vote

favorite












With the code that I am using, every time there is a "?" on data of the encoded message I get an error back "Incorrect Padding". When using a decoder online I get the correct value for both value1 and value2 below, but for value2 where there should be a "?" I get an exception instead.



The code is:



value1 = "Y29udGludWENCg=="
expected1 = b'continuarn'
value2 = "Y29udGludWE_DQo="
expected2 = b'continua?rn'

data1 = base64.b64decode(value1)
assert data1 == expected1
data2 = base64.b64decode(value2)
assert data2 == expected2


Decoding value2 throws a binascii.Error: Incorrect padding exception, but the data has the right amount of padding (the length of value2 is a multiple of 4).










share|improve this question
























  • Your base64 padding is indeed invalid. It can be repaired, which is what an online decoder might have done.
    – Martijn Pieters
    Nov 20 at 8:21










  • Just add the missing '='. Remove all non-Base64 characters first, then if the length is not a multiple of 4, add = characters until it is.
    – Martijn Pieters
    Nov 20 at 8:23










  • could you show me a exemple martijn?
    – joao santana
    Nov 20 at 8:26










  • Ah, I see what is going on: You have URL-safe data. Use base64.urlsafe_b64decode().
    – Martijn Pieters
    Nov 20 at 8:35










  • In future, start with a complete Minimal, Complete, and Verifiable example; I've filled in the blanks here once I figured out what kind of data you were expecting. Don't leave people trying to help you to guess.
    – Martijn Pieters
    Nov 20 at 10:33















up vote
-2
down vote

favorite












With the code that I am using, every time there is a "?" on data of the encoded message I get an error back "Incorrect Padding". When using a decoder online I get the correct value for both value1 and value2 below, but for value2 where there should be a "?" I get an exception instead.



The code is:



value1 = "Y29udGludWENCg=="
expected1 = b'continuarn'
value2 = "Y29udGludWE_DQo="
expected2 = b'continua?rn'

data1 = base64.b64decode(value1)
assert data1 == expected1
data2 = base64.b64decode(value2)
assert data2 == expected2


Decoding value2 throws a binascii.Error: Incorrect padding exception, but the data has the right amount of padding (the length of value2 is a multiple of 4).










share|improve this question
























  • Your base64 padding is indeed invalid. It can be repaired, which is what an online decoder might have done.
    – Martijn Pieters
    Nov 20 at 8:21










  • Just add the missing '='. Remove all non-Base64 characters first, then if the length is not a multiple of 4, add = characters until it is.
    – Martijn Pieters
    Nov 20 at 8:23










  • could you show me a exemple martijn?
    – joao santana
    Nov 20 at 8:26










  • Ah, I see what is going on: You have URL-safe data. Use base64.urlsafe_b64decode().
    – Martijn Pieters
    Nov 20 at 8:35










  • In future, start with a complete Minimal, Complete, and Verifiable example; I've filled in the blanks here once I figured out what kind of data you were expecting. Don't leave people trying to help you to guess.
    – Martijn Pieters
    Nov 20 at 10:33













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











With the code that I am using, every time there is a "?" on data of the encoded message I get an error back "Incorrect Padding". When using a decoder online I get the correct value for both value1 and value2 below, but for value2 where there should be a "?" I get an exception instead.



The code is:



value1 = "Y29udGludWENCg=="
expected1 = b'continuarn'
value2 = "Y29udGludWE_DQo="
expected2 = b'continua?rn'

data1 = base64.b64decode(value1)
assert data1 == expected1
data2 = base64.b64decode(value2)
assert data2 == expected2


Decoding value2 throws a binascii.Error: Incorrect padding exception, but the data has the right amount of padding (the length of value2 is a multiple of 4).










share|improve this question















With the code that I am using, every time there is a "?" on data of the encoded message I get an error back "Incorrect Padding". When using a decoder online I get the correct value for both value1 and value2 below, but for value2 where there should be a "?" I get an exception instead.



The code is:



value1 = "Y29udGludWENCg=="
expected1 = b'continuarn'
value2 = "Y29udGludWE_DQo="
expected2 = b'continua?rn'

data1 = base64.b64decode(value1)
assert data1 == expected1
data2 = base64.b64decode(value2)
assert data2 == expected2


Decoding value2 throws a binascii.Error: Incorrect padding exception, but the data has the right amount of padding (the length of value2 is a multiple of 4).







python base64 decode






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 10:32









Martijn Pieters

695k12924052245




695k12924052245










asked Nov 20 at 8:18









joao santana

45




45












  • Your base64 padding is indeed invalid. It can be repaired, which is what an online decoder might have done.
    – Martijn Pieters
    Nov 20 at 8:21










  • Just add the missing '='. Remove all non-Base64 characters first, then if the length is not a multiple of 4, add = characters until it is.
    – Martijn Pieters
    Nov 20 at 8:23










  • could you show me a exemple martijn?
    – joao santana
    Nov 20 at 8:26










  • Ah, I see what is going on: You have URL-safe data. Use base64.urlsafe_b64decode().
    – Martijn Pieters
    Nov 20 at 8:35










  • In future, start with a complete Minimal, Complete, and Verifiable example; I've filled in the blanks here once I figured out what kind of data you were expecting. Don't leave people trying to help you to guess.
    – Martijn Pieters
    Nov 20 at 10:33


















  • Your base64 padding is indeed invalid. It can be repaired, which is what an online decoder might have done.
    – Martijn Pieters
    Nov 20 at 8:21










  • Just add the missing '='. Remove all non-Base64 characters first, then if the length is not a multiple of 4, add = characters until it is.
    – Martijn Pieters
    Nov 20 at 8:23










  • could you show me a exemple martijn?
    – joao santana
    Nov 20 at 8:26










  • Ah, I see what is going on: You have URL-safe data. Use base64.urlsafe_b64decode().
    – Martijn Pieters
    Nov 20 at 8:35










  • In future, start with a complete Minimal, Complete, and Verifiable example; I've filled in the blanks here once I figured out what kind of data you were expecting. Don't leave people trying to help you to guess.
    – Martijn Pieters
    Nov 20 at 10:33
















Your base64 padding is indeed invalid. It can be repaired, which is what an online decoder might have done.
– Martijn Pieters
Nov 20 at 8:21




Your base64 padding is indeed invalid. It can be repaired, which is what an online decoder might have done.
– Martijn Pieters
Nov 20 at 8:21












Just add the missing '='. Remove all non-Base64 characters first, then if the length is not a multiple of 4, add = characters until it is.
– Martijn Pieters
Nov 20 at 8:23




Just add the missing '='. Remove all non-Base64 characters first, then if the length is not a multiple of 4, add = characters until it is.
– Martijn Pieters
Nov 20 at 8:23












could you show me a exemple martijn?
– joao santana
Nov 20 at 8:26




could you show me a exemple martijn?
– joao santana
Nov 20 at 8:26












Ah, I see what is going on: You have URL-safe data. Use base64.urlsafe_b64decode().
– Martijn Pieters
Nov 20 at 8:35




Ah, I see what is going on: You have URL-safe data. Use base64.urlsafe_b64decode().
– Martijn Pieters
Nov 20 at 8:35












In future, start with a complete Minimal, Complete, and Verifiable example; I've filled in the blanks here once I figured out what kind of data you were expecting. Don't leave people trying to help you to guess.
– Martijn Pieters
Nov 20 at 10:33




In future, start with a complete Minimal, Complete, and Verifiable example; I've filled in the blanks here once I figured out what kind of data you were expecting. Don't leave people trying to help you to guess.
– Martijn Pieters
Nov 20 at 10:33












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You do not have standard Base64 data, you have URL-safe base64 data.



Base64 normally uses letters, digits and / and + characters, but the latter two carry special meaning in URLs, so an alternative Base64url encoding is used that uses - and _ characters instead.



Use the base64.urlsafe_b64decode() function to decode these strings:



data2 = base64.urlsafe_b64decode(value2)


Demo:



>>> import base64
>>> value2 = "Y29udGludWE_DQo="
>>> base64.urlsafe_b64decode(value2)
b'continua?rn'





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388786%2ferror-decoding-incorrect-paddling-with-decode64-with-padding-present%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You do not have standard Base64 data, you have URL-safe base64 data.



    Base64 normally uses letters, digits and / and + characters, but the latter two carry special meaning in URLs, so an alternative Base64url encoding is used that uses - and _ characters instead.



    Use the base64.urlsafe_b64decode() function to decode these strings:



    data2 = base64.urlsafe_b64decode(value2)


    Demo:



    >>> import base64
    >>> value2 = "Y29udGludWE_DQo="
    >>> base64.urlsafe_b64decode(value2)
    b'continua?rn'





    share|improve this answer



























      up vote
      0
      down vote



      accepted










      You do not have standard Base64 data, you have URL-safe base64 data.



      Base64 normally uses letters, digits and / and + characters, but the latter two carry special meaning in URLs, so an alternative Base64url encoding is used that uses - and _ characters instead.



      Use the base64.urlsafe_b64decode() function to decode these strings:



      data2 = base64.urlsafe_b64decode(value2)


      Demo:



      >>> import base64
      >>> value2 = "Y29udGludWE_DQo="
      >>> base64.urlsafe_b64decode(value2)
      b'continua?rn'





      share|improve this answer

























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        You do not have standard Base64 data, you have URL-safe base64 data.



        Base64 normally uses letters, digits and / and + characters, but the latter two carry special meaning in URLs, so an alternative Base64url encoding is used that uses - and _ characters instead.



        Use the base64.urlsafe_b64decode() function to decode these strings:



        data2 = base64.urlsafe_b64decode(value2)


        Demo:



        >>> import base64
        >>> value2 = "Y29udGludWE_DQo="
        >>> base64.urlsafe_b64decode(value2)
        b'continua?rn'





        share|improve this answer














        You do not have standard Base64 data, you have URL-safe base64 data.



        Base64 normally uses letters, digits and / and + characters, but the latter two carry special meaning in URLs, so an alternative Base64url encoding is used that uses - and _ characters instead.



        Use the base64.urlsafe_b64decode() function to decode these strings:



        data2 = base64.urlsafe_b64decode(value2)


        Demo:



        >>> import base64
        >>> value2 = "Y29udGludWE_DQo="
        >>> base64.urlsafe_b64decode(value2)
        b'continua?rn'






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 10:37

























        answered Nov 20 at 8:37









        Martijn Pieters

        695k12924052245




        695k12924052245






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388786%2ferror-decoding-incorrect-paddling-with-decode64-with-padding-present%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python