PyCryptodome AES CBC encryption does not give desired output



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm trying to encrpyt & decrypt a simple text in AES with CBC mode in Python (2.7.14) with Pycryptodome (3.7.0)



Here's my code to attempt encryption:



from Crypto.Cipher import AES
from Crypto.Util import Padding
import base64
encryption_key = "1111111111111111111111111111111111111111111111111111111111111111".decode("hex")
text = "Test text"
text_padded = Padding.pad(text, AES.block_size)
iv = "0000000000000000"
cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
cipher_enc = cipher.encrypt(text_padded)
encrypted = iv + cipher_enc
print encrypted
print base64.b64encode(encrypted)
print encrypted.encode("hex")
print base64.b64encode(encrypted).encode("hex")


And the outputs are



0000000000000000X???]????H?
MDAwMDAwMDAwMDAwMDAwMFje9RzRXc3LHt8GBBLTSPQ=
3030303030303030303030303030303058def51cd15dcdcb1edf060412d348f4
4d4441774d4441774d4441774d4441774d4441774d466a6539527a525863334c4874384742424c545350513d


But when I enter the same key, text and initial vector values to http://aes.online-domain-tools.com/, I got different results.



Output is : 6a56bc5c0b05892ae4e63d0ca6b3169b



Here's the screenshot:



enter image description here



What am I doing wrong? How can I get the output value at the online encryption website by pycrypto?










