<?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: PrashikNikumbe</title>
    <description>The latest articles on DEV Community by PrashikNikumbe (@prashiknikumbe).</description>
    <link>https://dev.to/prashiknikumbe</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%2F488650%2Fa374ba7b-f214-4c00-a1dc-841b22184ff4.png</url>
      <title>DEV Community: PrashikNikumbe</title>
      <link>https://dev.to/prashiknikumbe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prashiknikumbe"/>
    <language>en</language>
    <item>
      <title>Cryptography using python.</title>
      <dc:creator>PrashikNikumbe</dc:creator>
      <pubDate>Thu, 30 Sep 2021 18:00:31 +0000</pubDate>
      <link>https://dev.to/prashiknikumbe/cryptography-using-python-29im</link>
      <guid>https://dev.to/prashiknikumbe/cryptography-using-python-29im</guid>
      <description>&lt;h2&gt;
  
  
  What is cryptography ?
&lt;/h2&gt;

&lt;p&gt;It is the process of securing the message/information for communication between sender and receiver. &lt;/p&gt;

&lt;h2&gt;
  
  
  Common terms:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Plain text : It is a message/information in the readable form.&lt;/li&gt;
&lt;li&gt;Cipher text : It is formed by applying cryptographic algorithms on plain text. It is not in a readable form and cannot be easily understood.&lt;/li&gt;
&lt;li&gt;Key : It is a variable value used in encryption and decryption process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Types of cryptography
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Symmetric key cryptography: same key is used for encryption and decryption.&lt;/li&gt;
&lt;li&gt;Asymmetric key cryptography: different key is used for encryption and decryption. pair of keys is used known as public and private key.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is hashing ?
&lt;/h2&gt;

&lt;p&gt;It is a cryptography technique which converts the plain text into unique hash value. Commonly the hash string is of fixed length independent of length of plain text. hash value cannot be reversed engineered in to plain text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Knowing our python libraries
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;cryptography: It includes both high level recipes and low-level interfaces to common cryptographic algorithms such as symmetric ciphers, message digests, and key derivation functions. We will be using it for demonstration of symmetric key cryptography.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;rsa: It include pure implementation of rsa algorithm. We will be using it for demonstration of asymmetric key cryptography.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;haslib: It provides interface for hashing messages easily. It contains numerous methods which will handle hashing any raw message in an encrypted format. We will be using it for demonstration of hashing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installing libraries....
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install cryptography
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install rsa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case of any error try to run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python -m pip install --upgrade pip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Symmetric key cryptography
&lt;/h2&gt;

&lt;p&gt;Fernet : It is a module inside cryptography package. It guarantees that a message encrypted using it cannot be manipulated or read without the key. &lt;/p&gt;

&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from cryptography.fernet import Fernet

plain_text = "Hello World"


# generating random key 
key = Fernet.generate_key()

# creating object of Fernet class
fernet = Fernet(key)

# encryption takes place using encrypt method
cipher = fernet.encrypt(plain_text.encode())

print("original string: ", plain_text)
print("encrypted string: ", cipher)

# decryption takes place using decrypt method
newMessage = fernet.decrypt(cipher).decode()

print("decrypted string: ", newMessage)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;original string:  Hello World
encrypted string:  b'gAAAAABhVfD58m85tEJe3U4AQRbhIXFULXdfFGZnzS7IHS6aH8VGC4il3HSTF2tMjQ7_WJJdUAcAHuNV27ravfvOFOPv1hsQYg=='
decrypted string:  Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Asymmetric key cryptography
&lt;/h2&gt;

&lt;p&gt;RSA algorithm is asymmetric cryptography algorithm. It uses different key for encryption and decryption i.e public and private.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import rsa

#newkeys method will generate both the keys
public_key, private_key = rsa.newkeys(512)

plain_text = "Hello World"

#encryption takes place using encrypt method using public key
#normal string should be encoded into byte string
#using encode method
cipher = rsa.encrypt(plain_text.encode(),public_key)

