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 Error: TypeError: write() argument must be str, not bytes
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- Python TypeError: Unrecognized value type: <class ‘str‘>dateutil.parser._parser.ParserError: Unknow
- Typeerror in Python regular expression: expected string or bytes like object
- [Solved] TypeError: Object of type ‘bytes’ is not JSON serializable
- Windows10 DOTA_devkit Error: TypeError: ‘>=‘ not supported between instances of ‘NoneType‘ and ‘str‘
- [Solved] TypeError: unsupported operand type(s) for /: ‘str‘ and ‘int‘
- [Solved] pycocotools Install Error: ERROR: Error expected str, bytes or os.PathLike object, not NoneType while executing
- Python 3.7 Error: AttributeError: ‘str‘ object has no attribute ‘decode‘ [How to Solve]
- Python: ___ () vs ___ str__()
- Djangorestframework-simplejwt: ‘str‘ object has no attribute ‘decode‘ [Solved]
- Python Error: SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3:
- Python Pandas Typeerror: invalid type comparison
- Python TypeError: not all arguments converted during string formatting [Solved]
- Python TypeError: coercing to Unicode: need string or buffer, NoneType found
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- [How to Solve] Python TypeError: ‘int‘ object is not subscriptable
- [resolution] str.contains() problem] valueerror: cannot index with vector containing Na/Nan values
- Python Run Error: TypeError: hog() got an unexpected keyword argument ‘visualise‘”