0% found this document useful (0 votes)
28 views10 pages

CSS Exp 1

The document outlines the design and implementation of a product cipher using substitution and transposition techniques in encryption. It explains the concepts of substitution ciphers, including a monoalphabetic substitution algorithm, and transposition ciphers, detailing their encryption and decryption processes. Additionally, it provides sample code for both cipher types, demonstrating how to encrypt and decrypt messages using specified keys.

Uploaded by

Madhura Kanse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views10 pages

CSS Exp 1

The document outlines the design and implementation of a product cipher using substitution and transposition techniques in encryption. It explains the concepts of substitution ciphers, including a monoalphabetic substitution algorithm, and transposition ciphers, detailing their encryption and decryption processes. Additionally, it provides sample code for both cipher types, demonstrating how to encrypt and decrypt messages using specified keys.

Uploaded by

Madhura Kanse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Department of Artificial Intelligence and Data Science

Experiment No. 1
Aim: Design and implement of product cipher using substitution and transposition.
Software Used: VS Code.

Theory:
What is Product Cipher?
A product cipher is a type of encryption technique that combines two or more simpler ciphers
to increase the overall security of the encryption process. The idea is to apply different
encryption operations in sequence, each contributing to the complexity and difficulty of
decrypting the message without the proper key.
The term "product" refers to the fact that multiple ciphering operations are combined to produce
a stronger encryption product. The security of the product cipher relies on the fact that if an
attacker can break one cipher, they still need to break the other one(s), making the system more
resistant to cryptanalysis.

Substitution ciphers and transposition ciphers are two basic types of classical product ciphers that
work by altering the structure of the plaintext to create cipher text.
What is Substitution Cipher?
A substitution cipher is a type of encryption in which each unit (typically a letter, symbol, or
block of text) in the plaintext is replaced with another unit according to a specific rule or system.
The goal of a substitution cipher is to transform the plaintext into ciphertext in such a way that
the encrypted message is unreadable without the decryption key.
In a substitution cipher, each character or group of characters in the plaintext is substituted with
a different character or symbol from a predefined set. The specific mapping from plaintext to
cipher text is determined by the cipher's key.

Encryption Algorithm (Monoalphabetic Substitution Cipher):


Define a key that is a substitution of the alphabet. Key = 3
Plaintext alphabet: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Ciphertext alphabet: D E F G H I J K L M N O P Q R S T U V W X Y Z A B C Steps:
- Take the plaintext letter (e.g., 'A').
- Look it up in the plaintext alphabet (it’s in the first position).
- Replace it with the letter in the same position of the ciphertext alphabet (in this case, 'D').
- Repeat this for each letter of the plaintext.
Department of Artificial Intelligence and Data Science

Example:

Plaintext: "HELLO" Encryption:

• H→K
• E→H
• L→O
• L→O
• O→R

Ciphertext: "K H O O R"

Decryption Algorithm (Monoalphabetic Substitution Cipher)


The key is the same as the encryption key, but in the decryption process, we reverse the
substitution (i.e., we map each ciphertext letter back to the corresponding plaintext letter).

Steps:

• Take the ciphertext letter (e.g., 'K'). Look it up in the ciphertext alphabet (it’s in the 4th
position).
• Replace it with the letter in the same position of the plaintext alphabet (in this case, 'H').
• Repeat this for each letter of the ciphertext.

Example:

Ciphertext: "K H O O R"

Plaintext: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Ciphertext:

DEFGHIJKLMNOPQRSTUVWXYZABC

Decryption:

• K→H
• H→E
• O→L
• O→L
• R→O

Plaintext: "HELLO"
Department of Artificial Intelligence and Data Science

What is Transposition Cipher?


A transposition cipher is a type of encryption where the positions of the characters in the
plaintext are rearranged (or permuted) according to a specific system or key, without changing
the actual characters themselves. Unlike substitution ciphers, where the characters are replaced
with other characters, transposition ciphers only alter the order of the characters.
The main goal of a transposition cipher is to create ciphertext that appears random, while
retaining the same characters as the plaintext, making it harder to read without knowing the
encryption method.

