在Laravel中编码,在Python中解码 [英] Encode in Laravel, decode in Python

查看:186
本文介绍了在Laravel中编码,在Python中解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel的 encryptString 方法来加密我网站上的某些数据.它使用OpenSSL的256位AES-CBC加密,而无需任何序列化.我现在正在尝试用Python解密这些数据,但是我不断收到有关密钥长度的错误,而且似乎无法弄清原因.

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 decrypt that data in Python but I keep getting errors about key length and can't seem to figure out why.

Example data to decrypt: eyJpdiI6ImdxY0VcLzFodmpISFV4allSWmJDdEpRPT0iLCJ2YWx1ZSI6IkxXd0ZJaUd2bTUweW5pNm0wUjQwOFM2N1wvWEs5SlYrNB4xNlR7Qkh1U3FvPSIsIm1hYyI6Ijc5ZWM0YTYxYjljZGFiNzgwNjY2NDU1ZmQ5Yjc1ZmJlOGU4NzBkMjQzMzA3MmVhYzE3NzY4ZmU1MWIyMjZlOTQifQ==

Example Key to use for decryption (from laravel .env):
base64:/AZejP0lh3McL/+Vy5yZcADdTcR65qnx5Jqinuw7raK=

我更改了这些值,因此使用这些值进行实际解密将不会提供任何真实数据,只是认为这会很好.然后,我尝试使用以下命令在Python 3.7中解密此数据:

I changed those values around so actually decrypting with those values won't give any real data, just figured it'd be good for example. I then try to decrypt this data in Python 3.7 with:

import base64
from Crypto.Cipher import AES

def decrypt(enc, key):
    IV = 16 * '\x00'
    decobj = AES.new(key, AES.MODE_CBC, IV)
    data = decobj.decrypt(base64.b64decode(enc))
    print(str(data.decode()))

if __name__ == "__main__":
    key = b"/AZejP0lh3McL/+Vy5yZcADdTcR65qnx5Jqinuw7raK="
    decrypt("eyJpdiI6ImdxY0VcLzFodmpISFV4allSWmJDdEpRPT0iLCJ2YWx1ZSI6IkxXd0ZJaUd2bTUweW5pNm0wUjQwOFM2N1wvWEs5SlYrNB4xNlR7Qkh1U3FvPSIsIm1hYyI6Ijc5ZWM0YTYxYjljZGFiNzgwNjY2NDU1ZmQ5Yjc1ZmJlOGU4NzBkMjQzMzA3MmVhYzE3NzY4ZmU1MWIyMjZlOTQifQ==", key)

看来这应该可行,但是当我运行它时,出现错误:ValueError: Incorrect AES key length (60 bytes),所以我不确定自己在做什么错.我已经尝试过填充/取消填充数据/密钥,但这似乎并没有改变任何东西.我想知道从Laravel解密时是否使用了错误的密钥,但是根据链接文档中的信息,它应该只是我的.env文件中的APP_KEY.

And it seems like this should work, but when I run it I get the error: ValueError: Incorrect AES key length (60 bytes) so I'm not sure what I'm doing wrong. I've tried padding/unpadding the data/key but that doesn't seem to change anything. I'm wondering if I'm getting the wrong key to use for decryption from Laravel, but from what I could tell in the linked documentation, it should just be the APP_KEY in my .env file.

如果有人可以帮助我或为我指明正确的方向,那就太好了!

If anyone could help me or point me in the right direction, that would be amazing!

这个问题是其他类似问题所独有的,因为我主要是想弄清楚是否从Laravel获得了正确的AES密钥,实际上我并不需要过多的解密帮助,我只是认为我正在抓住Laravel提供了错误的密钥.

This question is unique to other similar questions because I'm trying to figure out primarily if I'm getting the correct AES key from Laravel, I don't actually overly need help decrypting, I just think I'm grabbing the wrong key from Laravel.

看起来可行的新代码:

import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

def decrypt(enc, key):
    IV = 16 * '\x00'.encode()
    decobj = AES.new(key, AES.MODE_CBC, IV)
    data = decobj.decrypt(pad(base64.b64decode(enc), 16))
    print(base64.b64decode(data))

if __name__ == "__main__":
    key = base64.b64decode(b"/AZejP0lh3McL/+Vy5yZcADdTcR65qnx5Jqinuw7raK=")
    decrypt("eyJpdiI6ImdxY0VcLzFodmpISFV4allSWmJDdEpRPT0iLCJ2YWx1ZSI6IkxXd0ZJaUd2bTUweW5pNm0wUjQwOFM2N1wvWEs5SlYrNB4xNlR7Qkh1U3FvPSIsIm1hYyI6Ijc5ZWM0YTYxYjljZGFiNzgwNjY2NDU1ZmQ5Yjc1ZmJlOGU4NzBkMjQzMzA3MmVhYzE3NzY4ZmU1MWIyMjZlOTQifQ==", key)

现在print语句会打印一些字节,但是当我在其上运行.decode()时,会出现错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfa in position 0: invalid start byte,似乎无法弄清楚我需要做些什么才能使其能够被打印作为字符串.

The print statement now prints some bytes, but when I run .decode() on it I get the error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfa in position 0: invalid start byte and can't seem to figure out what I need to do to make it be able to be printed as a string.

推荐答案

问题:...试图在Python中解密该数据,但我不断收到有关密钥长度的错误

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

完成.b64decode(...之后,我可以在链接答案的代码中使用您的key.
示例代码.encode(...decode(...可以正常工作.
结论:您的密钥没有错!

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)


但是使用您的代码,我得到了与IV参数相关的 TypeError :

  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

已修复IV = 16 * '\x00'.encode(),导致出现 ValueError ,与enc相关:

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

这会导致 github问题:10

错误3表示"ERR_NOT_ENOUGH_DATA"

根据链接的GitHub页面,您必须重新阅读文档,关于填充数据,同时您进行编码.

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

来自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...

使用Python测试:3.4.2

Tested with Python: 3.4.2

这篇关于在Laravel中编码,在Python中解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