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
Python 3.5 error 'Only byte strings can be passed to C code' #35
Comments
The error is generated on a certain line of your program, with a call to a method in pycryptodome. Could you share the type of all the parameters you pass there, plus the name of the method? |
Unfortunately, I uninstalled pycryptodome. I use the Python Anaconda distribution. In this case, which is the preferred install method: drop-in replacement or pycryptodomex? I don't use a virtualenv. First, I uninstalled pycrypto and then installed pycryptodome but got the error. Next, I tried installing pycryptodomex by first uninstalling pycrypto but got the same error. Has pycryptodome passed your tests under python 3.5x? |
Would you mind sharing the code you use? |
from Crypto.Cipher import AES key = bytes.fromhex("0123456789abcdef0123456789abcdef") text = 'j' * 64 + 'i' * 128 File "C:/GIT/indoor-testfw-wired/network/delete.py", line 6, in Any idea how to fix it? Like no example found on the internet how to overcome this issue... Thanks |
instead of
This applies for all data (keys, ivs, data) that are passed to pycrypto(dome) while using Python 3, but not with Python 2. You can also predefine bytestrings: One final note on latin-1 encoding: This is for 1:1 encoding from string to bytes. If unicode content (like üä, etc.) are used the must be preserved use utf-8. Please pad after encoding with utf-8 because the encoded size may be bigger than the original string as one character sometimes does not equal to one byte. |
This also happens on my machine with Python 3.6 if I try to run basic examples like: |
As mentioned earlier, in that example all strings should be prefixed with
instead of:
|
or
Example:
|
#!/usr/bin/env python BS = 16
ts = time.clock() ser = np.array_str(image); print(time.clock()-ts) Please help! the code is showing the following error : TypeError: can't concat bytes to str |
@shivanshuIITR This errors says that somewhere in your code you "mix" normal text (str) with text encoded to bytes which is NOT possible. If you want help, would you mind |
I have a similar error when trying to use the code below with pycryptodome instead of pycrypto: from Crypto.Cipher import AES class colors(object): class Forensics(object):
When I run this it gives the following error: Traceback (most recent call last): Could someone please tell me where I am going wrong? |
@ShyavanS You pass a string to the cipher instead of bytes. So instead of
just use
This is standard Python 3 behaviour, PyCrypto was just a bit more "forgiving" and did this encoding "under the hood". |
Thank you so much! It started to work but now I am getting this error: Traceback (most recent call last): Do you mind helping me out with this as well? |
I think it should be You also need to encode As I said above: If you use Python 3 all data passed to a cipher object (including the key) must be bytes. This applies for plain- and ciphertexts, too. If you need to "revert" the encoding, use |
Thank you very much! The program works completely now! |
pycrypto at https://github.com/sfbahr/PyCrypto-Wheels hasn't been updated in three years and won't install on windows pycryptodome is a "drop in replacement" for pycrypto, but throws errors on this script. changing the KEY and IV strings to b'<string>' was listed as a solution here: Legrandin/pycryptodome#35
This stackoverflow question gave a simple solution : append Works on my end on python3, using |
pycrypto at https://github.com/sfbahr/PyCrypto-Wheels hasn't been updated in three years and won't install on windows pycryptodome is a "drop in replacement" for pycrypto, but throws errors on this script. changing the KEY and IV strings to b'<string>' was listed as a solution here: Legrandin/pycryptodome#35
On python 3.5, I replaced pycrypto with pycryptodome and got the error 'Only byte strings can be passed to C code' when running my program which previously worked with pycrypto.
Is there a solution?
The text was updated successfully, but these errors were encountered: