0% found this document useful (0 votes)
37 views11 pages

Krisha CNS Lab Manual

The document outlines experiments on cryptographic techniques, specifically the Hill cipher, Vigenère cipher, and Rail Fence Transposition cipher. Each section includes objectives, theoretical background, algorithms, and implementation details, highlighting the strengths and weaknesses of each method. Conclusions emphasize the effectiveness of these ciphers in encryption while noting their vulnerabilities to cryptanalysis.

Uploaded by

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

Krisha CNS Lab Manual

The document outlines experiments on cryptographic techniques, specifically the Hill cipher, Vigenère cipher, and Rail Fence Transposition cipher. Each section includes objectives, theoretical background, algorithms, and implementation details, highlighting the strengths and weaknesses of each method. Conclusions emphasize the effectiveness of these ciphers in encryption while noting their vulnerabilities to cryptanalysis.

Uploaded by

Urva Prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

220220131111

Experiment No: 3
Implementation of Hill cipher
Date:
Relevant CO: Explore the basic principles of the symmetric cryptography and
techniques with their strengths and weaknesses from perspective of cryptanalysis

Objectives: (a) to understand working fundamental of Hill Cipher


(b) to carry out Implementation of Hill cipher encryption-decryption.

Equipment/Instruments: Computer System, Turbo-c/ JDK


Theory: Each letter is represented by a number modulo 26. Often the simple scheme A =
0, B = 1... Z = 25, is used, but this is not an essential feature of the cipher. To encrypt a message,
each block of n letters is multiplied by an invertible n × n matrix, against modulus 26. To decrypt the
message, each block is multiplied by the inverse of the m trix used for encryption. The matrix used
for encryption is the cipher key, and it sho ld be chosen randomly from the set of invertible n × n
matrices (modulo 26).

Algorithm:

STEP-1: Read the plain text and key from the user.

STEP-2: Split the plain text into groups of length three.

STEP-3: Arrange the keyword in a 3*3 matrix.


STEP-4: Multiply the two matrices to obtain the cipher text of length three.
STEP-5: Combine all these groups to get the complete cipher text.

Program:
keyMatrix = [[0] * 3 for _ in
range(3)] messageVector = [[0] for _ in
range(3)] cipherMatrix = [[0] for _ in
range(3)]

def getKeyMatrix(key):
k=0
for i in range(3):
for j in range(3):
keyMatrix[i][j] =
ord(key[k]) % 65 k += 1

def encrypt(messageVector):
for i in range(3):
for j in range(1):
cipherMatrix[i][j] = 0
for x in range(3):
220220131111
cipherMatrix[i][j] += (keyMatrix[i][x] * messageVector[x][j])
cipherMatrix[i][j] = cipherMatrix[i][j] % 26
220220131111

def HillCipher(message, key):


getKeyMatrix(key)

for i in range(3):
messageVector[i][0] = ord(message[i]) % 65

encrypt(messageVector)

CipherText = []
for i in range(3):
CipherText.append(chr(cipherMatrix[i][0] + 65))

print("Ciphertext: ", "".join(CipherText))

def main():
message = "KRISHA"
key = "HILLMAGICVALLY"
HillCipher(message, key)

if name == " main ":


main()

Output:

Conclusion:
In conclusion, our exploration of the Hill cipher revealed its effectiveness as a modern encryption
technique. By utilizing matrices and modular arithmetic, the Hill cipher offers a robust method for
encrypting messages. Its ability to handle blocks of characters enhances security by reducing patterns
in the ciphertext. However, challenges may arise with key selection, especially concerning the
necessity of invertible matrices. Overall, the Hill cipher serves as a valuable tool for understanding
matrix-based encryption methods and their applications in modern cryptography.
220220131111
Quiz:

1. What preliminary knowledge required for Hill cipher ?


Ans: The preliminary knowledge required for the Hill cipher includes:
1. Basic understanding of linear algebra, including matrices and matrix
operations 2.Familiarity with modular arithmetic.
3.Knowledge of encryption principles and techniques.
4.Understanding of key generation and management.
5.Basic awareness of cryptanalysis techniques.

2. Hill cipher is example of which type of cipher technique?


