Monday, July 15, 2013

Trying out: Python's PyCrypto RSA Module

PyCrypto is a very good cryptographic library for python. Now, I am currently trying out PyCrypto's RSA module.

What I am currently trying to do is:
1. Alice will generate the public key and private key
2. Alice will then send the public key to Bob
3. Bob will encrypt a message using the public key that Alice sent
4. Alice will then receive the encrypted message
5. Alice will decrypt the encrypted message

Here is the source code.

Here is the output:

Alice will send this file:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHXGhd1bPKNJHQk08inY0N9hNf
F4SQGliy9qBV8IbuQQTvqF2T3Nx3+i2J6YdzCkWC87ec4Se/2KA9R4haj6t/M5by
jsO5RVe0olJ3tYiKEEM/QzB7ybPH7rlSGMfw14MyhZT7aVq2v6Qfdz3KvS62mf86
ARJ4vYXCd7FJOZaTcwIDAQAB
-----END PUBLIC KEY-----

Bob reads the file:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHXGhd1bPKNJHQk08inY0N9hNf
F4SQGliy9qBV8IbuQQTvqF2T3Nx3+i2J6YdzCkWC87ec4Se/2KA9R4haj6t/M5by
jsO5RVe0olJ3tYiKEEM/QzB7ybPH7rlSGMfw14MyhZT7aVq2v6Qfdz3KvS62mf86
ARJ4vYXCd7FJOZaTcwIDAQAB
-----END PUBLIC KEY-----

Alice reads the file with the encrypted message:
"'\x1f\xe3\xcfn\x8f!\x0eA\xa9e\xd4\x91\xd9Dl\x86\xfb\xf2\x1c[p!*\x0f\xb6\xcb\x07\xfa\x1bW$o\x19\x0f\xf8g\xbeM\xf9 \x1aG\x0e\xd7\x9aZ\xd6od\xf3\xf1\x07\xe4\x88\x8c*\xb6\xfb\xe2\xad_\xa4\x0c\xfc\x13\xd5h\x83\x86Vo\x90I\xc62\xa0\x8cMX\xdf\x9b\xe8\x9e\x87\xc3tn#\xa6\x1f\x9f.\xa5b\t4:;\x1bC'\xfe#\xddN\x9fn\x94\x8e\x9d\xe6L\xa9#\xda\x13\xf0k|6\xac2\xa0G\xef\xe6\x89"

Decrypted Message Received by Alice from Bob:
'Alice, will you marry me?'




Reference: http://en.wikipedia.org/wiki/Public-key_cryptography