0

Below is my code:

from base64 import b64encode, b64decode
from pathlib import Path
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

def decrypt(base64_ciphertext):
    private_key = RSA.import_key(open('fkey.key').read())
    ciphertext = b64decode(base64_ciphertext)
    cipher_rsa = PKCS1_v1_5.new(private_key)
    data = cipher_rsa.decrypt(ast.literal_eval(str(ciphertext)),'Error occured')
    return data

The value returned is Error occurred every time I run this function. Please help. I have confirmed that the ciphertext being passed is a byte string by logging it. Is there any way to log the actual error due to which decrypt fails instead of returning the sentinel value.

EDIT: It was a mistake from the company providing the API, they changed our credentials and did not inform us, sorry for the trouble and thanks for the help.

  • The problem is hard to identify from the posted code snippet. It could perhaps be the key or the ciphertext. Therefore some _test_ data would be helpful: Private and public key, plaintext and ciphertext. It is a bit striking that you call the key `recipient_key`, because for decryption the _own_ private key is used. The recipient's public key is applied for encryption. Perhaps you have confused something here. – Topaco Aug 30 '21 at 15:59
  • recipient_key is bad naming on my part I am sorry, I will change that, the private is working because I am using a similar snippet somewhere else, the data is coming from an API so I don't have the plaintext, I will try to share the cipher text as soon I get the chance, thank you. – tomoya sensei Aug 30 '21 at 16:15
  • On my machine the posted code works using my data (private key, associated public key and ciphertext generated with the public key). So most likely a problem with your data (private/public keys don't match, ciphertext was generated with different public key, different padding on encryption etc.). – Topaco Aug 31 '21 at 15:02
  • Thank you for confirming. Knowing that I can contact the support for the API, and I will update here as soon as I resolve the issue – tomoya sensei Sep 02 '21 at 03:57

0 Answers0