-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcipher.py
More file actions
119 lines (103 loc) · 4.19 KB
/
cipher.py
File metadata and controls
119 lines (103 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import math
#MonoAlphabetic Cipher
#Caesar Cipher
def CCencrypt(string, CCkey):
return ''.join([chr((ord(letter) + CCkey - 97) % 26 + 97) for letter in string])
def multiplicative(string, key):
return ''.join([chr(((ord(letter) - 97) * key) % 26 + 97) for letter in string])
def affine(string, key, key2):
string = string.lower()
return ''.join([chr((((ord(letter) - 97) * key) + key2) % 26 + 97) for letter in string])
def CCdecrypt(string, CCkey):
return ''.join([chr((ord(letter) - CCkey - 97) % 26 + 97) for letter in string])
#PolyAlphabetic Cipher [Vigenere, Autokey, Playfair]
#Vigenere Cipher
def vigenere_encrypt(msg, key):
msg = msg.replace(' ', '').lower()
key = key.replace(' ', '').lower()
return "".join([chr((((ord(msg[i])-97)+(ord(key[i%len(key)])-97))%26)+97) for i in range(len(msg))])
#Autokey Cipher
def autokey_encrypt(msg,key):
cipher = chr((ord(msg[0])-97 + int(key))%26 + 97)
return cipher+''.join([chr((ord(msg[i])-97 + ord(msg[i-1])-97)%26 + 97)for i in range(1,len(msg))])
#playfair cipher
def playfair(msg,keyword):
msg = msg.replace(' ', '').lower()
keyword = keyword.replace(' ', '').lower()
key = ''
key_matrix = []
for letter in keyword:
if letter not in key:
key += letter
for i in range(26):
if chr(i+97) not in key:
key += chr(i+97)
i = 0
key = key.replace('j','')
while i < len(key):
key_matrix.append(key[i:i+5])
i += 5
i = 0
while i < len(msg):
if len(msg[i:]) > 1:
if msg[i] == msg[i+1]:
msg = msg[:i+1] +'x'+msg[i+1:]
else:
msg += 'z'
i += 2
cipher = ''
i = 0
while i < len(msg):
if msg[i] == 'j':
msg[i] = 'i'
col1 = ((key.index(msg[i])+1) % 5) -1
row1 = math.ceil((key.index(msg[i])+1)/5) -1
if msg[i+1] == 'j':
msg[i+1] = 'i'
col2 = ((key.index(msg[i+1])+1) % 5) -1
row2 = math.ceil((key.index(msg[i+1])+1)/5) - 1
if row1 == row2:
col1 = (col1 + 1) % 5
col2 = (col2 + 1) % 5
cipher += key_matrix[row1][col1] + key_matrix[row2][col2]
elif col1 == col2:
row1 = (row1 + 1) % 5
row2 = (row2 + 1) % 5
cipher += key_matrix[row1][col1] + key_matrix[row2][col2]
else:
cipher += key_matrix[row1][col2] + key_matrix[row2][col1]
i += 2
return msg,cipher,key_matrix
#transposition Cipher [Keyed]
def columnar_transposition(msg,key):
blocks = []
key_length = len(key)
i = 0
if len(key) != len(set(key)): return "Error in provided key, It contains some repitition of digits!",None,None
if '0' in set(key): return "Error in provided key, It contains column number 0 but column starts from 1! Don't use 0 it is reserved for PROGRAMMERS only.",None,None
key_cipher = ''
if len(msg)%len(key) != 0: #Balancing letters in message. making it multiple of key by adding bogus character '_'
msg += '_'*(len(key) - len(msg)%len(key))
while (i < len(msg)): #dividing in blocks
blocks.append(msg[i:i+key_length])
i+=key_length
for index in key: #keyed encryption
for block in blocks:
key_cipher+= block[int(index)-1]
return None,msg, key_cipher
#transposition Cipher [keyless]
def keyless_transposition(msg,block_size):
msg = msg.replace(' ','_')
blocks = []
key_length = block_size
i = 0
key_cipher = ''
if len(msg)%key_length != 0: #Balancing letters in message. making it multiple of key by adding bogus character '_'
msg += '_'*(key_length - len(msg)%key_length)
while (i < len(msg)): #dividing in blocks
blocks.append(msg[i:i+key_length])
i+=key_length
transpose_matrix = ["".join([blocks[j][i] for j in range(len(blocks))]) for i in range(len(blocks[0]))]
print(transpose_matrix)
keyless_cipher = "".join([block for block in transpose_matrix])
return msg,keyless_cipher #, key_cipher