Skip to content

Instantly share code, notes, and snippets.

@pybokeh
Last active December 30, 2015 21:59
Show Gist options
  • Save pybokeh/7891691 to your computer and use it in GitHub Desktop.
Save pybokeh/7891691 to your computer and use it in GitHub Desktop.
PyCrypto Example using Python 3
{
"metadata": {
"name": "PyCrypto_Example"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": "# This code taken from youtube example: https://www.youtube.com/watch?v=8PzDfykGg_g&feature=youtube_gdata_player\n# but modified to work with Python 3 due to byte string to string conversion using decode('utf-8') method\n\nfrom Crypto.Cipher import AES # Dependency: install pycrypto - available at pypi: pip install pycrypto\nimport base64\nimport os\nimport codecs # This is needed to convert a string with single backslashes to byte string\n\ndef encryption(privateInfo):\n \"\"\" Method to encrypt your message using AES encryption\"\"\"\n \n BLOCK_SIZE = 16\n PADDING = '{'\n\n pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING\n\n EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))\n\n secret = os.urandom(BLOCK_SIZE) # Comment this line and uncomment line below to use hard-coded key\n #secret = b'\\xf9J\\xa4\\xd1\\t\\x17\\xb8\\xabt\\xfe\\x06\\x96\\xe3\\xe8(.'\n print('Encryption key:', secret)\n\n cipher = AES.new(secret)\n\n encoded = EncodeAES(cipher, privateInfo)\n print('Encrypted string:', encoded.decode('UTF-8'))\n\n\ndef decrypt_key_hardcoded(encryptedString):\n \"\"\" Method to decrypt message using a hard-coded decryption key \"\"\"\n \n PADDING = '{'\n DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).decode('utf-8').rstrip(PADDING)\n #DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)\n key = b'\\xf9J\\xa4\\xd1\\t\\x17\\xb8\\xabt\\xfe\\x06\\x96\\xe3\\xe8(.'\n try:\n cipher = AES.new(key)\n decoded = DecodeAES(cipher, encryptedString)\n print('Decoded:', decoded)\n except:\n print(\"Error in decoding the secret message\")\n \ndef decrypt_with_key(key, encryptedString):\n \"\"\" Method to decrypt message using a decryptionn key passed in as a parameter \"\"\"\n \n PADDING = '{'\n DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).decode('utf-8').rstrip(PADDING)\n try:\n cipher = AES.new(key)\n decoded = DecodeAES(cipher, encryptedString)\n print('Decoded:', decoded)\n except:\n print(\"Error in decoding the secret message\")",
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Let's encrypt our secret message"
},
{
"cell_type": "code",
"collapsed": false,
"input": "secret_message = input(\"Enter your secret message: \")\nencryption(secret_message)",
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Enter your secret message: Message I don't want the government to see\n"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "Encryption key: b'\\x06\\n\\xf5\\xd2\\x99\\xd0t\\x9fv\\x1b\\xcf\\xfe8\\xab\\x1e-'\nEncrypted string: ZQRLOzBcrXpLkCvk1Ofjr7ALZdJMcrDalXvwGDu4Bwx7ck/FXdo8WIFibE1r37Vn\n"
}
],
"prompt_number": 12
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": "Let's decrypt the message by passing in the decryption key"
},
{
"cell_type": "code",
"collapsed": false,
"input": "encoded_string = input(\"Copy/paste the encrypted string here: \")\nkey = input(\"Copy/paste the decryption key here (without the b and single quotes): \")\ndecrypt_with_key(codecs.escape_decode(key)[0], encoded_string)",
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Copy/paste the encrypted string here: ZQRLOzBcrXpLkCvk1Ofjr7ALZdJMcrDalXvwGDu4Bwx7ck/FXdo8WIFibE1r37Vn\n"
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": "Copy/paste the decryption key here (without the b and single quotes): \\x06\\n\\xf5\\xd2\\x99\\xd0t\\x9fv\\x1b\\xcf\\xfe8\\xab\\x1e-\n"
},
{
"output_type": "stream",
"stream": "stdout",
"text": "Decoded: Message I don't want the government to see\n"
}
],
"prompt_number": 14
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment