Alex-Just
12/12/2016 - 9:53 PM

Simple encrypt/decrypt algorithm (Vigenere cipher)

Simple encrypt/decrypt algorithm (Vigenere cipher)

#!/usr/bin/env python3

import base64

"""
Simple obfuscation that will obscure things from the very casual observer.
It is one of the strongest of the simple ancient ciphers.
https://en.wikipedia.org/wiki/Vigenère_cipher
"""


def encrypt(key, string):
    encoded_chars = []
    for i in range(len(string)):
        key_c = key[i % len(key)]
        encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
        encoded_chars.append(encoded_c)
    encoded_string = ''.join(encoded_chars)
    return base64.b64encode(bytes(encoded_string, 'utf-8')).decode('utf-8', 'ignore')


def decrypt(key, string):
    decoded_chars = []
    string = base64.b64decode(bytes(string, 'utf-8')).decode('utf-8', 'ignore')
    for i in range(len(string)):
        key_c = key[i % len(key)]
        encoded_c = chr(abs(ord(string[i]) - ord(key_c) % 256))
        decoded_chars.append(encoded_c)
    decoded_string = ''.join(decoded_chars)
    return decoded_string