19BECE30543 Cns
19BECE30543 Cns
for i in range(len(pt)):
char = pt[i]
if (char.isupper()):
result = result+ chr((ord(char) + k - 65)%26 + 65)
elif(ord(char)==32):
result = result+char
else:
result = result + chr((ord(char) +k - 97)%26 + 97)
return result
def decription(R,k):
result_d = ""
for i in range(len(R)):
char = R[i]
if (char.isupper()):
result_d = result_d + chr((ord(char) - k - 65)%26
+ 65)
elif(ord(char)==32):
result_d = result_d+char
else:
result_d = result_d + chr((ord(char) - k - 97)%26
+ 97)
return result_d
[1]
Cryptography and Network Security 19BECE30543
[2]
Cryptography and Network Security 19BECE30543
for i in range(len(pt)):
char = pt[i]
if (char.isupper()):
result = result + chr((ord(char) + k - 65) % 26 + 65)
elif (ord(char) == 32):
result = result + char
else:
result = result + chr((ord(char) + k - 97) % 26 + 97)
return result
[3]
Cryptography and Network Security 19BECE30543
Output :-
[4]
Cryptography and Network Security 19BECE30543
len_key = len(key)
len_plain = len(plain_text)
row = int(math.ceil(len_plain / len_key))
matrix = [ ['X']*len_key for i in range(row) ]
#Encryption
t = 0
for r in range(row):
for c, ch in enumerate(plain_text[t: t + len_key]):
matrix[r][c] = ch
t += len_key
# print(matrix)
sort_order = sorted([(ch, i) for i, ch in enumerate(key)]) #
to make alphabetically order of chars
# print(sort_order)
cipher_text = ''
for ch, c in sort_order:
for r in range(row):
cipher_text += matrix[r][c]
print("Encryption")
print("Plain text is :", plain_text)
print("Cipher text is:", cipher_text)
#Decryption
t = 0
for c in key_order:
for r,ch in enumerate(cipher_text[t : t+ row]):
matrix_new[r][c] = ch
t += row
[5]
Cryptography and Network Security 19BECE30543
print("Decryption")
print("Cipher text is:",cipher_text)
print("Plain text is :",p_text)
Output :
[6]
Cryptography and Network Security 19BECE30543
[7]
Cryptography and Network Security 19BECE30543
return hex
[8]
Cryptography and Network Security 19BECE30543
# S-box Table
sbox = [[[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0,
7],
[0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3,
8],
[4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5,
0],
[15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6,
13]],
[9]
Cryptography and Network Security 19BECE30543
[10]
Cryptography and Network Security 19BECE30543
# Initial Permutation
pt = permute(pt, initial_perm, 64)
print("After initial permutation", bin2hex(pt))
# Splitting
left = pt[0:32]
right = pt[32:64]
for i in range(0, 16):
# Expansion D-box: Expanding the 32 bits data into 48
bits
right_expanded = permute(right, exp_d, 48)
[11]
Cryptography and Network Security 19BECE30543
# Swapper
if (i != 15):
left, right = right, left
print("Round ", i + 1, " ", bin2hex(left), " ",
bin2hex(right), " ", rk[i])
# Combination
combine = left + right
pt = "123456ABCD132536"
key = "AABB09182736CCDD"
# Key generation
# --hex to binary
key = hex2bin(key)
[12]
Cryptography and Network Security 19BECE30543
# Splitting
left = key[0:28] # rkb for RoundKeys in binary
right = key[28:56] # rk for RoundKeys in hexadecimal
rkb = []
rk = []
for i in range(0, 16):
# Shifting the bits by nth shifts by checking from shift
table
left = shift_left(left, shift_table[i])
right = shift_left(right, shift_table[i])
rkb.append(round_key)
rk.append(bin2hex(round_key))
print("Encryption")
cipher_text = bin2hex(encrypt(pt, rkb, rk))
print("Cipher Text : ", cipher_text)
print("Decryption")
rkb_rev = rkb[::-1]
rk_rev = rk[::-1]
text = bin2hex(encrypt(cipher_text, rkb_rev, rk_rev))
print("Plain Text : ", text)
[13]
Cryptography and Network Security 19BECE30543
Output :-
[14]
Cryptography and Network Security 19BECE30543
dir_down = False
row, col = 0, 0
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
result = []
[15]
Cryptography and Network Security 19BECE30543
dir_down = None
row, col = 0, 0
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
index = 0
for i in range(key):
for j in range(len(cipher)):
if ((rail[i][j] == '*') and
(index < len(cipher))):
rail[i][j] = cipher[index]
index += 1
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] != '*'):
[16]
Cryptography and Network Security 19BECE30543
if dir_down:
row += 1
else:
row -= 1
return ("".join(result))
if __name__ == "__main__":
print(encryptRailFence("attack at once", 2))
print(encryptRailFence("Ldrp ITR ", 3))
print(encryptRailFence("defend the east wall", 3))
Output :-
[17]
Cryptography and Network Security 19BECE30543
result = list()
for c in key: # storing key
if c not in result:
if c == 'J':
result.append('I')
else:
result.append(c)
flag = 0
for i in range(65, 91): # storing other character
if chr(i) not in result:
if i == 73 and chr(74) not in result:
result.append("I")
flag = 1
[18]
Cryptography and Network Security 19BECE30543
[19]
Cryptography and Network Security 19BECE30543
while (1):
choice = int(input("\n 1.Encryption \n 2.Decryption: \n 3.EXIT"))
if choice == 1:
encrypt()
elif choice == 2:
decrypt()
elif choice == 3:
exit()
else:
print("Choose correct choice")
Output :
[20]
Cryptography and Network Security 19BECE30543
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
[21]
Cryptography and Network Security 19BECE30543
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))
def main():
message = "ACT"
key = "GYBNQKURP"
HillCipher(message, key)
if __name__ == "__main__":
main()
Output :
[22]
Cryptography and Network Security 19BECE30543
# Driver code
if __name__ == "__main__":
string = "Ldrp Itr"
keyword = "Xenesis"
key = generateKey(string, keyword)
cipher_text = cipherText(string, key)
print("Ciphertext :", cipher_text)
print("Original/Decrypted Text :",
originalText(cipher_text, key))
Output :
[23]
Cryptography and Network Security 19BECE30543
def main():
message = 'THE GERMAN ATTACK'
key = 'SECRET'
key_new = generate_key(message, key)
cipher_text = cipherText(message, key_new)
original_text = originalText(cipher_text, key_new)
print("Encrypted Text =", cipher_text)
print("Original Text =", original_text)
Output :
[24]
Cryptography and Network Security 19BECE30543
MY_KEY = "ldrp"
while True:
[25]
Cryptography and Network Security 19BECE30543
Output:-
character_list = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOp-
PqQrRsStTuUvVwWxXyYzZ,./\;:"[]{}()_-!@#$%^&*|?<>`~'
[26]
Cryptography and Network Security 19BECE30543
def get_plaintext():
plain_text = input('Enter your message: ')
return plain_text
def load_file(filename):
return contents
return ciphertext
return plaintext
def menu():
[27]
Cryptography and Network Security 19BECE30543
if choice == '1':
sheets = int(input('How many OTP should be generated?
'))
length = int(input('What will be the maximum message
length? '))
generate_otp(sheets, length)
save_file(filename, ciphertext)
menu()
Output :-
[28]
Cryptography and Network Security 19BECE30543
[29]
Cryptography and Network Security 19BECE30543
sample_plaintext_frequencies =
_count_letter_frequencies(sample_plaintext)
encrypted_text_frequencies = _count_letter_frequencies(en-
crypted_text)
decryption_dict = {}
for i in range(0, 26):
decryption_dict[encrypted_text_frequencies[i][0]] =
sample_plaintext_frequencies[i][0].lower()
f = open(dictionary_filepath, "w")
json.dump(decryption_dict, f)
f.close
encrypted_text = _readfile(encrypted_filepath)
f = open(dictionary_filepath, "r")
decryption_dict = json.load(f)
f.close()
decrypted_list = []
decrypted_text = "".join(decrypted_list)
f = open(decrypted_filepath, "w")
f.write(decrypted_text)
f.close()
def _count_letter_frequencies(text):
frequencies = {}
[30]
Cryptography and Network Security 19BECE30543
return sorted_by_frequency
def _readfile(path):
f = open(path, "r")
text = f.read()
f.close()
return text
Output :-
return gcd(b % a, a)
[31]
Cryptography and Network Security 19BECE30543
Output :-
[32]
Cryptography and Network Security 19BECE30543
Output :-
[33]
Cryptography and Network Security 19BECE30543
Output :-
[34]
Cryptography and Network Security 19BECE30543
[35]
Cryptography and Network Security 19BECE30543
[36]
Cryptography and Network Security 19BECE30543
[37]