0

First post here. It is also my first time on coding on Python.

I really need help for my RSA encryption python project.

My decryption shows my wrong key error if I turn the decryption into a function. (Error: wrong key?) But when i join the decrypt function into my encrypt function, it works and decrypts the message that i have been encrypted.

Can I ask why? (I'm running it on Linux Ubuntu)

 import os
 import M2Crypto

def encrypt():

   pubkey = (raw_input('Enter choosen public key:'))
   loadpub = M2Crypto.RSA.load_pub_key (pubkey + '-public.pem')

   encrypt = (raw_input('Enter message to decrypt:'))
   CipherText = loadpub.public_encrypt (encrypt, M2Crypto.RSA.pkcs1_oaep_padding)

   print "Encrypted message:"
   print CipherText.encode ('base64')
   f = open ('encryption.txt', 'w')
   f.write(str(CipherText.encode ('base64'))) #write ciphertext to file
   f.close()

def decrypt():
   privkey = (raw_input('Enter choosen private key:'))
   loadprivkey = M2Crypto.RSA.load_key (privkey + '-private.pem')

   try:
    PlainText = loadprivkey.private_decrypt (CipherText, M2Crypto.RSA.pkcs1_oaep_padding)
   except:
    print "Error: wrong key?"
    PlainText = ""

   if PlainText != "":
    print "Message decrypted by " + privkey + " :"
    print PlainText

 def first():
  print "Press 1 for encryption."
  print "Press 2 for decryption."
  qwe = (raw_input(''))
  if qwe == '1':
    encrypt()
    first()
  elif qwe == '2':
    decrypt()
    first()
  else: 
    print "Please enter a correct number"
    first()

  if __name__ == '__main__':
    first()
daniele3004
  • 13,072
  • 12
  • 67
  • 75
  • Can you confirm that your indentation in the code above is correct, and if not, fix it? In python, that could have a big effect. – roelofs Nov 22 '17 at 15:26
  • There also seems to be some code missing at the end. – roelofs Nov 22 '17 at 15:28
  • @roelofs It is correct btw. Newbie post here in Stackoverflow so I didn't know it would be messed up. My codes are working if run the script in the terminal. And i've edited the last parenthesis part too. Sorry. :) – Bernard1009 Nov 22 '17 at 15:33
  • Ok, but can you check the formatting in the question itself. The calls to `first()` and `decrupt()` near the end seem wrongly indented. – roelofs Nov 22 '17 at 15:35
  • @roelofs man this is embarassing. Haha. I've edited it now again. Sorry for mistakes. :) – Bernard1009 Nov 22 '17 at 15:39
  • I see you have a generic `catch` in `decrypt()`. Perhaps print the exception message (do a `Except exception` and then `print exception.message`), and confirm the error. – roelofs Nov 22 '17 at 15:56

1 Answers1

1

In your new decrypt() function, CipherText becomes a new variable. You need to reload the contents of the file you wrote to in encrypt() into CipherText.

Previously, the variable would've still contained the data from your encryption process (when encryption and decryption were performed in the same function).

roelofs
  • 2,132
  • 20
  • 25
  • Hello again! Sorry for the late reply. I've tried to search what to do to reload the contents of variable to other variable, and it works! Thank you! Link to the solution that I have used btw (https://stackoverflow.com/questions/10139866/calling-variable-defined-inside-one-function-from-another-function) – Bernard1009 Nov 23 '17 at 12:37
  • Just opening the file and reading it again (what you would do in a real life setup) would do :) – roelofs Nov 23 '17 at 22:03