python3.9 pycropto RSA Error: TypeError: can't concat str to bytes
1. Error reporting
The recent project uses the pycrypto package under python3.9. The following errors occur when using the RSA module, mainly because bytes and STR in python3 cannot be spliced
File "/usr/local/python-3.9.4/lib/python3.9/site-packages/Crypto/PublicKey/RSA.py", line 352, in exportKey
keystring = ''.join([ struct.pack(">I",len(kp))+kp for kp in keyparts])
File "/usr/local/python-3.9.4/lib/python3.9/site-packages/Crypto/PublicKey/RSA.py", line 352, in <listcomp>
keystring = ''.join([ struct.pack(">I",len(kp))+kp for kp in keyparts])
TypeError: can't concat str to bytes
2. Modification
We can check the error file /python3.9/site-packages/crypto/publickey/rsa Py
the main idea is to change the output variable into bytes for splicing. The original part of the file content is modified, line:346:
if format=='OpenSSH':
eb = long_to_bytes(self.e)
nb = long_to_bytes(self.n)
if bord(eb[0]) & 0x80: eb=bchr(0x00)+eb
if bord(nb[0]) & 0x80: nb=bchr(0x00)+nb
keyparts = [ 'ssh-rsa', eb, nb ]
keystring = ''.join([ struct.pack(">I",len(kp))+kp for kp in keyparts])
return 'ssh-rsa '+binascii.b2a_base64(keystring)[:-1]
Modified to:
if format=='OpenSSH':
eb = long_to_bytes(self.e)
nb = long_to_bytes(self.n)
if bord(eb[0]) & 0x80: eb=bchr(0x00)+eb
if bord(nb[0]) & 0x80: nb=bchr(0x00)+nb
keyparts = [ 'ssh-rsa', eb, nb ]
new_list = []
for kp in keyparts:
temp = struct.pack(">I", len(kp))
if isinstance(kp, str):
kp = bytes(kp,encoding='utf-8')
temp += kp
new_list.append(temp)
keystring = b''.join(new_list)
return b'ssh-rsa '+binascii.b2a_base64(keystring)[:-1]
Read More:
- Python PIP TypeError: expected str, bytes or os.PathLike object, not int
- [Solved] python tqdm raise RuntimeError(“cannot join current thread“) RuntimeError: cannot join current thr
- [Solved] Pdfplumber Read PDF Sheet Error: AttributeError: function/symbol ‘ARC4_stream_init‘ not found in library
- [How to Solve] ImportError: No module named typing
- Gunicorn Flask Error: [ERROR] Socket error processing request
- [Solved] Grid Search Error (GridSearchCV): ‘ascii‘ codec can‘t encode characters in position 18-20: ordinal not in r
- Djangorestframework-simplejwt: ‘str‘ object has no attribute ‘decode‘ [Solved]
- [Solved] Error “incorrect padding” in decoding of Base64 module in Python
- Python installs virtualenv through PIP and always reports an error: response.py“, line 438, in _error_catcher yield
- [Solved] ValueError: check_hostname requires server_hostname
- [Solved] This error might have occurred since this system does not have Windows Long Path support enabled.
- Pytorch ValueError: Expected more than 1 value per channel when training, got input size [1, 768
- [Solved] matplotlib.units.ConversionError: Failed to convert value(s) to axis units: ‘LiR‘
- Python TypeError: Unrecognized value type: <class ‘str‘>dateutil.parser._parser.ParserError: Unknow
- How to Solve Error: RuntimeError CUDA out of memory
- [resolution] str.contains() problem] valueerror: cannot index with vector containing Na/Nan values
- [Solved] AttributeError: ‘Manager‘ object has no attribute ‘get_by_natural_key‘
- [Solved] PyInstaller Error: ValueError: too many values to unpack
- Python Valueerror: cannot index with vector containing Na / Nan values
- OSError libespeak.so.1 error: no such file or directory [How to Solve]