Cryptography File 2023 24
Cryptography File 2023 24
Code:
def main():
input_text = input("\n Enter the plain text:")
key = int(input("\n Enter the key value:"))
length = len(input_text)
encrypted_text = ""
for i in range(length):
encrypted_char = chr(ord(input_text[i]) + key)
if input_text[i].isupper() and encrypted_char > 'Z':
encrypted_char = chr(ord(encrypted_char) - 26)
elif input_text[i].islower() and encrypted_char > 'z':
encrypted_char = chr(ord(encrypted_char) - 26)
encrypted_text += encrypted_char
print(encrypted_char, end="")
if __name__ == "__main__":
main()
Output:
Code:
if row1 == row2:
col1 = (col1 + 1) % 5
col2 = (col2 + 1) % 5
print(key_matrix[row1][col1] + key_matrix[row2][col2], end='')
elif col1 == col2:
row1 = (row1 + 1) % 5
row2 = (row2 + 1) % 5
print(key_matrix[row1][col1] + key_matrix[row2][col2], end='')
else:
print(key_matrix[row1][col2] + key_matrix[row2][col1], end='')
def main():
key_string = input("Enter key: ").upper().replace('J', 'I')
plaintext = input("Enter the plain text: ").upper().replace('J', 'I')
key_remaining = [ch for ch in "ABCDEFGHIKLMNOPQRSTUVWXYZ" if ch not
in key_string]
k=0
key_matrix = [['' for _ in range(5)] for _ in range(5)]
for i in range(5):
for j in range(5):
if k < len(key_string):
key_matrix[i][j] = key_string[k]
k += 1
else:
key_matrix[i][j] = key_remaining.pop(0)
for i in range(len(plaintext)):
if i + 1 == len(plaintext):
playfair_encrypt(plaintext[i], 'X', key_matrix)
else:
if plaintext[i] == plaintext[i + 1]:
playfair_encrypt(plaintext[i], 'X', key_matrix)
else:
playfair_encrypt(plaintext[i], plaintext[i + 1], key_matrix)
i += 1
if __name__ == "__main__":
main()
Output:
Enter key: 4
Enter the plain text: world
Code:
def hill_cipher_encrypt(plain_text, encryption_matrix):
cipher_text = []
return cipher_text
def main():
encryption_matrix = [[6, 24, 1], [13, 16, 10], [20, 17, 15]]
decryption_matrix = [[8, 5, 10], [21, 8, 21], [21, 12, 8]]
print("Input Values:")
for value in input_values:
print(value, end=' ')
if __name__ == "__main__":
main()
Output:
Enter plain text:
World
Input Values:
22 14 17 11 3
Encrypted Cipher Text:
R E X F F S
Decrypted Cipher Text:
W O R L D X
Program-04
Code:
def vigenere_cipher(text, key, mode='encrypt'):
result = ''
key_length = len(key)
for i in range(len(text)):
char = text[i]
if char.isalpha():
shift = ord(key[i % key_length].upper()) - 65
if mode == 'decrypt':
shift = -shift
result += chr((ord(char.upper()) - 65 + shift) % 26 + 65)
else:
result += char
return result
def main():
print("Vigenere Cipher Demo")
if __name__ == "__main__":
main()
Output:
Vigenere Cipher Demo
Enter the text: Word
Enter the key: 5
Encrypted Text: KCFR
Decrypted Text: WORD
Program-05
Object: Write a python program to implement Rail fence – row & Column Transformation.
Algorithm:
STEP-1: Read the plain text from the user.
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 corresponding
columns of the plain text.
STEP-5: Read the character’s row wise or column wise in the former order to get the
cipher text.
Code:
def rail_fence_cipher(input_string):
cipher_text = ""
# Encryption
for i in range(0, len(input_string), 2):
cipher_text += input_string[i]
for i in range(1, len(input_string), 2):
cipher_text += input_string[i]
return cipher_text
def rail_fence_decipher(cipher_text):
length = len(cipher_text)
# Decryption
decrypted_text = [''] * length
even_index = 0
odd_index = 1
for i in range(num_rows):
decrypted_text[even_index] = cipher_text[i]
even_index += 2
return ''.join(decrypted_text)
def main():
input_string = input("\n\t\t RAIL FENCE TECHNIQUE\n\nEnter the input string: ")
# Encryption
cipher_text = rail_fence_cipher(input_string)
print("\nCipher text after applying rail fence:", cipher_text)
# Decryption
decrypted_text = rail_fence_decipher(cipher_text)
print("Text after decryption:", decrypted_text)
if __name__ == "__main__":
main()
Output:
RAIL FENCE TECHNIQUE
Code:
import random
import math
def is_prime(num):
if num < 2:
return False
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
return False
return True
n=p*q
phi = (p - 1) * (q - 1)
def main():
print("RSA Encryption and Decryption Demo")
if __name__ == "__main__":
main()
OUTPUT:
Enter a Prime number: 3
Enter another prime number: 7
Encryption keys are: 11, 21
Decryption keys are: 11, 21
Program-07
class MD5:
def __init__(self):
self.initial_hash_values = [0x67452301, 0xEFCDAB89, 0x98BADCFE,
0x10325476]
self.f_functions = [self.func0, self.func1, self.func2,
self.func3]
self.M = [1, 5, 3, 7]
self.O = [0, 1, 5, 0]
self.rotations_0 = [7, 12, 17, 22]
self.rotations_1 = [5, 9, 14, 20]
self.rotations_2 = [4, 11, 16, 23]
self.rotations_3 = [6, 10, 15, 21]
self.all_rotations = [self.rotations_0, self.rotations_1,
self.rotations_2, self.rotations_3]
self.k_space = [0] * 64
self.k = None
groups = 1 + (message_length + 8) // 64
padded_message = bytearray(64 * groups)
padded_message[:message_length] = message.encode()
padded_message[message_length] = 0x80
q = message_length + 1
while q < 64 * groups:
padded_message[q] = 0
q += 1
w = 0
while w < 64 * groups:
block = bytearray(padded_message[w:w + 64])
abcd = h.copy()
for p in range(4):
function = self.f_functions[p]
rotations = self.all_rotations[p]
m = self.M[p]
o = self.O[p]
for q in range(16):
g = (m * q + o) % 16
f = abcd[1] + self.rotate_left(abcd[0] +
function(abcd) + self.k[q + 16 * p] + int.from_bytes(block[4 * g:4 * g +
4], 'little'), rotations[q % 4])
abcd[0], abcd[3], abcd[2], abcd[1] = abcd[3],
abcd[2], abcd[1], f
for p in range(4):
h[p] += abcd[p]
w += 64
return h
if __name__ == "__main__":
main()
OUTPUT:
Program-08
def sha1_hash(input_string):
sha1_hash_object = hashlib.sha1(input_string.encode())
return sha1_hash_object.hexdigest()
def main():
print("SHA-1 Hashing Demo")
sha1_result = sha1_hash(input_text)
if __name__ == "__main__":
main()
Output:
SHA-1 Hashing Demo
Enter the text to hash using SHA-1: 7
SHA-1 Hash: 902ba3cda1883801594b6e1b452790cc53948fda
Program-09
def main():
prime = int(input("Enter a prime number (shared prime): "))
primitive_root = int(input("Enter a primitive root modulo the prime: "))
diffie_hellman(prime, primitive_root)
if __name__ == "__main__":
main()
Output:
Enter a prime number (shared prime): 5
Enter a primitive root modulo the prime: 3
Enter private key for user A: 4
Enter private key for user B: 2
Public Key for User A: 1
Public Key for User B: 4
Shared Key for User A: 1
Shared Key for User B: 1
Program-10
def main():
key = b'Sixteen byte key' # 128-bit key
plaintext = b'This is a secret message'
# Encrypt
ciphertext = aes_encrypt(key, plaintext)
print(f'Encrypted Text: {ciphertext.hex()}')
# Decrypt
decrypted_text = aes_decrypt(key, ciphertext)
print(f'Decrypted Text: {decrypted_text.decode("utf-8")}')
if __name__ == "__main__":
main()
Output:
computer pc
Original value: computer pc
Encrypted value: beF17FXyKQdO0luQfezHQg==
Decrypted value: computer pc