Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value Error: Invalid data #16

Closed
gakar06 opened this issue Feb 2, 2018 · 6 comments
Closed

Value Error: Invalid data #16

gakar06 opened this issue Feb 2, 2018 · 6 comments

Comments

@gakar06
Copy link

gakar06 commented Feb 2, 2018

When I try to use this script on data I get the following error:

ValueError : Invalid data, illegal PKCS#7 padding. Could be using a wrong key.

What does the format need to be of the key and data coming into the xxtea.decrypt function? As of right now, the key is 16 bytes and the data is 256 bytes.

Thanks,
Chris

@ifduyue
Copy link
Owner

ifduyue commented Feb 2, 2018

@gakar06 Hello, did you use this lib to encrypt and decrypt? This xxtea lib may not be compatible with other ones.

@ifduyue
Copy link
Owner

ifduyue commented Feb 2, 2018

What does the format need to be of the key and data coming into the xxtea.decrypt function? As of right now, the key is 16 bytes and the data is 256 bytes.

key needs to be 16-byte long, data can be of any length.
Could you provide a minimal script to reproduce the problem?

@gakar06
Copy link
Author

gakar06 commented Feb 2, 2018

What is the purpose of doing the PKCS#7 padding and removal in the XXTEA code? Just curious. The issue seems to occurring within xxtea.c in the xxtea_decrypt function. At the bottom, the ELSE of

if (rc >= 0) {
/* Remove PKCS#7 padded chars /
Py_SIZE(retval) = rc;
}
else {
/
Illegal PKCS#7 padding */
PyErr_SetString(PyExc_ValueError, "Invalid data, illegal PKCS#7 padding. Could be using a wrong key.");
goto cleanup;
}

If I comment out the ELSE, the decrypt works. Any ideas why? What is the PKCS used for?

Thanks!
Chris

@ifduyue
Copy link
Owner

ifduyue commented Feb 2, 2018

It's because XXTEA algorithm works on two or more 32-bit integers.
If data length is not a multiple of 32-bit, we have to add paddings, and remove these paddings at the decryption.

@ifduyue
Copy link
Owner

ifduyue commented Feb 2, 2018

xxtea 0.1.5 doesn't add paddings, let's see the problem:

pip install xxtea==0.1.5

>>> import os
>>> import xxtea
>>> key = os.urandom(16)
>>> xxtea.encrypt('\0'*3, key)
'1aa3918940169fe3'
>>> xxtea.decrypt(_, key)
''

@ifduyue
Copy link
Owner

ifduyue commented May 9, 2018

Going to close this right now. Feel free to reopen it if there are any problems

@ifduyue ifduyue closed this as completed May 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants