this is the duction where datajs is a json
def encrypt(datajs, pub_key):
keyPub = RSA.importKey(pub_key) # import the public key
cipher = Cipher_PKCS1_v1_5.new(keyPub)
# print(cipher.encrypt.__doc__)
cipher_text = cipher.encrypt(datajs.encode()) # now we have the cipher
cipher_text = cipher_text.decode()
print(type(cipher_text))
print(cipher_text)
return cipher_text
this is the error showing
Traceback (most recent call last):
File "C:\Users\rahul.cs\PycharmProjects\pythonProject3\main.py", line 106, in <module>
encrypted = encrypt(datajs, pubkey)
File "C:\Users\rahul.cs\PycharmProjects\pythonProject3\main.py", line 63, in encrypt
cipher_text = cipher_text.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x95 in position 0: invalid start byte
0x95- probably it isLatin1,cp1250orISO-88590x95forcp1250it gives me•- seeb'\x95'.decode('cp1250')- so you have data incp1250and trycipher_text.decode("cp1250")decode()on that bytes object makes no sense and the error is a reflection of that fact. The result of encryption is a sequence of arbitrary bytes, not the encoding of a string. If you must convert the result to a string the standard method is to use base64 encoding. Depending in the specific use case you may want to use url-safe base64 encoding.