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
furas
  • 134,197
  • 12
  • 106
  • 148
Rahul rey
  • 11
  • 1
  • 3
    Does this answer your question? [UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c](https://stackoverflow.com/questions/12468179/unicodedecodeerror-utf8-codec-cant-decode-byte-0x9c) – Vedant Matanhelia May 26 '21 at 07:39
  • first you could check what encoding has char with code `0x95` - probably it is `Latin1`, `cp1250` or `ISO-8859` – furas May 26 '21 at 08:27
  • `0x95` for `cp1250` it gives me `•` - see `b'\x95'.decode('cp1250')` - so you have data in `cp1250` and try `cipher_text.decode("cp1250")` – furas May 26 '21 at 08: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. – President James K. Polk May 26 '21 at 11:37

0 Answers0