Ans: The Hill cipher is an example of a polygraphic substitution cipher technique, where groups of
letters are substituted using matrix multiplication.

Suggested Reference:
1. https://crypto.interactive-maths.com/hill-cipher.html

References used by the students:


1. https://www.geeksforgeeks.org/

Rubric wise marks obtained:

Rubrics 1 2 3 4 5 Total
Marks
220220131111

Experiment No: 4
Implementation of Vigenère cipher
Date:
Relevant CO: Explore the basic principles of the symmetric cryptography and techniques with their
strengths and weaknesses from perspective of cryptanalysis

Objectives: (a) to understand working fundamental of Vigenère Cipher.


(b) to carry out Implementation of Vigenere cipher encryption-decryption.

Equipment/Instruments: Computer System, Turbo-c/ JDK


Theory: To encrypt, a table of alphabets can be used, termed a tabula recta, Vigenère square, or Vigenère
table. It consists f the alphabet written out 26 times in differ nt rows, each alphabet shifted cyclically to the
left compared to the previous alphabet, corresponding to the 26 possible Caesar ciphers. At different points
in the encryption process, the cipher uses a different alphabet from one of the rows.
The alphabet used at each point repeating keyword depends on a Each row starts with a key letter. The
remainder of the row holds the letters A to Z. Although there are 26 key rows shown, you will only use as
many keys as there are unique letters in the key string, here just 5 keys, {L, E, M, O, N}. For successive
letters of the message, we are going to take successive letters of the key string, and encipher each message
letter using its corresponding key row. Choose the next letter of the key, go al ng that row to find the column
heading that atches the message character; the letter at the intersection of [key-row, msg-col] is the
enciphered letter.

Example:

Algorithm:
STEP-1: Arrange the alphabets in row and column of a 26*26 matrix.
STEP-2: Circulate the alphabets in each row to position left such that the first letter isattached to last.
STEP-3: Repeat this process for all 26 rows and construct the final key matrix.
STEP-4: The keyword and the plain text is read from the user.
STEP-5: The characters in the keyword are repeated sequentially so as to match withthat of the
220220131111
plain text.

STEP-6: Pick the first letter of the plain text and that of the keyword as the row indicesand column
indices respectively.
STEP-7: The junction character where these two meet forms the cipher character.
STEP-8: Repeat the above steps to generate the entire cipher text.

Program:
def generateKey(string, key):
key = list(key)
if len(string) == len(key):
return key
else:
for i in range(len(string) - len(key)):
key.append(key[i % len(key)])
return "".join(key)

def cipherText(string, key):


cipher_text = []
for i in range(len(string)):
x = (ord(string[i]) + ord(key[i])) % 26
x += ord('A')
cipher_text.append(chr(x))
return "".join(cipher_text)

def originalText(cipher_text, key):


orig_text = []
for i in range(len(cipher_text)):
x = (ord(cipher_text[i]) - ord(key[i]) + 26) % 26
x += ord('A')
orig_text.append(chr(x))
return "".join(orig_text)

if name == " main ":


string = "DESKTOPVI"
keyword = "ATM"
key = generateKey(string, keyword)
cipher_text = cipherText(string, key)

print("Ciphertext:", cipher_text)
print("Original/Decrypted Text:", originalText(cipher_text, key))

Output:
220220131111

Conclusion:
In conclusion, our exploration of the Vigenère cipher highlighted its effectiveness as a
polyalphabetic substitution cipher. By employing a keyword to determine multiple cipher alphabets,
the Vigenère cipher enhances security compared to monoalphabetic ciphers. Despite its historical
significance, the Vigenère cipher is susceptible to frequency analysis and Kasiski examination when
key length is known. However, varying key lengths and selecting non-trivial keywords can
significantly improve its resistance to cryptanalysis. Overall the Vigenère cipher remains an
important cipher in the history of cryptography, demonstrating the evolution of encryption
techniques over time.

Quiz:

1. Encryption in Vigenere cipher is done using a keyword or a keyword phrase.


Suggested Reference:
1. https://intellipaat.com/blog/vigenere-cipher/

References used by the students:

1. https://www.javatpoint.com

