Last active
September 28, 2018 12:33
-
-
Save supern64/6362f64b681a5af9eb087112d394da5b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# EncryptTools (ENGLISH CHARACTER ONLY!!!) | |
# This version of the format supports numbers and capital letters and is case-sensitive | |
import random | |
from collections import namedtuple | |
EncryptedNamedTuple = namedtuple('Encrypted', 'text, key') | |
char = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] | |
excp = ["'", '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '-', '=', '+', '"', '<', '>', '/', '[', ']', '{', '}', '.', ',', ' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '?'] | |
def encrypt(message): | |
""" | |
Encrypts a message with a random key. Returns a Encrypted object. | |
""" | |
err = True | |
while err is True: | |
try: | |
count = 0 | |
enckey = _pdigitrand(len(message)) | |
split = list(message) | |
enckey2 = list(str(enckey)) | |
enc = [] | |
for i in split: | |
if not i in char and i not in excp: | |
raise ValueError("Characters must be in English only.") | |
if i in excp: | |
enc.append(i) | |
else: | |
l = char.index(i) | |
q = l + int(enckey2[count]) | |
enc.append(char[q]) | |
count = count + 1 | |
except IndexError: | |
err = True | |
else: | |
err = False | |
if err == False: | |
return Encrypted(''.join(enc), enckey) | |
def decrypt(encrypted): | |
""" | |
Decrypts a Encrypted object returned by encrypt() | |
""" | |
if not isinstance(encrypted, Encrypted): | |
raise TypeError("'encrypted' must be an Encrypted object") | |
enckey = encrypted.key | |
count = 0 | |
split = list(encrypted.message) | |
enckey2 = list(str(enckey)) | |
enc = [] | |
for i in split: | |
if i in excp: | |
enc.append(i) | |
else: | |
l = char.index(i) | |
q = l - int(enckey2[count]) | |
enc.append(char[q]) | |
count = count + 1 | |
return ''.join(enc) | |
def decryptwithkey(message, key): | |
""" | |
Decrypts with raw message and key. | |
""" | |
if len(str(key)) != len(message): | |
raise ValueError("Key is invalid. Key length should be equal to message length.") | |
return decrypt(Encrypted(message, key)) | |
def encryptastuple(message): | |
""" | |
Encrypts a message with a random key. Returns a namedtuple. | |
""" | |
tmp = encrypt(message) | |
return EncryptedNamedTuple(tmp.message, tmp.key) | |
def _pdigitrand(n): | |
range_start = 10**(n-1) | |
range_end = (10**n)-1 | |
return random.randint(range_start, range_end) | |
class Encrypted(object): | |
def __init__(self, encrypted, key): | |
self.message = encrypted | |
self.key = key | |
def __str__(self): | |
return self.message | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment