Advance Cryptography
with Python
by
[Link] Mahdi
PH.D in Cyber security
Sem 2
Lab 5
A5-1 Algorithm 1
import numpy as np
reg_1 = [Link]
reg_2 = [Link]
reg_3 = [Link]
def user_input():
usrinput = [Link](list(map(int,(input("Enter the 64 bit:
").strip().split()))), dtype=bool)
print(usrinput)
print(type(usrinput))
if len(usrinput) == 64:
return usrinput
else:
while len(usrinput) != 64:
if len(usrinput) == 64:
return usrinput
usrinput = [Link](list(map(int,(input("Enter the
64 bit: ").strip().split()))), dtype=bool)
return usrinput
def load_key(key):
global reg_1
global reg_2
global reg_3
reg_1 = key[0:19]
reg_2 = key[19:41]
reg_3 = key[41:64]
print("registers loaded succesfully")
def get_majority(a,b,c):
if int(a) + int(b) + int(c) > 1:
return True
else:
return False
def clock_a5(clocking):
c = clocking
global reg_1
global reg_2
global reg_3
while c != 0:
majority = get_majority(reg_1[8], reg_2[10], reg_3[10])
# print(majority)
if reg_1[8] == majority:
first_bit = int(reg_1[18]) ^ int(reg_1[17]) ^
int(reg_1[16]) ^ int(reg_1[13])
temp_arr1 = np.empty_like(reg_1)
temp_arr1[0] = first_bit
temp_arr1[1:] = reg_1[:18]
reg_1 = temp_arr1
if reg_2[10] == majority:
first_bit = int(reg_2[20]) ^ int(reg_2[21])
temp_arr2 = np.empty_like(reg_2)
temp_arr2[0] = first_bit
temp_arr2[1:] = reg_2[:21]
reg_2 = temp_arr2
if reg_3[10] == majority:
first_bit = int(reg_3[20]) ^ int(reg_3[21]) ^
int(reg_3[22])
temp_arr3 = np.empty_like(reg_3)
temp_arr3[0] = first_bit
temp_arr3[1:] = reg_3[:22]
reg_3 = temp_arr3
#calculating final output bit
output = int(reg_1[18]) ^ int(reg_2[21]) ^
int(reg_3[22])
print(int(output), end= '')
c -= 1
def main():
key = user_input()
load_key(key)
clocking = int(input("Please enter the number of times to
clock the Stream Generator: "))
clock_a5(clocking)
main()
#1 1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1 1 0 1 0 1 1 1 0 0 0 1 1 0 0
1 0 1 1 0 1 0 0 1 0 0 1 0 0 0 1 1 0 1 1 0 1 1 0 0 1 0 1 1 0 1 1
1
A5-1 Algorithm 2
import re
import copy
import sys
x_length = 19
y_length = 22
z_length = 23
key_one = ""
x = []
y = []
z = []
def load_registers(key):
i = 0
while (i < x_length):
[Link](i, int(key[i])) # takes first 19 elements
from key
i = i + 1
j = 0
p = x_length
while (j < y_length):
[Link](j, int(key[p])) # takes next 22 elements from
key
p = p + 1
j = j + 1
k = y_length + x_length
r = 0
while (r < z_length):
[Link](r, int(key[k])) # takes next 23 elements from
key
k = k + 1
r = r + 1
def set_key(key):
if (len(key) == 64 and [Link]("^([01])+", key)):
key_one = key
load_registers(key)
return True
return False
def get_key():
return key_one
def to_binary(plain):
s = ""
i = 0
for i in plain:
binary = str(' '.join(format(ord(x), 'b') for x in i))
j = len(binary)
while (j < 8):
binary = "0" + binary
s = s + binary
j = j + 1
binary_values = []
k = 0
while (k < len(s)):
binary_values.insert(k, int(s[k]))
k = k + 1
return binary_values
def get_majority(x, y, z):
if (x + y + z > 1):
return 1
else:
return 0
def get_keystream(length):
x_temp = [Link](x)
y_temp = [Link](y)
z_temp = [Link](z)
keystream = []
i = 0
while i < length:
majority = get_majority(x_temp[8], y_temp[10],
z_temp[10])
if x_temp[8] == majority:
new = x_temp[13] ^ x_temp[16] ^ x_temp[17] ^
x_temp[18]
x_temp_two = [Link](x_temp)
j = 1
while (j < len(x_temp)):
x_temp[j] = x_temp_two[j-1]
j = j + 1
x_temp[0] = new
if y_temp[10] == majority:
new_one = y_temp[20] ^ y_temp[21]
y_temp_two = [Link](y_temp)
k = 1
while (k < len(y_temp)):
y_temp[k] = y_temp_two[k-1]
k = k + 1
y_temp[0] = new_one
if z_temp[10] == majority:
new_two = z_temp[7] ^ z_temp[20] ^ z_temp[21] ^
z_temp[22]
z_temp_two = [Link](z_temp)
m = 1
while (m < len(z_temp)):
z_temp[m] = z_temp_two[m-1]
m = m + 1
z_temp[0] = new_two
[Link](i, x_temp[18] ^ y_temp[21] ^
z_temp[22])
i = i + 1
return keystream
def to_str(binary):
s = ""
length = len(binary) - 8
i = 0
while (i <= length):
s = s + chr(int(binary[i:i+8], 2))
i = i + 8
return str(s)
def encrypt(plain):
s = ""
binary = to_binary(plain)
keystream = get_keystream(len(binary))
i = 0
while (i < len(binary)):
s = s + str(binary[i] ^ keystream[i])
i = i + 1
return s
def decrypt(cipher):
s = ""
binary = []
keystream = get_keystream(len(cipher))
i = 0
while (i < len(cipher)):
[Link](i, int(cipher[i]))
s = s + str(binary[i] ^ keystream[i])
i = i + 1
return to_str(str(s))
def inp_key():
tha_key = str(input('Enter a 64-bit key: '))
if (len(tha_key) == 64 and [Link]("^([01])+", tha_key)):
return tha_key
else:
while (len(tha_key) != 64 and not [Link]("^([01])+",
tha_key)):
if (len(tha_key) == 64 and [Link]("^([01])+",
tha_key)):
return tha_key
tha_key = str(input('Enter a 64-bit key: '))
return tha_key
def choice():
inp = str(
input('\n[1]: Implement A51 Cipher\n[2]: Exit\nOption:
'))
if (inp == '1' or inp == '2'):
return inp
else:
while (inp != '1' or inp != '2'):
if (inp == '1' or inp == '2'):
return inp
inp = str(
input('\n[1]: Implement A51 Cipher\n[2]:
Exit\nOption: '))
return inp
def text():
try:
inp = str(input('Enter the plaintext: '))
except:
inp = str(input('Try again: '))
return inp
def main():
key = str(inp_key())
set_key(key)
first_choice = choice()
if (first_choice == '1'):
plaintext = str(text())
print("\nPlainText:", plaintext)
a = encrypt(plaintext)
print("\nEncrypted Text:", a)
ciphertext = a
print("\nDecrypted Text:", decrypt(ciphertext))
elif (first_choice == '2'):
print("\nTerminal Closed")
[Link](0)
# 64-bit key:
110101100101001011010111000110010110100100100011011011001011011
1
main()