-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_encryption.py
More file actions
72 lines (58 loc) · 2.05 KB
/
simple_encryption.py
File metadata and controls
72 lines (58 loc) · 2.05 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
# For building the encrypted string:
# If the input-string is null or empty or n is <= 0 then return the input text.
def encrypt(encrypted_text, n):
if (encrypted_text == None or
len(encrypted_text) <= 0 or
n <= 0):
return encrypted_text
else:
counter = 0
length = len(encrypted_text)
text = encrypted_text
result = ''
while (counter < n):
for i in range(length):
if (i % 2 == 0 or i == 0):
result += text[i]
for i in range(length - 1, 0, -1):
if (i % 2 != 0 and i != 0):
result = text[i] + result
counter += 1
text = result
result = ''
return text
def decrypt(encrypted_text, n):
if (encrypted_text == None or
len(encrypted_text) <= 0 or
n <= 0):
return encrypted_text
else:
length = len(encrypted_text)
first_part, second_part = encrypted_text[:length//2], encrypted_text[length//2:]
text = ''
repititions, counter, first_index, second_index = 0, 0, 0, 0
while repititions < n:
text, counter, first_index, second_index = '', 0, 0, 0
while counter < length:
if counter % 2 == 0 or counter == 0:
text += second_part[first_index]
first_index += 1
if (counter % 2 != 0 and counter != 0):
text += first_part[second_index]
second_index += 1
counter += 1
first_part, second_part = text[:length//2], text[length//2:]
repititions += 1
return text
print(encrypt("This is a test!", 1))
# si etTi sats!
print(encrypt("This is a test!", 2)) #
# s eT ashi tist!
print(decrypt(encrypt("This is a test!", 1), 1)) #
# This is a test!
print(decrypt(encrypt("This is a test!", 2), 2)) #
# This is a test!
print(decrypt("hsi etTi sats!", 1)) #
# This is a test!
print(decrypt("s eT ashi tist!", 2)) #
# This is a test!