Rubric wise marks obtained:

Rubrics 1 2 3 4 5 Total
Marks
220220131111

Experiment No: 5
Implementation of Rail Fence Transposition
Date:
Relevant CO: Explore the basic principles of the symmetric cryptography and techniques with their
strengths and weaknesses from perspective of cryptanalysis

Objectives: (a) to understand working fundamental of Rail Fence Transposition Cipher


(b) to carry out Implementation of Rail Fence Transposition Cipher encryption-
decryption.

Equipment/Instruments: Computer System, Turbo-c/ JDK


Theory:In the rail fence cipher, the plain text is written downwards and diagonally on successive "rails" of
an imaginary fence, then moving up when we reach the bottom rail.When we reach the top rail, the
message is written downwards again until the whole plaintextis written out. The message is then read off in
rows.

Example:

Algorithm:

STEP-1: Read the Plain text.


STEP-2: Arrange the plain text in row columnar matrix format.
STEP-3: Now read the keyword depending on the number of columns of the plain text.
STEP-4: Arrange the characters of the keyword in sorted order and the correspondingcolumns of the
plain text.

STEP-5: Read the characters row wise or column wise in the former order to get thecipher text.

Program:

def encryptRailFence(text, key):


# Create an empty rail fence structure
rail = [['\n' for _ in range(len(text))] for _ in range(key)]
220220131111

# Variables to determine movement direction


dir_down = False
row, col = 0, 0

# Place characters in rail matrix


for i in range(len(text)):
if row == 0 or row == key - 1:
dir_down = not dir_down

rail[row][col] = text[i]
col += 1

if dir_down:
row += 1
else:
row -= 1

# Read the matrix in row-wise manner to get encrypted text


result = []
for i in range(key):
for j in range(len(text)):
if rail[i][j] != '\n':
result.append(rail[i][j])

return "".join(result)

def decryptRailFence(cipher, key):


# Create an empty rail fence structure
rail = [['\n' for _ in range(len(cipher))] for _ in range(key)]

# Variables to determine movement direction


dir_down = None
row, col = 0, 0

# Mark the positions in the rail matrix


for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False

rail[row][col] = '*'
col += 1

if dir_down:
row += 1
else:
row -= 1

# Fill in the characters from cipher text into the matrix


index = 0
for i in range(key):
220220131111

for j in range(len(cipher)):
if rail[i][j] == '*' and index < len(cipher): rail[i]
[j] = cipher[index]
index += 1

# Read the matrix in zig-zag manner to get decrypted text


result = []
row, col = 0, 0
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False

if rail[row][col] != '*':
result.append(rail[row][col])
col += 1

if dir_down:
row += 1
else:
row -= 1

return "".join(result)

# Testing the functions


if name == " main ":
print("Encrypted:", encryptRailFence("hello", 2))
print("Encrypted:", encryptRailFence("lab manual", 2))
print("Decrypted:", decryptRailFence("hloel", 2))
print("Decrypted:", decryptRailFence("lba mnuaal", 2))

Output:

Conclusion:
In conclusion, our exploration of the Rail Fence Transposition cipher demonstrated its simplicity and
effectiveness as a transposition technique. By rearranging characters in a zigzag pattern across
multiple "rails," the cipher provides a basic level of encryption. However, the Rail Fence
Transposition cipher is vulnerable to cryptanalysis techniques such as brute-force attacks and
frequency analysis, particularly for shorter message lengths. Despite its limitations, the cipher serves
as a valuable educational tool in understanding transposition techniques and their role in
cryptography.
220220131111

Quiz:

1. Encryption in Rail fence cipher is done using


a) by arranging the letters in a zig zag fashion in a table
b) by randomly arranging letters
c) by arranging letters in vigenere table
d) by swapping adjacent letters
Ans: a) by arranging the letters in a zigzag fashion in a table

2. Rail fence cipher is more secure than one time pad cipher? True/False
Ans: False

Suggested Reference:
1. https://crypto.interactive-maths.com/rail-fence-cipher.html

References used by the students:

1. https://w3school.com.python

Rubric wise marks obtained:

Rubrics 1 2 3 4 5 Total
Marks

You might also like