-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathone-time-pad.py
More file actions
102 lines (84 loc) · 3.56 KB
/
one-time-pad.py
File metadata and controls
102 lines (84 loc) · 3.56 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
'''
Title: One Time Pad Cipher
Description: Encrypts a message using a key as long as the message and the
Vignere Cipher
Developer: Alexander Beck
Email: beckhv2@gmail.com
Github: https://github.com/bexcoding
.....//\\......//\\......//\\......//\\......//\\......//\\......//\\....../
....// \\....// \\....// \\....// \\....// \\....// \\....// \\....//
\..// /\ \\..// /\ \\..// /\ \\..// /\ \\..// /\ \\..// /\ \\..// /\ \\..//
\\// / \ \\// / \ \\// / \ \\// / \ \\// / \ \\// / \ \\// / \ \\// /
|| | <> | || | <> | > | || | <> | || |
|| | <> | || | <> | || || // > | || | <> | || |
/\ \ / /\ \ / || || // / /\ \ / /\ \
/ \ \/ / \ \/ ||____ ___ ___ || // / / \ \/ / \
<> | || | <> | || || \\ // \\ // \\ |||| | | <> | || | <> |
<> | || | <> | || || || ||____|| || || \\ | | <> | || | <> |
\ / || \ / || || || || || || \\ | \ / || \ /
\/ //\\ \/ //\\ ||____// \\___// \\___// || \\ \\ \/ //\\ \/ /
//..\\ //..\\ \\ //..\\ //
\ //....\\ //....\\ //....\\ //....\\ //....\\ //....\\ //....\\ //.
\\//......\\//......\\//......\\//......\\//......\\//......\\//......\\//..
'''
import string
import random
CHARACTERS = string.ascii_uppercase
CHAR_LENGTH = len(CHARACTERS)
def encrypt(old_index, cipher_key):
"""
int, int -> int
takes the old index and adds it with the cipher key to get new index
old_index: original index value from CHARACTERS
cipher_key: value to shift original index by
assumes: old_index and cipher_key >= 0 and <= (len(CHARACTERS) - 1)
"""
new_index = old_index + cipher_key
if new_index > (CHAR_LENGTH - 1):
new_index -= CHAR_LENGTH
return new_index
def decrypt(old_index, cipher_key):
"""
int, int -> int
takes the old index and subtracts from it the cipher key to get new index
old_index: original index value from CHARACTERS
cipher_key: value to shift original index by
assumes: old_index and cipher_key >= 0 and <= (len(CHARACTERS) - 1)
"""
new_index = old_index - cipher_key
if new_index < 0:
new_index += CHAR_LENGTH
return new_index
def one_time_pad_cipher(message, mode, cipher_key):
"""
str, str, int -> str
adjusts the message with the key based on the mode chosen
message: the message to be encrypted or decrypted
mode: either "encrypt" or "decrypt"
cipher_key: string that evaluates to several keys to shift original index
assumes: mode will be either "encrypt" or "decrypt"
"""
cipher_key = cipher_key.upper()
message = message.upper()
current_cipher_index = 0
new_message = []
for letter in message:
if letter in CHARACTERS:
original_index = CHARACTERS.find(letter)
current_cipher_value = CHARACTERS.find(cipher_key[current_cipher_index])
if mode == "encrypt":
modified_index = encrypt(original_index, current_cipher_value)
elif mode == "decrypt":
modified_index = decrypt(original_index, current_cipher_value)
new_message.append(CHARACTERS[modified_index])
current_cipher_index += 1
return "".join(new_message)
def generate_key():
"""
() -> str
creates a cipher key 250 characters long using uppercase letters
"""
new_key = []
for i in range(250):
new_key.append(random.choice(CHARACTERS))
return "".join(new_key)