summaryrefslogtreecommitdiff
path: root/oneTimePad.py
blob: 7f2e1eb152086ea69bd667e1d0e6492ffa9832b1 (plain)
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
def alphabetEncrypt():
    inp = input("Enter a message: ")
    key = input("Enter key: ")
    output = ""
    for i in range(len(inp)):
        output = output + chr((ord(inp[i])+ord(key[i])-194)%26 + 97)
    print(output)

def alphabetDecrypt():
    inp = input("Enter a ciphertext: ")
    key = input("Enter the key: ")
    output = ""
    for i in range(len(inp)):
        working = ord(inp[i])-ord(key[i])
        if working < 0:
            working += 26
        else:
            working %= 26
        working += 97
        output = output + chr(working)
    print(output)

def xorEncryptDecrypt(inp):
    key = input("Enter a key: ")
    inpb = ""
    for char in inp:
        inpb = inpb + format(ord(char), "08b")
    keyb = ""
    for char in key:
        keyb = keyb + format(ord(char), "08b")
    xor = int(inpb, 2) ^ int(keyb, 2)
    result = bin(xor)[2:].zfill(len(inpb))
    chunks = []
    for i in range(0, len(result), 8):
        chunks.append(result[i:i+8])
    print(chunks)
    output = ""
    for chunk in chunks:
        output = output + (chr(int(chunk,2)))
    return output

alphabetEncrypt()
alphabetDecrypt()
output  = xorEncryptDecrypt("Hello")
print(output)
print("\n\n",xorEncryptDecrypt(output))