Answers for "how to build a cipher in python"

1

python vernam cipher

vernam_dict = dict((i, chr(i + 96)) for i in range(1, 27))
# Vernam by replacing char of plain by char(ord(sum of plain and key))
def vernam_encrypt(plain, key):
    plain = plain.lower()
    ckey = ''.join([(key[i % len(key)]) for i in range(len(list(plain)))])
    print(ckey)
    cipher = ''
    for i in range(len(plain)):
        if plain[i] == ' ':
            cipher += ' '
        else:
            cipher += vernam_dict[(ord(plain[i]) + ord(ckey[i])) % 26]
    print(cipher, plain)

print(vernam_encrypt('mountains are bae', 'hello'))

def vernam_decrypt(ctext, key):
    cupper = ctext.upper()
    text_num = [letters.index(u) for u in cupper]
    intm_key = [letters.index(ik) for ik in key]
    c = ''
    for i in range(len(cupper)):
        ee = text_num[i] - intm_key[i]
        if ee < 0:
            c += letters[ee + 26]
        else:
            c += letters[ee]
    return c
  
print(vernam_decrypt('BSJDE', 'UOYSQ'))
Posted by: Guest on November-24-2020
0

caesar cipher python

def cc_encrypt(msg: str,key: int) -> str:
    encrypted_msg = ''

    try:
        for char in msg:
            encrypted_msg += str(chr(ord(char)+int(key)))
    except:
        print(Exception)
        pass
    
    return encrypted_msg

def cc_decrypt(msg: str,key: int) -> str:
    decrypted_msg = ''

    try:
        for char in msg:
            decrypted_msg += chr(ord(char)-int(key))
    except:
        print(Exception)
        pass

    return decrypted_msg

  
message = 'Hello World!'
key = 9
print(f'Caesar Cipher:nEncrypted: {cc_encrypt(message,key)}nDecrypted: {cc_decrypt(cc_encrypt(message,key),key)}')
Posted by: Guest on December-03-2020

Code answers related to "how to build a cipher in python"

Python Answers by Framework

Browse Popular Code Answers by Language