-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDouble_Transposition_Cipher.py
82 lines (75 loc) · 1.82 KB
/
Double_Transposition_Cipher.py
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
def encryptr_f(text, key):
r = [['\n' for i in range(len(text))]
for j in range(key)]
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
r[row][col] = text[i]
col += 1
if dir_down:
row += 1
else:
row -= 1
result = []
for i in range(key):
for j in range(len(text)):
if r[i][j] != '\n':
result.append(r[i][j])
return("" . join(result))
def decryptr_f(cipher, key):
r = [['\n' for i in range(len(cipher))]
for j in range(key)]
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
r[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 ((r[i][j] == '*') and
(index < len(cipher))):
r[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 (r[row][col] != '*'):
result.append(r[row][col])
col += 1
if dir_down:
row += 1
else:
row -= 1
return("".join(result))
str1=input("Enter Plain Text:")
k=int(input("Enter key:"))
encrypt=encryptr_f(str1,k)
decrypt=decryptr_f(encrypt,k)
print("your encrypted text is")
print(encrypt)
print("your decrypted text is")
print(decrypt)
#This is double transposition cipher
str1=input("Enter Plain Text:")
k=int(input("Enter key:"))
encrypt=encryptr_f(encryptr_f(str1,k),k)
decrypt=decryptr_f(decryptr_f(encrypt,k),k)
print("your encrypted text is")
print(encrypt)
print("your decrypted text is")
print(decrypt)