0

I have public key, private key and encrypted message ( e, n, d, message). I want to encrypt and decrypt with OpenSSL in Python.

I have keys I don't need to generate them. I saw many questions about RSA but all of them creates keys, by generate method. I haven't seen questions about performing encryption and decryption.

How do I perform RSA encryption and decryption in Python usomg OpenSSL?


What I have is the followings:

#Public key:
e="65537"

n="2483790199491205341506001624338547531741771200963322451" \
  "16318655098567854682220895878748602513720919196015584783557" \
  "704400541915850094004767776687012394493261448676783015273985771" \
  "1238030112386627501698642461625407366533907839206541565912321199" \
  "009791795944233570230631191423356738502486763195167267521973031" \
  "6838578210434343067511636079081818744400113533624136339709745782" \
  "32161853372590090308494113224155565481298018056338822080805188480" \
  "139150684063550507331062187412707210886548924698896783031493679037" \
  "3122088161029787856707927049345768779125257912445784686277424030038" \
  "539380288863347855630618237433032833865316901740219"


#Private key
d="152501997096795110757068525202189319208625862269501399381045003" \
  "01373684948285528219578200125958795897780598922907027278290745917" \
  "4083840545807194541888429655727807270271016523695687179904011971106" \
  "46024638603131783118232131092639581621182826911051011196270811088775" \
  "8622627957416117004996969971673524599345136221501081814180958260506967" \
  "05549363779862358358393233189560520163106785535319492545898745183439" \
  "10980478364023104227720426942196244946117979269924656213962726626606" \
  "77452212629548965644705371048342816305068001182195025882564173365857" \
  "07762540909960941277936950557159506459454566798472128560135656506235" \
  "741389170953"


#encrypted message
encoded="187216163520278606105320112446137004408231369834741341053563682" \
        "277774349916058822189964158715390402738262899525931062389534962" \
        "09104749822344117450601254708536373034264130933521987327974000" \
        "255146756518397668069770185737907343422454676477169144712992560" \
        "738066894543224559303296179944700852861503983647039123452966586" \
        "43024446530008588087574157621730825724439869400851215840977916" \
        "767440706251849931986529460039147463908090086303953826751056882" \
        "5732583473943114017472152320746478960753673137088195122814398113" \
        "5288648561417818449968250721180493107501204327582989947582671" \
        "70231934908068721013345590521202959891172540575563129"
eneski
  • 1,575
  • 17
  • 40
  • You need to show what format your key and message is in and what you've tried. As it stands the question is off topic (for at least 3 different reasons). Besides that, if shown a working option, how would you know it is secure? – Maarten Bodewes Oct 29 '16 at 19:26
  • Well, you seem to be right. I'm getting fodder when searching for Python and `RSA_encrypt` and `RSA_decrypt`. Maybe PyOpenSSL lacks the bindings for the functions. I thought this topic would have a few good posts and answers. – jww Oct 29 '16 at 19:53
  • @jww yeah, actually there is a method called importKey which imports the key from the file and I put my key in the file, but my attempts were failed – eneski Oct 29 '16 at 20:00
  • @MaartenBodewes Thanks for warning, I put what I got (keys and cipher text) but I tried so many things and I can not put them here, it would make the question unreadable, though – eneski Oct 29 '16 at 20:02
  • This is pretty clearly an exercise. It uses "raw" RSA (RSA without padding of any kind). The idea of performing an exercise is to try out things you've learned. What library are you using? Or should you create the routines yourself? If so, where are you stuck? Creating the key, deciphering or decoding? – Maarten Bodewes Oct 29 '16 at 20:40
  • Thanks for the blast from the past, by the way... I instantly recognized the monkey-ness of the answer! – Maarten Bodewes Oct 29 '16 at 21:08
  • @MaartenBodewes Yes, this is an exercise. In the exercise I am supposed to use pyOpenSSL. Where I am stuck is using the library. As I mentioned the question, I can not find appropriate functions to use my datas. I know how RSA works(not deeply). But I can not figure this out this problem. And I did not understand what you mean by monkey-ness? – eneski Oct 29 '16 at 21:16
  • This isn't real RSA encryption. The plaintext isn't padded at all. Anyway, you can decrypt it as follows: `plaintext = pow(int(encoded,10),int(d,10),int(n,10)); print hex(plaintext)[2:-1].decode('hex')` – r3mainer Oct 29 '16 at 21:59
  • 1
    @MaartenBodewes With three arguments, `pow()` *is* a modular operation. – r3mainer Oct 30 '16 at 00:05
  • @squeamishossifrage thank you for your solution. Could you please explain how you solved it. Especially wonder how you understand that the plaintext is not padded at first look. You can put it as an answer maybe some people will need it later – eneski Oct 30 '16 at 01:12
  • @squeamishossifrage Right, all the more reason to make it into an answer :) – Maarten Bodewes Oct 30 '16 at 09:17

1 Answers1

1

The ciphertext can be decoded without using pyOpenSSL or any other external libraries.

Simply reverse the encryption operation using the formula m = cd mod n as follows:

n="24837..."
d="15250..."
encoded="18721..."
plaintext = pow(int(encoded,10),int(d,10),int(n,10))
print hex(plaintext)[2:-1].decode('hex')

The pow() operator performs a modular exponentiation operation to calculate (encoded**d) % n. In the print statement, the resulting value is converted to hexadecimal (removing the first two characters 0x and the last character L), and then decoded as a hexadecimal string to retrieve the original bytes.

Since the message was encoded without using any padding, no further operations are required.

r3mainer
  • 23,981
  • 3
  • 51
  • 88