|
1 |
| -# =============================================================== |
2 |
| -# ============ FUNCTION TO DO ASCII BASED ENCRYPTION =========== |
3 |
| -# ============ BASED ON A CERTAIN KEY =========== |
4 |
| -# ================================================================ |
| 1 | +from random import seed, randint |
5 | 2 |
|
| 3 | +class __crypt: |
| 4 | + """A way of encrypting and decrypting text in an ASCII way""" |
| 5 | + |
| 6 | + def encrypt(text, key): |
| 7 | + """Encrypts the text, key is used as seed and must be given""" |
| 8 | + seed(key) |
| 9 | + if not isinstance(text, str): |
| 10 | + raise TypeError("{} should be a type string".format(text)) |
| 11 | + return "".join([chr(ord(something) + randint(1,randint(2,10))) for something in text]) |
| 12 | + |
| 13 | + def decrypt(text, key): |
| 14 | + """Decrypts the text, key is used as seed and must be given and must be the same as the encryption.""" |
| 15 | + seed(key) |
| 16 | + if not isinstance(text, str): |
| 17 | + raise TypeError("{} should be a type string".format(text)) |
| 18 | + return "".join([chr(ord(something) - randint(1,randint(2,10))) for something in text]) |
| 19 | + |
| 20 | +if __name__ == '__main__': |
| 21 | + text = input("Put in some text to encrypt: ") |
| 22 | + key = input("Put in a key: ") |
6 | 23 |
|
7 |
| -def encrypt(text, key=0): |
8 |
| - if not isinstance(text, str): |
9 |
| - raise TypeError("{} should be a type string".format(text)) |
10 |
| - if not isinstance(key, int): |
11 |
| - raise TypeError("{} should be of type int".format(key)) |
12 |
| - return "".join([chr(ord(something) + key) for something in text]) |
13 |
| - |
14 |
| - |
15 |
| -# =================================================================== |
16 |
| -# ============= FUNCTION TO DO ASCII BASED DECRYPTION =============== |
17 |
| -# ============= BASED ON A CERTAIN KEY =============== |
18 |
| -# =================================================================== |
19 |
| - |
20 |
| - |
21 |
| -def decrypt(text, key=0): |
22 |
| - if not isinstance(text, str): |
23 |
| - raise TypeError("{} should be a type string".format(text)) |
24 |
| - if not isinstance(key, int): |
25 |
| - raise TypeError("{} should be of type int".format(key)) |
26 |
| - return "".join([chr(ord(something) - key) for something in text]) |
| 24 | + print(f"Encrypted: {__crypt.encrypt(text, key)}") |
0 commit comments