{"id":70,"date":"2019-07-06T10:16:46","date_gmt":"2019-07-06T04:46:46","guid":{"rendered":"http:\/\/techieshouts.com\/?p=70"},"modified":"2022-08-09T19:09:02","modified_gmt":"2022-08-09T13:39:02","slug":"aes-encryption-and-decryption-python-aes-package","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/aes-encryption-and-decryption-python-aes-package\/","title":{"rendered":"AES Encryption using Python packages"},"content":{"rendered":"\n<p>AES stands for Advanced Encryption Standard. AES is the first and only publicly accessible\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cipher\" target=\"_blank\" rel=\"noopener\">cipher<\/a>\u00a0approved by the\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/National_Security_Agency\" target=\"_blank\" rel=\"noopener\">National Security Agency<\/a>(NSA). There is multiple AES package available in python.<\/p>\n\n\n\n<p>In this post, we will see the implementation with two different packages in python<\/p>\n\n\n\n<ol><li>Method1 &#8211; &#8220;crypto&#8221; package<\/li><li>Method2 &#8211; &#8220;cryptography&#8221; package<\/li><\/ol>\n\n\n\n<h2> U<strong>sing the &#8220;Crypto&#8221; AES package<\/strong><\/h2>\n\n\n\n<p>In this method, the python version used is 2.6.6<\/p>\n\n\n\n<p>Install the &#8220;crypto&#8221; package in your machine. Installation can be done by running &#8220;pip install crypto&#8221;<\/p>\n\n\n\n<p>Once done, you will be able to run the below program to encrypt and decrypt the text<\/p>\n\n\n\n<pre data-mode=\"python\" data-theme=\"github\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">from Crypto.Cipher import AES\n\ndef encrypt(plain_text, iv, key):\n    obj = AES.new(key, AES.MODE_CFB, iv)\n\n    enc_text = obj.encrypt(plain_text)\n    return enc_text\n\ndef decrypt(enc_text, iv, key):\n    obj = AES.new(key, AES.MODE_CFB, iv)\n\n    plain_text = obj.decrypt(enc_text)\n    return plain_text\n\nif __name__ == \"__main__\":\n    plaintext = \"Testing AES encryption\/decryption in techieshouts.com\"\n    iv = \"TestMeInitVector\"\n    key = \"YourSampleEncKey\"\n    print(\"Plain text: \",plaintext)\n    print(\"Calling encryption library\")\n    encryptedtext = encrypt(plaintext,iv,key)\n    print(\"Encrypted text\")\n    print(encryptedtext)\n    decryptedtext = decrypt(encryptedtext,iv,key)\n    print(\"Decrypted text\")<\/pre>\n\n\n\n<p><strong>from Crypto.Cipher import AES<\/strong><\/p>\n\n\n\n<p>Import AES from the crypto cipher package so that we can use the encrypt and decrypt methods in the code.<\/p>\n\n\n\n<p><strong>obj = AES.new(key, AES.MODE_CFB, iv)<\/strong><\/p>\n\n\n\n<p>After importing the package, the AES object can be created using the above code. Key and iv are the secret characters that used to encrypt the content. Since these two are the secret elements of encryption, knowing these two will help the programmer to decrypt the encrypted contents.<\/p>\n\n\n\n<p><strong>enc_text = obj.encrypt(plain_text)<\/strong><\/p>\n\n\n\n<p>In the above line, we are calling the &#8220;encrypt&#8221; method of the AES package.<\/p>\n\n\n\n<h2><strong>Using the &#8220;cryptography&#8221; package<\/strong><\/h2>\n\n\n\n<p>There is another way to do encryption and decryption using the same AES algorithm. But, this time we will implement the same using the &#8220;cryptography &#8221; package.<\/p>\n\n\n\n<p>In this method, the python version used is 2.6.6<\/p>\n\n\n\n<p>Before running the below program, we have to make sure that the &#8220;cryptography &#8221; package is installed. If not, please run the pip command in administrator mode and get the package<\/p>\n\n\n\n<p>After installing the &#8220;cryptography&#8221; package, just execute the below program to see the results.<\/p>\n\n\n\n<pre data-mode=\"python\" data-theme=\"github\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">from cryptography.hazmat.primitives import padding\nfrom cryptography.hazmat.primitives.ciphers import (Cipher, algorithms, modes)\n\ndef encrypt(p_text, iv, key):\n    backend = default_backend()\n\n    cipher = Cipher(algorithms.AES(key),modes.CFB(iv),backend=backend)\n    padder = padding.PKCS7(128).padder() # 128 bit\n    text = padder.update(p_text) + padder.finalize()\n    encryptor = cipher.encryptor()\n    c_text = encryptor.update(text) + encryptor.finalize()\n    return c_text\n\n\ndef decrypt(p_text, iv, key):\n    backend = default_backend()\n\n    cipher = Cipher(algorithms.AES(key),modes.CFB(iv),backend=backend)\n    padder = padding.PKCS7(128).padder() # 128 bit\n    text = padder.update(p_text) + padder.finalize()\n    decryptor = cipher.decryptor()\n    c_text = decryptor.update(text) + decryptor.finalize()\n    return c_text\n\n\nif __name__ == \"__main__\":\n    plaintext = \"Testing AES encryption\/decryption in techieshouts.com\"\n    iv = \"TestMeInitVector\"\n    key = \"YourSampleEncKey\"\n    print(\"Plain text: \",plaintext)\n    print(\"Calling encryption library\")\n    encryptedtext = encrypt(plaintext,iv,key)\n    print(\"Encrypted text\")\n    print(encryptedtext)\n    decryptedtext = decrypt(encryptedtext,iv,key)\n    print(\"Decrypted text\")<\/pre>\n\n\n\n<p><strong>cipher = Cipher(algorithms.AES(key),modes.CFB(iv),backend=backend)<\/strong><\/p>\n\n\n\n<p>We are creating the cipher object using the above line. While creating the object itself we are passing the iv and key.<\/p>\n\n\n\n<p>Once the cipher object is created, we apply cipher padding and encrypt the given text using the below lines<\/p>\n\n\n\n<pre data-mode=\"python\" data-theme=\"github\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">padder = padding.PKCS7(128).padder() # 128 bit\ntext = padder.update(p_text) + padder.finalize()\nencryptor = cipher.encryptor()\nc_text = encryptor.update(text) + encryptor.finalize()<\/pre>\n\n\n\n<p>The same technique is used for decrypting the content. But, here, instead of the encryptor, we will use the decryptor method as in below line<\/p>\n\n\n\n<p><strong>cipher.decryptor()<\/strong><\/p>\n\n\n\n\n\n<p>Check the same <a href=\"https:\/\/techieshouts.com\/aes-encryption-and-decryption-in-java\/\">&#8220;AES implementation using Java&#8221;<\/a><\/p>\n\n\n\n<p>Reference &#8211; <a href=\"https:\/\/en.wikipedia.org\/wiki\/Advanced_Encryption_Standard\" target=\"_blank\" rel=\"noopener\">Advance Encryption Standard &#8211; wiki<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AES stands for Advanced Encryption Standard. AES is the first and only publicly accessible\u00a0cipher\u00a0approved by the\u00a0National Security Agency(NSA). There is multiple AES package available in python. In this post, we will see the implementation with two different packages in python Method1 &#8211; &#8220;crypto&#8221; package Method2 &#8211; &#8220;cryptography&#8221; package Using the &#8220;Crypto&#8221; AES package In this\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/aes-encryption-and-decryption-python-aes-package\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":67,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[30,31,34,33],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/70"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":25,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":580,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/70\/revisions\/580"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media\/67"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}