share|improve this question






























    1















    I'm trying to encrpyt & decrypt a simple text in AES with CBC mode in Python (2.7.14) with Pycryptodome (3.7.0)



    Here's my code to attempt encryption:



    from Crypto.Cipher import AES
    from Crypto.Util import Padding
    import base64
    encryption_key = "1111111111111111111111111111111111111111111111111111111111111111".decode("hex")
    text = "Test text"
    text_padded = Padding.pad(text, AES.block_size)
    iv = "0000000000000000"
    cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
    cipher_enc = cipher.encrypt(text_padded)
    encrypted = iv + cipher_enc
    print encrypted
    print base64.b64encode(encrypted)
    print encrypted.encode("hex")
    print base64.b64encode(encrypted).encode("hex")


    And the outputs are



    0000000000000000X???]????H?
    MDAwMDAwMDAwMDAwMDAwMFje9RzRXc3LHt8GBBLTSPQ=
    3030303030303030303030303030303058def51cd15dcdcb1edf060412d348f4
    4d4441774d4441774d4441774d4441774d4441774d466a6539527a525863334c4874384742424c545350513d


    But when I enter the same key, text and initial vector values to http://aes.online-domain-tools.com/, I got different results.



    Output is : 6a56bc5c0b05892ae4e63d0ca6b3169b



    Here's the screenshot:



    enter image description here



    What am I doing wrong? How can I get the output value at the online encryption website by pycrypto?










    share|improve this question


























      1












      1








      1








      I'm trying to encrpyt & decrypt a simple text in AES with CBC mode in Python (2.7.14) with Pycryptodome (3.7.0)



      Here's my code to attempt encryption:



      from Crypto.Cipher import AES
      from Crypto.Util import Padding
      import base64
      encryption_key = "1111111111111111111111111111111111111111111111111111111111111111".decode("hex")
      text = "Test text"
      text_padded = Padding.pad(text, AES.block_size)
      iv = "0000000000000000"
      cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
      cipher_enc = cipher.encrypt(text_padded)
      encrypted = iv + cipher_enc
      print encrypted
      print base64.b64encode(encrypted)
      print encrypted.encode("hex")
      print base64.b64encode(encrypted).encode("hex")


      And the outputs are



      0000000000000000X???]????H?
      MDAwMDAwMDAwMDAwMDAwMFje9RzRXc3LHt8GBBLTSPQ=
      3030303030303030303030303030303058def51cd15dcdcb1edf060412d348f4
      4d4441774d4441774d4441774d4441774d4441774d466a6539527a525863334c4874384742424c545350513d


      But when I enter the same key, text and initial vector values to http://aes.online-domain-tools.com/, I got different results.



      Output is : 6a56bc5c0b05892ae4e63d0ca6b3169b



      Here's the screenshot:



      enter image description here



      What am I doing wrong? How can I get the output value at the online encryption website by pycrypto?










      share|improve this question
















      I'm trying to encrpyt & decrypt a simple text in AES with CBC mode in Python (2.7.14) with Pycryptodome (3.7.0)



      Here's my code to attempt encryption:



      from Crypto.Cipher import AES
      from Crypto.Util import Padding
      import base64
      encryption_key = "1111111111111111111111111111111111111111111111111111111111111111".decode("hex")
      text = "Test text"
      text_padded = Padding.pad(text, AES.block_size)
      iv = "0000000000000000"
      cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
      cipher_enc = cipher.encrypt(text_padded)
      encrypted = iv + cipher_enc
      print encrypted
      print base64.b64encode(encrypted)
      print encrypted.encode("hex")
      print base64.b64encode(encrypted).encode("hex")


      And the outputs are



      0000000000000000X???]????H?
      MDAwMDAwMDAwMDAwMDAwMFje9RzRXc3LHt8GBBLTSPQ=
      3030303030303030303030303030303058def51cd15dcdcb1edf060412d348f4
      4d4441774d4441774d4441774d4441774d4441774d466a6539527a525863334c4874384742424c545350513d


      But when I enter the same key, text and initial vector values to http://aes.online-domain-tools.com/, I got different results.



      Output is : 6a56bc5c0b05892ae4e63d0ca6b3169b



      Here's the screenshot:



      enter image description here



      What am I doing wrong? How can I get the output value at the online encryption website by pycrypto?







      python encryption pycrypto pycryptodome






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 13:23







      erdimeola

















      asked Nov 15 '18 at 13:05









      erdimeolaerdimeola

      769516




      769516






















          1 Answer
          1






          active

          oldest

          votes


















          1














          first in python 3: python 3 is a lot stricter about bytes vs strings.



          this reproduces the given example:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.hex())
          print(iv.hex())
          print(cipher_enc.hex())

          # 1111111111111111111111111111111111111111111111111111111111111111
          # 00000000000000000000000000000000
          # 6a56bc5c0b05892ae4e63d0ca6b3169b


          note that there is no need for encrypted = iv + cipher_enc; you are running AES in CBC mode already.




          got it to run on python 2 as well:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.encode('hex'))
          print(iv.encode('hex'))
          print(cipher_enc.encode('hex'))





          share|improve this answer

























          • Thank you very much, My problem was not using the b'' notation and adding initial vector value again I suppose.

            – erdimeola
            Nov 15 '18 at 13:43






          • 1





            you are welcome! good luck with pycrypto!

            – hiro protagonist
            Nov 15 '18 at 13:44











          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',
          autoActivateHeartbeat: false,
          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%2f53320143%2fpycryptodome-aes-cbc-encryption-does-not-give-desired-output%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









          1














          first in python 3: python 3 is a lot stricter about bytes vs strings.



          this reproduces the given example:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.hex())
          print(iv.hex())
          print(cipher_enc.hex())

          # 1111111111111111111111111111111111111111111111111111111111111111
          # 00000000000000000000000000000000
          # 6a56bc5c0b05892ae4e63d0ca6b3169b


          note that there is no need for encrypted = iv + cipher_enc; you are running AES in CBC mode already.




          got it to run on python 2 as well:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.encode('hex'))
          print(iv.encode('hex'))
          print(cipher_enc.encode('hex'))





          share|improve this answer

























          • Thank you very much, My problem was not using the b'' notation and adding initial vector value again I suppose.

            – erdimeola
            Nov 15 '18 at 13:43






          • 1





            you are welcome! good luck with pycrypto!

            – hiro protagonist
            Nov 15 '18 at 13:44















          1














          first in python 3: python 3 is a lot stricter about bytes vs strings.



          this reproduces the given example:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.hex())
          print(iv.hex())
          print(cipher_enc.hex())

          # 1111111111111111111111111111111111111111111111111111111111111111
          # 00000000000000000000000000000000
          # 6a56bc5c0b05892ae4e63d0ca6b3169b


          note that there is no need for encrypted = iv + cipher_enc; you are running AES in CBC mode already.




          got it to run on python 2 as well:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.encode('hex'))
          print(iv.encode('hex'))
          print(cipher_enc.encode('hex'))





          share|improve this answer

























          • Thank you very much, My problem was not using the b'' notation and adding initial vector value again I suppose.

            – erdimeola
            Nov 15 '18 at 13:43






          • 1





            you are welcome! good luck with pycrypto!

            – hiro protagonist
            Nov 15 '18 at 13:44













          1












          1








          1







          first in python 3: python 3 is a lot stricter about bytes vs strings.



          this reproduces the given example:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.hex())
          print(iv.hex())
          print(cipher_enc.hex())

          # 1111111111111111111111111111111111111111111111111111111111111111
          # 00000000000000000000000000000000
          # 6a56bc5c0b05892ae4e63d0ca6b3169b


          note that there is no need for encrypted = iv + cipher_enc; you are running AES in CBC mode already.




          got it to run on python 2 as well:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.encode('hex'))
          print(iv.encode('hex'))
          print(cipher_enc.encode('hex'))





          share|improve this answer















          first in python 3: python 3 is a lot stricter about bytes vs strings.



          this reproduces the given example:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.hex())
          print(iv.hex())
          print(cipher_enc.hex())

          # 1111111111111111111111111111111111111111111111111111111111111111
          # 00000000000000000000000000000000
          # 6a56bc5c0b05892ae4e63d0ca6b3169b


          note that there is no need for encrypted = iv + cipher_enc; you are running AES in CBC mode already.




          got it to run on python 2 as well:



          from Crypto.Cipher import AES

          encryption_key = 32 * b'x11'
          text = "Test text".encode()
          text_padded = text + (AES.block_size - (len(text) % AES.block_size)) * b'x00'
          iv = 16 * b'x00'
          cipher = AES.new(encryption_key, AES.MODE_CBC, iv)
          cipher_enc = cipher.encrypt(text_padded)
          print(encryption_key.encode('hex'))
          print(iv.encode('hex'))
          print(cipher_enc.encode('hex'))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 13:39

























          answered Nov 15 '18 at 13:33









          hiro protagonisthiro protagonist

          20.7k74264




          20.7k74264












          • Thank you very much, My problem was not using the b'' notation and adding initial vector value again I suppose.

            – erdimeola
            Nov 15 '18 at 13:43






          • 1





            you are welcome! good luck with pycrypto!

            – hiro protagonist
            Nov 15 '18 at 13:44

















          • Thank you very much, My problem was not using the b'' notation and adding initial vector value again I suppose.

            – erdimeola
            Nov 15 '18 at 13:43






          • 1





            you are welcome! good luck with pycrypto!

            – hiro protagonist
            Nov 15 '18 at 13:44
















          Thank you very much, My problem was not using the b'' notation and adding initial vector value again I suppose.

          – erdimeola
          Nov 15 '18 at 13:43





          Thank you very much, My problem was not using the b'' notation and adding initial vector value again I suppose.

          – erdimeola
          Nov 15 '18 at 13:43




          1




          1





          you are welcome! good luck with pycrypto!

          – hiro protagonist
          Nov 15 '18 at 13:44





          you are welcome! good luck with pycrypto!

          – hiro protagonist
          Nov 15 '18 at 13:44



















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320143%2fpycryptodome-aes-cbc-encryption-does-not-give-desired-output%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

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus