Closed tchauvel closed 4 years ago
What error are you getting? Include the trace back. Where did these keys come from? Are you sure that the two keys match?
@tchauvel
New test source code:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
DIR = "/mnt/elkdata/linux-home-folder/projects/crypto/keys/"
def encryption(arg_publickey, arg_cleartext):
encryptor = PKCS1_OAEP.new(arg_publickey)
ciphertext = encryptor.encrypt(arg_cleartext)
return base64.b64encode(ciphertext)
def decryption(arg_privatekey, arg_b64text):
decoded_data = base64.b64decode(arg_b64text)
decryptor = PKCS1_OAEP.new(arg_privatekey)
decrypted = decryptor.decrypt(decoded_data)
return decrypted
PUBKEY = DIR + "issue_424.pubkey"
PRIVKEY = DIR + "issue_424.privkey"
f_pubkey = open(PUBKEY, 'rb')
publickey = RSA.importKey(f_pubkey.read())
f_privkey = open(PRIVKEY, 'rb')
privatekey = RSA.importKey(f_privkey.read())
cleartext1 = b'Text to Encrypt'
ciphertext = encryption(publickey, cleartext1)
cleartext2 = decryption(privatekey, ciphertext)
print(cleartext1)
print(cleartext2)
I used your supplied public and private key which are indeed consistent.
Run my program yourself. You will see that cleartext1 = cleartext2.
Your original test program was confused. Please desk check it.
Mine is consistent about cryptography and the encoding scheme.
Please close this (non)-issue.
Thank you.
Hi,
I trying to run 2 separate code and I'm getting that error during the decrypting phase:
Encryption:
`from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP import base64 import binascii
f = open('C:\Users\Documents\keys\public.pem', 'rb') publickey = RSA.importKey(f.read())
msg = b'Text to Encrypt' encryptor = PKCS1_OAEP.new(publickey) encrypted = encryptor.encrypt(msg)
encryptedtext = binascii.hexlify(encrypted) encryptedstring = encryptedtext.decode("utf-8")
ret_dict = {} ret_dict["encryptedtext"] = encryptedstring ret_dict["returncode"] = 0 return ret_dict`
Decryption code:
`from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP import base64 import binascii
f1 = open('C:\Users\Documents\keys\private.pem', 'rb') privatekey = RSA.importKey(f1.read())
encrypted = d["encryptedtext"] decode_data = base64.b64decode(encrypted)
decryptor = PKCS1_OAEP.new(privatekey) decrypted = decryptor.decrypt(encrypted)
print('Decrypted:', decrypted) decryptedtext = base64.b64encode(decrypted)
ret_dict = {} ret_dict["encryptedtext"] = decryptedtext ret_dict["returncode"] = 0 # set to 1 to output on the FAILURE output return ret_dict `
I can only save my ciphertext as a string and use it in the decryption code.
For that exemple the keys are:
Public PEM:
-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCovr+APx7w0zsC2gg4bbLYUCDu ndx4zcPvsnOMChlcFVUtUunU2CzeSf++pVn8KedZy6nEidrI90nASlVaCPH8xuGm mzvx2EZ5pnqiy4vWRg1/BYBQgyEg+ot1wuqp642DHxTg1knyhgAyOtkwcOisD97b istwkderBnL5TRXhOwIDAQAB -----END PUBLIC KEY-----
Private PEM:
-----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQCovr+APx7w0zsC2gg4bbLYUCDundx4zcPvsnOMChlcFVUtUunU 2CzeSf++pVn8KedZy6nEidrI90nASlVaCPH8xuGmmzvx2EZ5pnqiy4vWRg1/BYBQ gyEg+ot1wuqp642DHxTg1knyhgAyOtkwcOisD97bistwkderBnL5TRXhOwIDAQAB AoGAKctU0wbPOwaWTxaBgrho23q9LVycBq+wH+YrCDxYG56NAjTPBZClTEk3tpht uS8Hxg3TdoJOVggplB7VFP+yL2p8rQmEZCCOso2KxKZALxZAcVKOKr28D+Ug/gr6 nJjEPP0CcW+Uu1jeA7a1KWk4DrwwHR/H55udgOZOdc115QECQQDN7Jsy9wBbkx5y 3hV221Y4f4Pdryp4Jkxr//Q7ESSZWejTJuiI4iymDCdigni3w3O94ijJ/VkvM598 1TaVR45RAkEA0cehc9q+sEKCwJYVM03geM6WTEnPban+DFKt4UtVUrFuHgMw68c8 5Di7qWO2G8me9MPmnGN8pQ1ThVYoOTLXywJBALA/PmZUef3m4Ty3T++2swkQOrDW Z6cGQIp/O24Csix+g4OV3Ziq3EQSSvNJcED4DGwM0ZRwPpDjwjF524nRAMECQQCu HW6BFU7uRtX1eNwxz/J+8qDGwwB6efyVR85wTzGLSB8oo+dn2t9MYVZJGqyc1bSs XSi8Jsrex3fKFJ+gEfYPAkBqYmSg+52DzwWuYkkjlx8OAwoQdArNKDHp2ancRGu8 DjI9ped8ub5o7f4cDvATDBLVXPt0qRw3wA3slsVBMVZS -----END RSA PRIVATE KEY-----