Black Hat Python — Encrypt and Decrypt with RSA Cryptography

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


Generate a RSA private/ public key pair

We generate the pair using 4096 bits private key length:

Private Key:
-----BEGIN RSA PRIVATE KEY-----
MIIJKgIBAAKCAgEAqaH+h0vMyIeRN0LwJKt5Dhu+1HNxWMEcGqEl+pFJrCZpMpaq
p62b16ias3TupUHL/Jd/V594TyPDytY8kLP+asw0wr3RwnOdKjzHqdBQz96sAgKF
uss6HZW9KEjLcyFTWOgDqvapw163BRfGUj3tpoumGXYkmV3gQ0NaPVR/CLTfPuY2
WzMX0hSwu18lt2R2/ogzLcxUlwiSsFfQJZoRUUi0rztivrvpxKXpeQNNtOtRlZzD
jsvh+VNDIlwpPY4hExtAiGOGVQR5Wqn9Me4ynWTY/QpUfdVhtaRPVZqZ/EPrkGpM
q9Ua5SbU+0cWNdEvN2Lu0PPrxEzEUg/8Ewlm/LE0YklaT7RpkOwJK2H9iJgCZff+
JlOb/ZdoC2qDJv6hFB0XFH7VQUfBlS0qFHwnyJAxnDuJTNT9fPwb0+kuIoZjA6tL
tPuvxDMCvlnZOdO3aZWW0OYlvZNuTzRk62o7tEjvGU2usedSfvf/jYO3vAM9dInu
s/D/4R4SQmI8Z889bga3LJlZeozbIwRPXhR6wuwCk3JfMtzAWPnl4jw5ZO86t2XI
5+k+guCXx25p0t3KOFMo4mGcmw1miacsnPhDxcsofdEckZt/u7OfBDJMM3Jn1fCF
yMpOV31i5OCNTDmTFQfA6o2iavkRUYru84qLiYrg27e8FZzfotraHdQKVAsCAwEA
AQKCAgEAqGqQVW2ZfMK0F4Sa/h1MzAgZ975CG9dmer3iAhs7ZPKh4TsbZe0E3CGm
rmN7CJIB39/d63YFsbUVkA4SBaze/NOP0oU5e/Djamr75QpRIYC3kCtG8IUTRwpC
ynszmzPMF2oLMjNBqxofZawMF90qVP8zQvY8MhS1MrLtJhT9JvzE5SMVdoiMruQh
73Xw8VqKlf1NqrvChE1brj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iFfqNvIRDR
4cr9gJJzUhQ7ZTKXLCKGbA5EYUl6DA95eivzwr+0v5BhfGY0zElDl6HjCK45dw==
-----END RSA PRIVATE KEY-----
Public Key:
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqaH+h0vMyIeRN0LwJKt5
Dhu+1HNxWMEcGqEl+pFJrCZpMpaqp62b16ias3TupUHL/Jd/V594TyPDytY8kLP+
asw0wr3RwnOdKjzHqdBQz96sAgKFuss6HZW9KEjLcyFTWOgDqvapw163BRfGUj3t
poumGXYkmV3gQ0NaPVR/CLTfPuY2WzMX0hSwu18lt2R2/ogzLcxUlwiSsFfQJZoR
UUi0rztivrvpxKXpeQNNtOtRlZzDjsvh+VNDIlwpPY4hExtAiGOGVQR5Wqn9Me4y
nWTY/QpUfdVhtaRPVZqZ/EPrkGpMq9Ua5SbU+0cWNdEvN2Lu0PPrxEzEUg/8Ewlm
/LE0YklaT7RpkOwJK2H9iJgCZff+JlOb/ZdoC2qDJv6hFB0XFH7VQUfBlS0qFHwn
yJAxnDuJTNT9fPwb0+kuIoZjA6tLtPuvxDMCvlnZOdO3aZWW0OYlvZNuTzRk62o7
tEjvGU2usedSfvf/jYO3vAM9dInus/D/4R4SQmI8Z889bga3LJlZeozbIwRPXhR6
wuwCk3JfMtzAWPnl4jw5ZO86t2XI5+k+guCXx25p0t3KOFMo4mGcmw1miacsnPhD
xcsofdEckZt/u7OfBDJMM3Jn1fCFyMpOV31i5OCNTDmTFQfA6o2iavkRUYru84qL
iYrg27e8FZzfotraHdQKVAsCAwEAAQ==
-----END PUBLIC KEY-----

Encrypting our file using the public key

We will be using this image file:

MD5 (img.jpg) = 33a76ad3ce6633960ce300db2e625bdb

and this encryption script:

The resulting encrypted file:

MD5 (encrypted_img.jpg) = 595c7c94f4f80fc02a67f325f034fc22
iFVwskJ4974C+5NuQg2lbbjuPOjoAsub1pb7Tcc0Yhtu/1oxIIbMLUMMdOC0Bu+d32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n02BRibCr95HveU92uh9TTtEv0OJ2DyXIzoD0d0sRLS7AjpizahTo+QedjR64PMcFz8x1GxN7DoRZoNWYSCdJpfbz3B1omwUHi2qaWERDolTGE3n550LSfhl/vzP/3Z/D3nmfWe5rnDmtFWLvPS+OpE1vRpBAh69ZgZk8K7bxTKnyoD29TbLQjwd1GN14a/ITptei88eltiq4bd9wNBm9y88XUk8BQ6NJZC3mbeGJz8m3fSKzoPom3xLg3slHfgLBlCwxgS1IIruihvrrm4wsxc+qYN8sAAR0
------------------------------TRUNCATED-----------------------------

Decrypting our file using the private key

We can use the following decryption script:

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

Hope this helps someone!