Encryption algorithm for Transposition Cipher:


Department of Artificial Intelligence and Data Science

Decryption algorithm for Transposition Cipher:

Conclusion: Hence, we studied and understood the design and implementation of product cipher
using Substitution Cipher and Transposition Cipher.
Department of Artificial Intelligence and Data Science

Name: Madhura Kanse Roll No: 619


Code: Substitution Cipher
key = 'abcdefghijklmnopqrstuvwxyz'

def encrypt(n, plaintext):

result = ""

for char in plaintext.lower():

if char in key:

i = (key.index(char) + n) % 26

result += key[i]

else:

result += char

return result

def decrypt(n, ciphertext):

result = ""

for char in ciphertext:

if char in key:

i = (key.index(char) - n) %26

result += key[i]

else:

result += char

return result

# Input and testing

text = input("Enter a message for

Encryption: ")

print("Enter a key:")
offset = input()
Department of Artificial Intelligence and Data Science

offset = int(offset, 10)

# Convert the key input to an integer

print("Plaintext: " + text)

# Encrypt the message

encrypted = encrypt(offset, text)

print('Encrypted Message:',

encrypted)

# Decrypt the message

decrypted = decrypt(offset,

encrypted)

print('Decrypted Message:',

decrypted)

Output:
Department of Artificial Intelligence and Data Science

Name: Madhura Kanse Roll No: 619


Code: Transposition Cipher
def encrypt(key, plaintext):
#Remove spaces from plaintext

plaintext = plaintext.replace(' ', '')


# Calculate the number of columns

and rows
num_columns = len(key)
num_rows = len(plaintext) //
num_columns
if len(plaintext) %
num_columns != 0:

num_rows += 1

# Create an empty matrix for the columns


matrix = ['' for _ in
range(num_columns)]
# Populate the matrix by filling columns
with characters

for i, char in
enumerate(plaintext):

column = i % num_columns
matrix[column] += char
# Sort the key and determine
the column order
Department of Artificial Intelligence and Data Science

sorted_key =
sorted(enumerate(key),

key=lambda x: x[1])
column_order = [idx for idx, _

in sorted_key]
# Generate the ciphertext by
concatenating the columns in
sorted order
ciphertext = ''.join(matrix[i] for
i in column_order)

return ciphertext
def decrypt(key, ciphertext):
# Calculate the number of
columns and rows

num_columns = len(key)
num_rows = len(ciphertext) //

num_columns
if len(ciphertext) %

num_columns != 0:
num_rows += 1

# Create an empty matrix for the columns


matrix = ['' for _ in
range(num_columns)]
# Sort the key and determine
the column order
Department of Artificial Intelligence and Data Science

sorted_key =
sorted(enumerate(key),

key=lambda x: x[1])
column_order = [idx for idx, _

in sorted_key]
current_char = 0
# Fill the matrix with the
ciphertext columns
for col in column_order:
for row in
range(num_rows):
if current_char <
len(ciphertext):
matrix[col] +=
ciphertext[current_char]
current_char += 1
# Reconstruct the plaintext
from the matrix by reading rows
plaintext =
''.join(''.join(matrix[col][row] if

row < len(matrix[col]) else ''


for col in
range(num_columns)) for row in
range(num_rows))

return plaintext.strip()
# Main Program
Department of Artificial Intelligence and Data Science

text = input("Enter a message for


encryption: ")
key =
'dcbafghijklmnopqrstuvwxyz' #
Example key (customize as
needed)

# Encrypt the plaintext


print("Plaintext: " + text)
encrypted = encrypt(key, text)

print("Encrypted: " + encrypted)

# Decrypt the ciphertext


decrypted = decrypt(key,

encrypted)
print("Decrypted: " + decrypted)

Output:

You might also like