-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateKey.py
33 lines (26 loc) · 930 Bytes
/
createKey.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
import secrets
import string
letters = string.ascii_lowercase
while True:
try:
length = int(input("\nThe key length should be at least as long as the length of the plaintext that will be "
"used.\nKey length: "))
if length < 1:
raise ValueError()
else:
break
except ValueError:
print("That value wasn't valid. Make sure to enter a positive integer.")
key = ""
for letter in range(length):
key += letters[secrets.randbelow(25)]
while True:
try:
fileName = input(
"\nEnter the name of the file that key should be written to (exclude the .txt): ").strip() + ".txt"
with open(fileName, "w") as f:
f.write(key)
break
except OSError:
print("This isn't a valid file name. Try again.")
print("\nThe key with length " + str(length), "has been written to", fileName + "\n")