print("original string: ", plain_text)
print("encrypted string: ", cipher)

#decryption takes place using decrypt method using private key
#after that string is converted from byte stream to string
#using decode method
newMessage = rsa.decrypt(cipher, private_key).decode()
print("decrypted string: ", newMessage)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;original string:  Hello World
encrypted string:  b')e`\x00\xd7\xdb\xce\xae)\x93 \x06\x8b\x9a\x08\x90\xca`\xbd\x0e\xcc&amp;gt;72$\x08_\x0b\x9a6\x93\xf8\xc4\x1f\x8cv\xf7\xd1\x8e\x84\xb4\xd0\xb1\nPj\xee\xc5\x14\x88B\xd4{\x89[%\xab}s\xdcY\x05\x93\xba'
decrypted string:  Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hashing
&lt;/h3&gt;

&lt;p&gt;Message Digest Algorithm 5 (MD5) is a cryptographic hash algorithm that can be used to create a 128-bit string value from an arbitrary length string.&lt;/p&gt;

&lt;p&gt;Secure Hash Algorithm(SHA), used for hashing data and certificate files. It is more secure than MD5. It has 6 types namely SHA-0, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import hashlib

plain_text = "Hello World"

#MD5 Hashing takes place
md5Hash  = hashlib.md5(plain_text.encode())
#Hexidecimal string is generated
hexValue1 = md5Hash.hexdigest()

print("MD5 Hash Value:",hexValue1)

#SHA Hashing takes place
shaHash = hashlib.sha1(plain_text.encode())
#Hexidecimal string is generated
hexValue2 = shaHash.hexdigest()

print("SHA Hash Value:",hexValue2)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MD5 Hash Value: b10a8db164e0754105b7a99be72e3fe5
SHA Hash Value: 0a4d55a8d778e5022fab701977c5d840bbc486d0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>cryptography</category>
      <category>hashing</category>
    </item>
    <item>
      <title>Blowfish Cipher</title>
      <dc:creator>PrashikNikumbe</dc:creator>
      <pubDate>Tue, 13 Oct 2020 16:18:46 +0000</pubDate>
      <link>https://dev.to/prashiknikumbe/blowfish-cipher-1ip1</link>
      <guid>https://dev.to/prashiknikumbe/blowfish-cipher-1ip1</guid>
      <description>&lt;p&gt;Blowfish is a symmetric-key block cipher designed by Bruce Schneier in 1993.It is found to be faster than DES and used as a replacement to it. It is unpatented means anyone can freely use it. One major feature is that its S-Boxes are made by using private key, also its subkey generation process is complex. It is more secured as no cryptanalysis of it is found till now. &lt;/p&gt;

&lt;p&gt;• Plain text size: 64-bit&lt;br&gt;
• Key Size: It is variable in size and comes in range 32-bits to 448-bit&lt;br&gt;
• Number of rounds: 16&lt;br&gt;
• S-box used: 4&lt;br&gt;
• Subkey used(P-array): 18&lt;/p&gt;

&lt;p&gt;There is total three parts in Blowfish algorithm&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Key Expansion&lt;/li&gt;
&lt;li&gt; Encryption&lt;/li&gt;
&lt;li&gt; Decryption&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  1)Key Expansion
&lt;/h1&gt;

&lt;p&gt;P-array is used which is consist of eighteen 32-bit subkey initialized with hexadecimal digits of pi. Four S-boxes are used which is of 32-bit each and has 256 entries initialized with hex values of pi.&lt;/p&gt;

&lt;p&gt;The private key is divided in fourteen 32-bit key for this the key size should be 448-bit but if its less than 448-bit then repetition of a key is done to make it of 488-bit. These values are stored in K-array.&lt;/p&gt;

&lt;p&gt;Step1:The first value of P-array i.e. P[0] is XORed with K[0] and result is replaced with P[0].This process is continues till all fourteen values of P-array XORed and replaced with all fourteen values of   K-array, now for P[14] to P[17] it is X0Red with K[0] to K[3].&lt;/p&gt;

