<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hossam Hamdy</title>
    <description>The latest articles on DEV Community by Hossam Hamdy (@nullx33f).</description>
    <link>https://dev.to/nullx33f</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F542076%2F55bf20e3-ccd6-4900-be7b-eec133036da3.jpg</url>
      <title>DEV Community: Hossam Hamdy</title>
      <link>https://dev.to/nullx33f</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nullx33f"/>
    <language>en</language>
    <item>
      <title>XOR Encryption in Python3</title>
      <dc:creator>Hossam Hamdy</dc:creator>
      <pubDate>Mon, 04 Jan 2021 01:19:29 +0000</pubDate>
      <link>https://dev.to/nullx33f/xor-encryption-in-python3-51c</link>
      <guid>https://dev.to/nullx33f/xor-encryption-in-python3-51c</guid>
      <description>&lt;p&gt;Hi Folks, In my article I will explain to you how to encrypt texts with XOR encryption using python 3.&lt;/p&gt;

&lt;p&gt;first we have to import those modules&lt;/p&gt;

&lt;h1&gt;
  
  
  1- importing area
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Va3sACXU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9uzxe43lcv899gs3dqrn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Va3sACXU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9uzxe43lcv899gs3dqrn.png" alt="importing area"&gt;&lt;/a&gt;&lt;br&gt;
In the previous section we import the string module to be able to use all upper and lower case letters, numbers and signs to generate the encryption/decryption key.&lt;/p&gt;

&lt;p&gt;We also import the random module to generate a random string of those letters, numbers and symbols so that we can create a random decryption key every time.&lt;/p&gt;

&lt;h1&gt;
  
  
  2- Generate Random Key Function.
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0NFbOqqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/51l2x7eamf90hdee3fv3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0NFbOqqf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/51l2x7eamf90hdee3fv3.png" alt="gen"&gt;&lt;/a&gt;&lt;br&gt;
In the previous section, we created a function that:&lt;br&gt;
– Receive parameter named size which is considered the length of the encryption key to be generated.&lt;br&gt;
– Then it generate the key from a set of letters, numbers and signs that we typed in the previous code&lt;br&gt;
– Returns the generated key.&lt;/p&gt;

&lt;p&gt;[ The length of the key is very important, for example if you want to encrypt a text consisting of 100 characters and the length of the key consists of 50 characters, then only 50 characters of the text will be encrypted ]&lt;/p&gt;

&lt;h1&gt;
  
  
  3- XOR Encryption Function.
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BC7xTtW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nin4z5mdjzzm8joxthle.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BC7xTtW0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nin4z5mdjzzm8joxthle.png" alt="function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the most important part of the article is the encryption and decryption function.&lt;br&gt;
here the enc_XOR function receive 2 parameters: Text, Key.&lt;/p&gt;

&lt;p&gt;Bitwise operators works on bits and performs bit-by-bit operation. For each character in text and another one of the key It will be processed through the XOR (^) Bitwise Operator and you can read more about the XOR Bitwise Operator in python @ Python Bitwise Operators&lt;/p&gt;

&lt;p&gt;We will talk about XOR operator example:&lt;br&gt;
0 XOR 0 = 0&lt;br&gt;
0 XOR 1 = 1&lt;br&gt;
1 XOR 0 = 1&lt;br&gt;
1 XOR 1 = 0&lt;/p&gt;

&lt;p&gt;[1] An example about encryption:&lt;br&gt;
a(Text) = 0011 1100&lt;br&gt;
b(Key) = 0000 1101&lt;br&gt;
a^b(Encrypted Text) = 0011 0001&lt;/p&gt;

&lt;p&gt;[2] An example about decryption:&lt;br&gt;
c(Encrypted Text) = 0011 0001&lt;br&gt;
b(Key) = 0000 1101&lt;br&gt;
c^b = 0011 1100 = a(Clear Text)&lt;/p&gt;

&lt;p&gt;So if we pass a clear text with a key it will be encrypted &amp;amp; If we pass the encrypted text with the same key it will be decrypted.&lt;/p&gt;

&lt;h1&gt;
  
  
  4- Output Area.
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WlX94522--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1rfdgp57l5z2mo5vo3lt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WlX94522--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1rfdgp57l5z2mo5vo3lt.png" alt="output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Actually, In this section we are only print the outputs 😂&lt;br&gt;
But notice something that we pass the encrypted text(encrypted_text) and key in the encryption function to reverse its effect.&lt;/p&gt;

&lt;h1&gt;
  
  
  [+] The End.
&lt;/h1&gt;

&lt;p&gt;I hope you understood the article well and achieved the benefit you hoped for.&lt;br&gt;
You will find the script on my GitHub Account &lt;a class="comment-mentioned-user" href="https://dev.to/nullx33f"&gt;@nullx33f&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;Thanks for reading. 💙&lt;/p&gt;

</description>
      <category>python</category>
      <category>security</category>
    </item>
  </channel>
</rss>
