1

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
4
  • 3
    Does this answer your question? UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c Commented May 26, 2021 at 7:39
  • first you could check what encoding has char with code 0x95 - probably it is Latin1, cp1250 or ISO-8859 Commented May 26, 2021 at 8:27
  • 0x95 for cp1250 it gives me - see b'\x95'.decode('cp1250') - so you have data in cp1250 and try cipher_text.decode("cp1250") Commented May 26, 2021 at 8:29
  • 1
    Calling 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. Commented May 26, 2021 at 11:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.