&lt;p&gt;Step2: Now the plaintext of 64-bit initialized to all zero bits and passed through blowfish encryption process which used P-array formed in Step1 and S-boxes which described above. The result which is of 64-bit breaks into two parts of 32-bit each and replaced with P[0] and P[1].&lt;/p&gt;

&lt;p&gt;Step3: Process in Step2 is repeated with modified P-array and results in now replaced with P[2] and P[3].&lt;/p&gt;

&lt;p&gt;Step4:The above Step3 is repeated until all P-arrays elements are replaced and All S-boxes get replaced in order with the values of generated by blowfish algorithm.&lt;/p&gt;

&lt;p&gt;Total 521 iterations are required to generate all sub-keys. They are stored and used in encryption and decryption process.&lt;/p&gt;

&lt;h1&gt;
  
  
  2)Encryption
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A1_PbBAK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/8cndzkgcj9tg6cn690yr.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A1_PbBAK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/8cndzkgcj9tg6cn690yr.PNG" alt="Encryption Flowchart" width="776" height="615"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown above, plain text of 64-bit is passed through 16 rounds. There is one sub key for each round that comes from P-array, utilization of P[0] to P[15] is done in this block. The output of 16th round is used in whitening process which utilizes the remaining subkey P[16] and P[17]. The final results come out as a cipher text of 64-bit.&lt;br&gt;
Now let’s see what happens in each round.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rounds
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r9l9ywF_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/43yncl93hrxfmp5qsy9q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r9l9ywF_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/43yncl93hrxfmp5qsy9q.png" alt="Round flowchart" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In first round the plain text is divided into two parts of size 32-bit each. The left part is XORed with 32-bit P[0] for 1st round, also it feed into right part of plain text, passed for next round. The XORed value is passed through function F and the returned 32-bit value is again XORed with right part of plain text. Now it feed into left part of plaintext which is used in next round. The process continues for next fifteen rounds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function(F)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p2oiIOeb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/zk3yl2uq6hc5rqss2ngu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p2oiIOeb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/zk3yl2uq6hc5rqss2ngu.png" alt="Function flowchart" width="693" height="688"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Function F takes input which comes from XORed of left side of plain text and P[i] as shown in round process. The input is of 32-bit and which is divided into four 8-bit blocks. S-boxes take input as an     8-bit and maps within its 256 entries and give output of 32-bit. Output of S-box1 and S-box 2 is feed into modulo add of 2^32 and its result XORed with output of S-box3 which is further modulo add 2^32 with S-box4 output. The resultant of these function is of 32-bit.This function runs for 16 times.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output Whitening
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1B43XpOt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/2944ox14nnatvf711pgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1B43XpOt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/2944ox14nnatvf711pgx.png" alt="Output Whitening flowchart" width="800" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output that comes from 16th round is break into left and right part. Left part is modulo added 2^32 with P[16],result of it now becomes the right part of Cipher Text. Similarly, right part modulo added 2^32 with P[17] and result is feed into left part of Cipher Text. This process is more like undo the swapping that occurred in 16th round. This block gives a Cipher Text of 64-Bit.&lt;/p&gt;

&lt;h1&gt;
  
  
  3)Decryption
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5Vd87doR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/yuo7qrroib2gtuc45k0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5Vd87doR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/yuo7qrroib2gtuc45k0g.png" alt="Alt Text" width="752" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The decryption process is same as of encryption the difference is that the P-array is used here in a reverse manner. As shown above in 16 rounds the P-array values from P[17] to P[2] is used and remaining two i.e. P[1] and P[0] values are used in Whitening process. P-array and S-boxes are same that used in encryption process. Function F is also same which is used in round process.&lt;/p&gt;

</description>
      <category>blockcipher</category>
    </item>
  </channel>
</rss>
