{"id":17538,"date":"2021-06-02T17:17:37","date_gmt":"2021-06-02T17:17:37","guid":{"rendered":"https:\/\/www.askpython.com\/?p=17538"},"modified":"2023-02-16T19:56:51","modified_gmt":"2023-02-16T19:56:51","slug":"cryptography-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/cryptography-module","title":{"rendered":"Easy Introduction to Cryptography in Python"},"content":{"rendered":"\n<p><strong><em>Cryptography<\/em><\/strong> is defined as the process of keeping important information safe and secure by converting them into something humans can&#8217;t directly understand to keep the actual message safe and away from the wrong hands.<\/p>\n\n\n\n<p>In the modern era of modern technology, everyone should crypt the data being sent because not only it&#8217;s a great practice but also it keeps personal and official information safe and secure. <\/p>\n\n\n\n<p>One must also have a strong cryptography algorithm to make sure the encrypted text is much harder to hack through and your information is more secure from going into the wrong hands.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why is Cryptography important?<\/h2>\n\n\n\n<p>Cryptography is important for the following reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Guarding important information and communication info<\/strong> against unauthorized people and prevent access of information to them.<\/li><li>Have <strong>digital signatures<\/strong> which are helpful in protecting important information against forgeries.<\/li><li>It is also essential to maintain the <strong>integrity <\/strong>of the information.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing Cryptography in Python<\/h2>\n\n\n\n<p>Now that we learned a lot about Cryptography. Now let&#8217;s learn how to implement it yourself using the Python programming language.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Importing Modules<\/h3>\n\n\n\n<p>To perform cryptography, we will be using the <code>cryptography<\/code> module and we will be making use of the <code>Fernet<\/code> objects. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom cryptography.fernet import Fernet\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Implementing Cryptography<\/h3>\n\n\n\n<p>To implement cryptography we will be generating a Fernet key (known as the &#8220;secret key&#8221;) and then we create a Fernet object using the key.<\/p>\n\n\n\n<p>This key is very important and it needs to be kept safe! If someone finds your key he\/she can decrypt all your secret messages and if you lose it then you will no longer be able to decrypt your own messages.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nkey = Fernet.generate_key()\nFernet_obj= Fernet(key)\n<\/pre><\/div>\n\n\n<p>The next step is to encrypt the text where we use of the encrypt function and pass the message to the function. The function will return the encrypted message.<\/p>\n\n\n\n<p>Along with this let&#8217;s also store the decrypted message from the encrypted message using the <code>decrypt<\/code> function and pass the encrypted message.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEncry_text = Fernet_obj.encrypt(b&quot;I am a secret! I will get encrypted into something you will never understand&quot;)\nOrg_text= Fernet_obj.decrypt(Encry_text)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Printing Results<\/h3>\n\n\n\n<p>Now let&#8217;s have a <a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-print-function\" data-type=\"post\" data-id=\"2517\">print<\/a> the encrypted and decrypted message we obtained.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;The Encrypted text is: &quot;, cipher_text)\nprint(&quot;\\nThe Decrypted text is: &quot;,plain_text)\n<\/pre><\/div>\n\n\n<p>The output looked something like what&#8217;s shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nThe Encrypted text is:  b&#039;gAAAAABgsSrnZRaDQbApvKL_xiXfCXHV_70u5eXZKDqYIkMKwxochYNy0lmVrvPFtQWya22MZh92rimscuA5VBuoN-B5YfCRHvpBYhKsbIiuPuz-CklJ-EFyZtZ_S7TRe-b9VSoee03Z8jkxwQpR8FatZ1XWA7xZvm5WpGSQFZkN8w7Ix8riyOo=&#039;\n\nThe Decrypted text is:  b&#039;I am a secret! I will get encrypted into something you will never understand&#039;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Congratulations! Today you learned about Cryptography and how to implement the same on your own. Try the same out yourself and keep your messages a secret from the outside world! Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cryptography is defined as the process of keeping important information safe and secure by converting them into something humans can&#8217;t directly understand to keep the actual message safe and away from the wrong hands. In the modern era of modern technology, everyone should crypt the data being sent because not only it&#8217;s a great practice [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":17539,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-17538","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/17538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=17538"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/17538\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/17539"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=17538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=17538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=17538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}