rafatic
rafatic

Reputation: 223

RSA : decrypt openSSL certificate

I am trying to decrypt an encrypted openSSL certificate using the Crypto library. For that, I use the following function :

def decryptMessage(privateKeyString, encryptedMessage):

    print 'Enter private key pass phrase'
    passPhrase = raw_input()

    privateKey= RSA.importKey(privateKeyString, passPhrase)

    return privateKey.decrypt(ast.literal_eval(str(encryptedMessage)))

This function works fine with small strings such as foo or lorem ipsum. However, when I try to decrypt a larger string such as an OpenSSL certificate, the decryption fails and returns this

#«$,^5ô¦┬ {¯9 ██─╚áW¸ÍÀóÂ╗ö▓├ô{Òv&s´À;d▒§= I×òòòÿÞ:Mu▄ñ├Zc╬a╣fÙ╚g╝z¯¹þÞÖ*└²}?õÑ:~Ì ôı*▓açõ─░3Ñz{³é├ p}7Ĭ/tıN®╣¥‗Pzô£▄¤▄╩Ý,æQ'mfî.«¯┴C%tÏ­ýõ/ñlÚ0╗ò¼(Ï5▓ø5Ì└ûƒuƒä£█ÂF=)─y@O~§LßÆ▄Ð░mËÅ9Uwõh▀Û/▓Ï,APð HѪm■Îç¼"§Ô,XvÓÏÄÃM■▓v╠@örÂùuE­

Thus, I'd like to know how my function fails to decrypt bigger strings.

Upvotes: 0

Views: 354

Answers (1)

Alfe
Alfe

Reputation: 59416

RSA is for en/decrypting small chunks of data, e. g. a random number which is then used for en/decrypting the communication using a block cipher like AES. Large chunks of data are simply not supported by asymmetric encryption algorithms like RSA.

Your approach seems to lack the knowledge needed for doing encryption right. This is very dangerous. I propose to have a look at the "cryptography" Python package which contains everything you will need: https://pypi.python.org/pypi/cryptography

There are also some very nice Youtube videos of presentations before an audience the developers of this package made in which they explain how to use what and how they develop cryptography.

Upvotes: 1

Related Questions