Last active
June 8, 2022 14:57
-
-
Save Alex-Just/ba40bde7d65a73aa7f2d62b5e7f75039 to your computer and use it in GitHub Desktop.
Simple encrypt/decrypt algorithm (Vigenere cipher)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment