forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautokey.py
More file actions
Latest commit
150 lines (136 loc) · 4.64 KB
/
Copy pathautokey.py
File metadata and controls
150 lines (136 loc) · 4.64 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
https://en.wikipedia.org/wiki/Autokey_cipher
An autokey cipher (also known as the autoclave cipher) is a cipher that
incorporates the message (the plaintext) into the key.
The key is generated from the message in some automated fashion,
sometimes by selecting certain letters from the text or, more commonly,
by adding a short primer key to the front of the message.
"""
defencrypt(plaintext: str, key: str) ->str:
"""
Encrypt a given `plaintext` (string) and `key` (string), returning the
encrypted ciphertext.
>>> encrypt("hello world", "coffee")
'jsqqs avvwo'
>>> encrypt("coffee is good as python", "TheAlgorithms")
'vvjfpk wj ohvp su ddylsv'
>>> encrypt("coffee is good as python", 2)
Traceback (most recent call last):
...
TypeError: key must be a string
>>> encrypt("", "TheAlgorithms")
Traceback (most recent call last):
...
ValueError: plaintext is empty
>>> encrypt("coffee is good as python", "")
Traceback (most recent call last):
...
ValueError: key is empty
>>> encrypt(527.26, "TheAlgorithms")
Traceback (most recent call last):
...
TypeError: plaintext must be a string
"""
ifnotisinstance(plaintext, str):
raiseTypeError("plaintext must be a string")
ifnotisinstance(key, str):
raiseTypeError("key must be a string")
ifnotplaintext:
raiseValueError("plaintext is empty")
ifnotkey:
raiseValueError("key is empty")
key+=plaintext
plaintext=plaintext.lower()
key=key.lower()
plaintext_iterator=0
key_iterator=0
ciphertext=""
whileplaintext_iterator<len(plaintext):
if (
ord(plaintext[plaintext_iterator]) <97
orord(plaintext[plaintext_iterator]) >122
):
ciphertext+=plaintext[plaintext_iterator]
plaintext_iterator+=1
eliford(key[key_iterator]) <97orord(key[key_iterator]) >122:
key_iterator+=1
else:
ciphertext+=chr(
(
(ord(plaintext[plaintext_iterator]) -97+ord(key[key_iterator]))
-97
)
%26
+97
)
key_iterator+=1
plaintext_iterator+=1
returnciphertext
defdecrypt(ciphertext: str, key: str) ->str:
"""
Decrypt a given `ciphertext` (string) and `key` (string), returning the decrypted
ciphertext.
>>> decrypt("jsqqs avvwo", "coffee")
'hello world'
>>> decrypt("vvjfpk wj ohvp su ddylsv", "TheAlgorithms")
'coffee is good as python'
>>> decrypt("vvjfpk wj ohvp su ddylsv", "")
Traceback (most recent call last):
...
ValueError: key is empty
>>> decrypt(527.26, "TheAlgorithms")
Traceback (most recent call last):
...
TypeError: ciphertext must be a string
>>> decrypt("", "TheAlgorithms")
Traceback (most recent call last):
...
ValueError: ciphertext is empty
>>> decrypt("vvjfpk wj ohvp su ddylsv", 2)
Traceback (most recent call last):
...
TypeError: key must be a string
"""
ifnotisinstance(ciphertext, str):
raiseTypeError("ciphertext must be a string")
ifnotisinstance(key, str):
raiseTypeError("key must be a string")
ifnotciphertext:
raiseValueError("ciphertext is empty")
ifnotkey:
raiseValueError("key is empty")
key=key.lower()
ciphertext_iterator=0
key_iterator=0
plaintext=""
whileciphertext_iterator<len(ciphertext):
if (
ord(ciphertext[ciphertext_iterator]) <97
orord(ciphertext[ciphertext_iterator]) >122
):
plaintext+=ciphertext[ciphertext_iterator]
else:
plaintext+=chr(
(ord(ciphertext[ciphertext_iterator]) -ord(key[key_iterator])) %26
+97
)
key+=chr(
(ord(ciphertext[ciphertext_iterator]) -ord(key[key_iterator])) %26
+97
)
key_iterator+=1
ciphertext_iterator+=1
returnplaintext
if__name__=="__main__":
importdoctest
doctest.testmod()
operation=int(input("Type 1 to encrypt or 2 to decrypt:"))
ifoperation==1:
plaintext=input("Typeplaintext to be encrypted:\n")
key=input("Type the key:\n")
print(encrypt(plaintext, key))
elifoperation==2:
ciphertext=input("Type the ciphertext to be decrypted:\n")
key=input("Type the key:\n")
print(decrypt(ciphertext, key))
decrypt("jsqqs avvwo", "coffee")