Crypto
Crypto
Crypto
Playfair Cipher
def generate_key_matrix(key):
matrix = []
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
key_string += char
return matrix
def format_message(message):
formatted_message = ""
i=0
formatted_message += message[i]
formatted_message += "X"
if i + 1 < len(message):
formatted_message += message[i + 1]
i += 2
if len(formatted_message) % 2 != 0:
formatted_message += "X"
return formatted_message
for i in range(5):
for j in range(5):
if matrix[i][j] == char:
return i, j
return None
def playfair_encrypt(key, message):
matrix = generate_key_matrix(key)
message = format_message(message)
cipher_text = ""
if row1 == row2:
else:
return cipher_text
2. Hill Cipher
import numpy as np
return matrix
n = int(len(key) ** 0.5)
key_matrix = create_matrix_of_integers_from_string(key, n)
message_matrix = create_matrix_of_integers_from_string(message, n)
encrypted_message = ""
return encrypted_message
key = input("Enter the Hill Cipher key (perfect square length): ")
message = input("Enter the message to encrypt (length should match key size): ")
3. Vigenère Cipher
key = key.upper()
cipher_text = ""
key_length = len(key)
for i in range(len(plain_text)):
return cipher_text
result = 1
exponent = exponent // 2
return result
5. S-box (DES)
def s_box_substitution(input_bits):
s_box = [
row = int(row_bits, 2)
col_bits = input_bits[1:5]
col = int(col_bits, 2)
s_box_value = s_box[row][col]
return s_box_result
else:
result = s_box_substitution(input_bits)
def s_des_round_key_generation(key):
P8 = [6, 3, 7, 4, 8, 5, 10, 9]
left_half = permuted_key[:5]
right_half = permuted_key[5:]
left_half_ls1 = left_shift(left_half, 1)
right_half_ls1 = left_shift(right_half, 1)
K1 = apply_permutation(combined_ls1, P8)
left_half_ls2 = left_shift(left_half_ls1, 2)
right_half_ls2 = left_shift(right_half_ls1, 2)
K2 = apply_permutation(combined_ls2, P8)
return K1, K2
else:
K1, K2 = s_des_round_key_generation(key)
return ''.join(['1' if a_bit != b_bit else '0' for a_bit, b_bit in zip(a, b)])
def s_box_substitution(expanded_half):
col0 = int(expanded_half[1:3], 2)
col1 = int(expanded_half[5:7], 2)
ep_table = [4, 1, 2, 3, 2, 3, 4, 1]
s_box_result = s_box_substitution(xor_result)
p4_table = [2, 4, 3, 1]
return p4_result
ip_table = [2, 6, 3, 1, 4, 8, 5, 7]
left_half = ip_plaintext[:4]
right_half = ip_plaintext[4:]
ip_inv_table = [4, 1, 3, 5, 7, 2, 8, 6]
return ciphertext
ip_table = [2, 6, 3, 1, 4, 8, 5, 7]
ip_ciphertext = apply_permutation(ciphertext, ip_table)
left_half = ip_ciphertext[:4]
right_half = ip_ciphertext[4:]
ip_inv_table = [4, 1, 3, 5, 7, 2, 8, 6]
return plaintext
plaintext = "10101010"
key1 = "10100100"
key2 = "01010011"
print(f"Ciphertext: {ciphertext}")
while b:
a, b = b, a % b
return a
d_old, d_new = 0, 1
while r_new != 0:
if r_old > 1:
if d_old < 0:
d_old += phi
return d_old
n=p*q
phi = (p - 1) * (q - 1)
while True:
e = int(input(f"Enter an integer e such that 1 < e < {phi} and gcd(e, {phi}) = 1: "))
break
else:
print(f"Invalid value for e. Make sure 1 < e < {phi} and gcd(e, {phi}) = 1.")
d = mod_inverse(e, phi)
while b:
a, b = b, a % b
return a
d_old, d_new = 0, 1
while r_new != 0:
if r_old > 1:
raise Exception("No modular inverse found")
if d_old < 0:
d_old += phi
return d_old
def generate_rsa_keys():
n=p*q
phi = (p - 1) * (q - 1)
while True:
e = int(input(f"Enter an integer e such that 1 < e < {phi} and gcd(e, {phi}) = 1: "))
break
else:
print(f"Invalid value for e. Make sure 1 < e < {phi} and gcd(e, {phi}) = 1.")
d = mod_inverse(e, phi)
public_key = (e, n)
private_key = (d, n)
e, n = public_key
return encrypted_message
d, n = private_key
return decrypted_message
if __name__ == "__main__":
while b:
a, b = b, a % b
return a
d_old, d_new = 0, 1
while r_new != 0:
if r_old > 1:
if d_old < 0:
d_old += phi
return d_old
def generate_rsa_keys():
n=p*q
phi = (p - 1) * (q - 1)
while True:
e = int(input(f"Enter an integer e such that 1 < e < {phi} and gcd(e, {phi}) = 1: "))
if 1 < e < phi and gcd(e, phi) == 1:
break
else:
print(f"Invalid value for e. Make sure 1 < e < {phi} and gcd(e, {phi}) = 1.")
d = mod_inverse(e, phi)
public_key = (e, n)
private_key = (d, n)
e, n = public_key
return encrypted_message
d, n = private_key
return decrypted_message
e, n = public_key
modified_plaintext])
return original_plaintext
if __name__ == "__main__":
encrypted_message)
import random
result = 1
if exp % 2 == 1:
exp = exp // 2
return result
def generate_large_prime():
primes = [17, 23, 47, 59, 61, 71, 89, 101, 103, 107]
return random.choice(primes)
def elgamal_key_generation():
p = generate_large_prime()
g = random.randint(2, p - 1)
x = random.randint(1, p - 2)
y = mod_exp(g, x, p)
public_key = (p, g, y)
private_key = x
p, g, y = public_key
k = random.randint(1, p - 2)
c1 = mod_exp(g, k, p)
return ciphertext
c1, c2 = ciphertext
x = private_key
s = mod_exp(c1, x, p)
s_inv = mod_exp(s, p - 2, p)
return decrypted_message
if __name__ == "__main__":
message = int(input(f"Enter the message to encrypt (an integer less than {public_key[0]}): "))