diff --git a/algorithms.py b/algorithms.py index ae913df..544d003 100644 --- a/algorithms.py +++ b/algorithms.py @@ -1,26 +1,23 @@ -# =============================================================== -# ============ FUNCTION TO DO ASCII BASED ENCRYPTION =========== -# ============ BASED ON A CERTAIN KEY =========== -# ================================================================ +from random import seed, randint - -def encrypt(text, key=0): - if not isinstance(text, str): - raise TypeError("{} should be a type string".format(text)) - if not isinstance(key, int): - raise TypeError("{} should be of type int".format(key)) - return "".join([chr(ord(something) + key) for something in text]) - - -# =================================================================== -# ============= FUNCTION TO DO ASCII BASED DECRYPTION =============== -# ============= BASED ON A CERTAIN KEY =============== -# =================================================================== - - -def decrypt(text, key=0): - if not isinstance(text, str): - raise TypeError("{} should be a type string".format(text)) - if not isinstance(key, int): - raise TypeError("{} should be of type int".format(key)) - return "".join([chr(ord(something) - key) for something in text]) +class __crypt: + """A way of encrypting and decrypting text in an ASCII way""" + def encrypt(text, key): + """Encrypts the text, key is used as seed and must be given""" + seed(key) + if not isinstance(text, str): + raise TypeError("{} should be a type string".format(text)) + return "".join([chr(ord(something) + randint(1,randint(2,10))) for something in text]) + + def decrypt(text, key): + """Decrypts the text, key is used as seed and must be given and must be the same as the encryption.""" + seed(key) + if not isinstance(text, str): + raise TypeError("{} should be a type string".format(text)) + return "".join([chr(ord(something) - randint(1,randint(2,10))) for something in text]) + +if __name__ == '__main__': + a = __crypt.encrypt("Hello world", 5) + print(a) + b = __crypt.decrypt(a, 5) + print(b)