-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.py
49 lines (36 loc) · 1.33 KB
/
otp.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
# https://codereview.stackexchange.com/questions/116044/one-time-pad-algorithm-for-encryption-and-decryption
from random import choice
import string
def convert_to_bits(s):
"""Converts string s to a string containing only 0s or 1s,
representing the original string."""
return "".join(format(ord(x), 'b') for x in s)
def gen_random_key(n):
"""Generates a random key of bits (with 0s or 1s) of length n"""
k = []
for i in range(n):
k.append(choice(["0", "1"]))
return "".join(k)
def xor(m, k):
"""Given strings m and k of characters 0 or 1,
it returns the string representing the XOR
between each character in the same position.
This means that m and k should be of the same length.
Use this function both for encrypting and decrypting!"""
r = []
for i, j in zip(m, k):
r.append(str(int(i) ^ int(j))) # xor between bits i and j
return "".join(r)
if __name__ == "__main__":
ls = []
for i in range(100):
for i in range(100):