Black Hat Python — Encrypt and Decrypt with RSA Cryptography

Ismail Akkila
Dec 21, 2017 · 2 min read

Its very straighforward to encrypt/ decrypt files using Python. In this post, I will show a few scripts to accomplish this.

Image for post
Image for post

Generate a RSA private/ public key pair

Private Key:-----BEGIN RSA PRIVATE KEY-----MIIJKgIBAAKCAgEAqaH+h0vMyIeRN0LwJKt5Dhu+1HNxWMEcGqEl+pFJrCZpMpaqp62b16ias3TupUHL/Jd/V594TyPDytY8kLP+asw0wr3RwnOdKjzHqdBQz96sAgKFuss6HZW9KEjLcyFTWOgDqvapw163BRfGUj3tpoumGXYkmV3gQ0NaPVR/CLTfPuY2WzMX0hSwu18lt2R2/ogzLcxUlwiSsFfQJZoRUUi0rztivrvpxKXpeQNNtOtRlZzDjsvh+VNDIlwpPY4hExtAiGOGVQR5Wqn9Me4ynWTY/QpUfdVhtaRPVZqZ/EPrkGpMq9Ua5SbU+0cWNdEvN2Lu0PPrxEzEUg/8Ewlm/LE0YklaT7RpkOwJK2H9iJgCZff+JlOb/ZdoC2qDJv6hFB0XFH7VQUfBlS0qFHwnyJAxnDuJTNT9fPwb0+kuIoZjA6tLtPuvxDMCvlnZOdO3aZWW0OYlvZNuTzRk62o7tEjvGU2usedSfvf/jYO3vAM9dInus/D/4R4SQmI8Z889bga3LJlZeozbIwRPXhR6wuwCk3JfMtzAWPnl4jw5ZO86t2XI5+k+guCXx25p0t3KOFMo4mGcmw1miacsnPhDxcsofdEckZt/u7OfBDJMM3Jn1fCFyMpOV31i5OCNTDmTFQfA6o2iavkRUYru84qLiYrg27e8FZzfotraHdQKVAsCAwEAAQKCAgEAqGqQVW2ZfMK0F4Sa/h1MzAgZ975CG9dmer3iAhs7ZPKh4TsbZe0E3CGmrmN7CJIB39/d63YFsbUVkA4SBaze/NOP0oU5e/Djamr75QpRIYC3kCtG8IUTRwpCynszmzPMF2oLMjNBqxofZawMF90qVP8zQvY8MhS1MrLtJhT9JvzE5SMVdoiMruQh73Xw8VqKlf1NqrvChE1brj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l6DA95eivzwr+0v5BhfGY0zElDl6HjCK45dw==-----END RSA PRIVATE KEY-----Public Key:-----BEGIN PUBLIC KEY-----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqaH+h0vMyIeRN0LwJKt5Dhu+1HNxWMEcGqEl+pFJrCZpMpaqp62b16ias3TupUHL/Jd/V594TyPDytY8kLP+asw0wr3RwnOdKjzHqdBQz96sAgKFuss6HZW9KEjLcyFTWOgDqvapw163BRfGUj3tpoumGXYkmV3gQ0NaPVR/CLTfPuY2WzMX0hSwu18lt2R2/ogzLcxUlwiSsFfQJZoRUUi0rztivrvpxKXpeQNNtOtRlZzDjsvh+VNDIlwpPY4hExtAiGOGVQR5Wqn9Me4ynWTY/QpUfdVhtaRPVZqZ/EPrkGpMq9Ua5SbU+0cWNdEvN2Lu0PPrxEzEUg/8Ewlm/LE0YklaT7RpkOwJK2H9iJgCZff+JlOb/ZdoC2qDJv6hFB0XFH7VQUfBlS0qFHwnyJAxnDuJTNT9fPwb0+kuIoZjA6tLtPuvxDMCvlnZOdO3aZWW0OYlvZNuTzRk62o7tEjvGU2usedSfvf/jYO3vAM9dInus/D/4R4SQmI8Z889bga3LJlZeozbIwRPXhR6wuwCk3JfMtzAWPnl4jw5ZO86t2XI5+k+guCXx25p0t3KOFMo4mGcmw1miacsnPhDxcsofdEckZt/u7OfBDJMM3Jn1fCFyMpOV31i5OCNTDmTFQfA6o2iavkRUYru84qLiYrg27e8FZzfotraHdQKVAsCAwEAAQ==-----END PUBLIC KEY-----

Encrypting our file using the public key

Image for post
Image for post
MD5 (img.jpg) = 33a76ad3ce6633960ce300db2e625bdb

and this encryption script:

The resulting encrypted file:

MD5 (encrypted_img.jpg) = 595c7c94f4f80fc02a67f325f034fc22iFVwskJ4974C+5NuQg2lbbjuPOjoAsub1pb7Tcc0Yhtu/1oxIIbMLUMMdOC0Bu+d32Y8OQ7YxICggtHRgjLiafyG0c9WPCdn6Ay/mZ4qBVD1AYSUfGxta6C6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McFz8x1GxN7DoRZoNWYSCdJpfbz3B1omwUHi2qaWERDolTGE3n550LSfhl/vzP/3Z/D3nmfWe5rnDmtFWLvPS+OpE1vRpBAh69ZgZk8K7bxTKnyoD29TbLQjwd1GN14a/ITptei88eltiq4bd9wNBm9y88XUk8BQ6NJZC3mbeGJz8m3fSKzoPom3xLg3slHfgLBlCwxgS1IIruihvrrm4wsxc+qYN8sAAR0
------------------------------TRUNCATED-----------------------------

Decrypting our file using the private key

MD5 (img.jpg) = 33a76ad3ce6633960ce300db2e625bdb
MD5 (decrypted_img.jpg) = 33a76ad3ce6633960ce300db2e625bdb

Hope this helps someone!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store