You are on page 1of 6

EXPERIMENT NO.

Taqueveem Ahmad
211259
Cryptography and System Security
CSL602
Computer Engineering
M.H. Saboo Siddik College of
Engineering

Aim: Design and Implementation of a product cipher using Substitution and Transposition

ciphers.

Keywords: Encryption, cipher, substitution, transposition, caesar, plaintext.

1. INTRODUCTION

In cryptography, a cipher (also spelled as cypher) refers to an algorithm designed for encryption or
decryption. It entails a series of precisely defined steps that can be executed as a procedure. An
alternative term, albeit less common, is encipherment. To encipher or encode is to convert information
into cipher or code. While in everyday language, "cipher" is often used interchangeably with "code" as
both involve a set of steps to encrypt a message, the distinction between the two is significant in
cryptography, particularly in classical cryptography.
In general, codes involve substituting strings of characters of varying lengths in the output, whereas
ciphers typically substitute the same number of characters as are input. A code establishes a mapping
between different meanings, allowing words or phrases to be represented as letters or numbers. Codes
usually offer a direct correspondence from input to key and are primarily employed to expedite
communication. Conversely, ciphers operate algorithmically, necessitating adherence to the cipher's
process for decryption. Ciphers are frequently employed to encrypt written information, ensuring its
confidentiality.

2. THEORY
Caesar Cipher:

The Caesar cipher is one of the simplest and most widely known encryption techniques. It is named
after Julius Caesar, who is reputed to have used it to communicate with his generals. The method
involves shifting each letter in the plaintext by a fixed number of positions down or up the alphabet.
For example,
Encryption:
Plaintext: "HELLO"
Shift: 3 (each letter will be shifted three positions down the alphabet)
Encrypting:
H -> K
E -> H
L -> O
L -> O
O -> R
Ciphertext: "KHOOR"
So, "HELLO" encrypted using the Caesar cipher with a shift of 3 becomes "KHOOR".
Decryption:
Ciphertext: "KHOOR"
Shift: 3 (same as used for encryption)
Decrypting:
K -> H
H -> E
O -> L
O -> L
R -> O
Plaintext: "HELLO"
So, "KHOOR" decrypted using the Caesar cipher with a shift of 3 becomes "HELLO".
In this example, we shifted each letter in the plaintext three positions down the alphabet to encrypt it,
and then shifted each letter back three positions to decrypt it.
Encryption process:
i. Choose a Shift: Decide on the number of positions each letter will be shifted. This is known
as the key or shift value. For example, a shift of 3 means that each letter will be replaced by
the letter that is three positions down the alphabet.
ii. Prepare the Plaintext: Write down the message that you want to encrypt.
iii. Shift Each Letter: For each letter in the plaintext:
iv. If the letter is uppercase, shift it by the chosen amount to the right in the alphabet.
v. If the letter is lowercase, similarly shift it to the right.
vi. If shifting beyond Z or z is required, wrap around to the beginning of the alphabet.
vii. Create the Ciphertext: Write down the resulting letters to form the ciphertext, which is the
encrypted message.
Decryption process:
The decryption process is essentially the reverse of the encryption process:
i. Choose the Same Shift: You need to know the same shift value that was used for encryption.
ii. Prepare the Ciphertext: Write down the encrypted message that you want to decrypt.
iii. Shift Each Letter Back: For each letter in the ciphertext:
iv. If the letter is uppercase, shift it by the same amount to the left in the alphabet.
v. If the letter is lowercase, similarly shift it to the left.
vi. If shifting before A or a is required, wrap around to the end of the alphabet.
vii. Recover the Plaintext: Write down the resulting letters to form the original plaintext.

Product Cipher:

A product cipher is a cryptographic technique that combines multiple encryption methods in sequence
or in parallel to enhance security. It aims to overcome the weaknesses of individual ciphers by
leveraging the strengths of each component cipher. Typically, product ciphers involve both substitution
and transposition techniques
Substitution:
 Substitution involves replacing each plaintext character with another character based on a
fixed system. This could be a simple substitution (like the Caesar cipher) or a more complex
one (like the Vigenère cipher).
 Substitution ciphers ensure that each character in the plaintext is replaced with another
character according to a predetermined rule, thereby obscuring the original message.
Transposition:
 Transposition involves rearranging the order of characters or bits in the message without
altering the characters themselves.
 This could be done by writing the plaintext in a grid and then rearranging the columns or rows
based on a key, or by other methods such as rail fence or columnar transposition.

3. PROGRAM

//Server.py

import socket

def caesar_cipher(message, shift):


cipher_text = ""
for char in message:
if char.isalpha():
shifted_char = chr((ord(char) - ord('a' if char.islower() else 'A') + shift) % 26 + ord('a' if char.islower() else
'A'))
cipher_text += shifted_char
else:
cipher_text += char
return cipher_text

def reverse_cipher(message):
return message[::-1]

def caesar_decipher(cipher_text, shift):


return caesar_cipher(cipher_text, -shift)

def reverse_decipher(cipher_text):
return reverse_cipher(cipher_text)

def handle_client(client_socket):
request = client_socket.recv(1024).decode()
message, shift = request.split(",")
encrypted_message = caesar_cipher(message, int(shift))
decrypted_message = caesar_decipher(encrypted_message, int(shift))

encrypted_message2 = reverse_cipher(encrypted_message)
decrypted_message2 = reverse_decipher(encrypted_message)
with open("output.txt", "w") as file:
file.write(f"Caesar Encrypted message: {encrypted_message}\nReverse Encrypted message:
{encrypted_message2}\n")
with open("output.txt", "a") as file:
file.write(f"Reverse Decrypted message: {encrypted_message}\nDecrypted message: {decrypted_message}")

client_socket.send("Done".encode())
client_socket.close()

def start_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("localhost", 8888))
server_socket.listen(5)
print("Server started. Listening on port 8888...")
while True:
client_socket, client_address = server_socket.accept()
print(f"Accepted connection from {client_address[0]}:{client_address[1]}")
handle_client(client_socket)

if __name__ == "__main__":
start_server()

//Client.py

import socket

def send_request(message, shift):


client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 8888))

request = f"{message},{shift}"
client_socket.send(request.encode())

response = client_socket.recv(1024).decode()
print(response)

client_socket.close()

if __name__ == "__main__":
message = input("Enter message to encrypt: ")
shift = 3

send_request( message, shift)


4. OUTPUT
//Server.py

//Client.py
//Output File

5. CONCLUSION

In conclusion, the design and implementation of a product cipher utilizing both substitution and
transposition ciphers offer a robust approach to encryption. By combining these two cryptographic
techniques, we can create a cipher that is more secure and resistant to cryptanalysis compared to using
either cipher alone.

6. REFERENCES

[1] Simmons, Gustavus J.. "product cipher". "Encyclopedia" is a reference book or set of books
containing articles on various topics, often arranged alphabetically.Britannica, 8 Oct. 2015,
https://www.britannica.com/topic/product-cipher. Accessed 6 February 2024.

[2] "Caesar Cipher" on Wikipedia: https://en.wikipedia.org/wiki/Caesar_cipher

[3] "Caesar Cipher" on Crypto Corner: https://crypto.interactive-maths.com/caesar-shift-cipher.html

You might also like