Encode in Laravel, decode in Python

后端 未结 3 1015
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 16:56

I\'m using Laravel\'s encryptString method to encrypt some data on my website. This uses OpenSSL\'s 256-bit AES-CBC encryption without any serialization. I\'m now trying to decr

3条回答
  •  无人共我
    2021-01-23 17:22

    Question: ...trying to decrypt that data in Python but I keep getting errors about key length

    I can use your key, in the code of the linked answer, after doing .b64decode(....
    The example code .encode(... and decode(... works as expecte.
    Conclusion: There is nothing wrong, with your Key!

    key = b"/AZejP0lh3McL/+Vy5yZcADdTcR65qnx5Jqinuw7raK="
    key = base64.b64decode(key)
    

    But with your code, I got TypeError, related to the IV parameter:

      expect_byte_string(iv)
    File "/usr/local/lib/python3.4/dist-packages/Crypto/Util/_raw_api.py", line 172, in expect_byte_string
      TypeError: Only byte strings can be passed to C code
    

    Fixed with IV = 16 * '\x00'.encode(), results in ValueError, related to enc:

      data = decobj.decrypt(base64.b64decode(enc))
    File "/usr/local/lib/python3.4/dist-packages/Crypto/Cipher/_mode_cbc.py", line 209, in decrypt
      ValueError: Error 3 while decrypting in CBC mode
    

    This leads to github issues:10

    Error 3 means "ERR_NOT_ENOUGH_DATA"

    According to the linked GitHub page, you have to reread the documentation, about padding data while you are encoding.


    Working example from GitHub:

    import base64
    from Crypto.Cipher import AES
    from Crypto.Util.Padding import pad, unpad
    
    key = b"/AZejP0lh3McL/+Vy5yZcADdTcR65qnx5Jqinuw7raK="
    key = base64.b64decode(key)
    
    BLOCK_SIZE = 32
    encryption_suite = AES.new(key, AES.MODE_CBC, b'This is an IV...')
    cipher_text = encryption_suite.encrypt(pad(b'A really secret message...', BLOCK_SIZE))
    
    decryption_suite = AES.new(key, AES.MODE_CBC, b'This is an IV...')
    print(unpad(decryption_suite.decrypt(cipher_text), BLOCK_SIZE).decode())
    >>> A really secret message...
    

    Tested with Python: 3.4.2

提交回复
热议问题