python vernam cipher

Solutions on MaxInterview for python vernam cipher by the best coders in the world

showing results for - "python vernam cipher"
Norah
10 Aug 2016
1vernam_dict = dict((i, chr(i + 96)) for i in range(1, 27))
2# Vernam by replacing char of plain by char(ord(sum of plain and key))
3def vernam_encrypt(plain, key):
4    plain = plain.lower()
5    ckey = ''.join([(key[i % len(key)]) for i in range(len(list(plain)))])
6    print(ckey)
7    cipher = ''
8    for i in range(len(plain)):
9        if plain[i] == ' ':
10            cipher += ' '
11        else:
12            cipher += vernam_dict[(ord(plain[i]) + ord(ckey[i])) % 26]
13    print(cipher, plain)
14
15print(vernam_encrypt('mountains are bae', 'hello'))
16
17def vernam_decrypt(ctext, key):
18    cupper = ctext.upper()
19    text_num = [letters.index(u) for u in cupper]
20    intm_key = [letters.index(ik) for ik in key]
21    c = ''
22    for i in range(len(cupper)):
23        ee = text_num[i] - intm_key[i]
24        if ee < 0:
25            c += letters[ee + 26]
26        else:
27            c += letters[ee]
28    return c
29  
30print(vernam_decrypt('BSJDE', 'UOYSQ'))