We are firstly receiving the plaintext and the key as inputs and then we are encrypting the plaintext with the key. After that, we are decrypting the ciphertext with the same key to obtain the same plaintext.
If the plaintext contains any non-alphabetic character, then we would keep that character as it is in the ciphertext. Otherwise, we would encrypt the letter.
Please note that while encrypting a letter, we need to first get the ordinality of the letter and subtract it with the ordinality of ‘a’ to obtain the position of the letter in the alphabet. After that, we would add the integer key modulo 26 with the obtained position of the letter. We would thus obtain the position of the encrypted letter in the alphabet. We would add the ordinality of ‘a’ with the position to obtain the encrypted letter.
The decryption operation is similar. Here, we would need to take each ciphertext letter and subtract the key in a similar way.
So, the output of the above code is like this:
Plaintext: apple Key: 3 Ciphertext: dssoh Decrypted Text: apple
We can give any integer as the key. For example, for key = 1000, the output is like the following:
Plaintext: apple Key: 1000 Ciphertext: mbbxq Decrypted Text: apple
Please note that in this program we are considering all small letters. The above program can be extended to consider capital letters also. I would leave it on the reader to implement the same.
I hope this helps. However, readers who want to know more about how different cryptographic algorithms work and how they are used in various secure network protocols can refer to the book “Cryptography And Public Key Infrastructure.”










































0 Comments