|
| 1 | +import string |
| 2 | +import random |
| 3 | +import jwt |
| 4 | +import base64 |
| 5 | +import os |
| 6 | +from dotenv import load_dotenv |
| 7 | +load_dotenv() |
| 8 | + |
| 9 | + |
| 10 | +class CustomEncrDecr: |
| 11 | + def __init__(self) -> None: |
| 12 | + self.__SECRET_KEY = str(os.getenv('SECRET_KEY')) |
| 13 | + self.__ENDE_KEY_LEN = int(os.getenv('ENDE_KEY_LEN')) |
| 14 | + self.__keyStore = privateKeyStore(self.__ENDE_KEY_LEN) |
| 15 | + |
| 16 | + def _encryptText(self, text) -> str: |
| 17 | + encryptedText = '' |
| 18 | + for char in text: |
| 19 | + val = self.__keyStore._getCipherEStore().get(char) |
| 20 | + encryptedText += char if val is None else val |
| 21 | + encryptedText = base64.b64encode(encryptedText.encode('utf-8')).decode('utf-8') |
| 22 | + return encryptedText |
| 23 | + |
| 24 | + def _decryptText(self, encryptedText) -> str: |
| 25 | + decryptedText = '' |
| 26 | + encryptedText = base64.b64decode(encryptedText.encode('utf-8')).decode('utf-8') |
| 27 | + for idx in range(0, len(encryptedText), self.__ENDE_KEY_LEN): |
| 28 | + st = encryptedText[idx: idx + self.__ENDE_KEY_LEN] |
| 29 | + val = self.__keyStore._getCipherDStore().get(st) |
| 30 | + decryptedText += st if val is None else val |
| 31 | + return decryptedText |
| 32 | + |
| 33 | + def _generateCipherText(self) -> str: |
| 34 | + self.__keyStore._generateCipherEStore() |
| 35 | + self.__keyStore._generateCipherDStore() |
| 36 | + dataString = jwt.encode(self.__keyStore._getCipherEStore(), self.__SECRET_KEY, algorithm='HS256') |
| 37 | + cipherText = base64.b64encode(dataString.encode('utf-8')).decode('utf-8') |
| 38 | + with open('cipher_text.txt', 'w') as file: |
| 39 | + file.write(str(cipherText).strip()) |
| 40 | + |
| 41 | + def _verifyCipherText(self, cipherText='') -> None: |
| 42 | + dataString = base64.b64decode(cipherText.encode('utf-8')).decode('utf-8') |
| 43 | + cipherEstore = jwt.decode(dataString, self.__SECRET_KEY, algorithms=['HS256']) |
| 44 | + self.__keyStore._setCipherEStote(cipherEstore) |
| 45 | + self.__keyStore._generateCipherDStore() |
| 46 | + |
| 47 | + |
| 48 | +class privateKeyStore: |
| 49 | + def __init__(self, CIPHER_KEY) -> None: |
| 50 | + self.__CIPHER_ESTORE = {} |
| 51 | + self.__CIPHER_DSTORE = {} |
| 52 | + self.__ENDE_KEY_LEN = CIPHER_KEY |
| 53 | + |
| 54 | + def _getCipherEStore(self) -> dict: |
| 55 | + return self.__CIPHER_ESTORE |
| 56 | + |
| 57 | + def _getCipherDStore(self) -> dict: |
| 58 | + return self.__CIPHER_DSTORE |
| 59 | + |
| 60 | + def _setCipherEStote(self, cipherEstore) -> None: |
| 61 | + self.__CIPHER_ESTORE = cipherEstore |
| 62 | + |
| 63 | + def _generateCipherDStore(self) -> dict: |
| 64 | + for k, v in self.__CIPHER_ESTORE.items(): |
| 65 | + self.__CIPHER_DSTORE[v] = k |
| 66 | + |
| 67 | + def _generateCipherEStore(self) -> dict: |
| 68 | + for char in string.ascii_letters + string.digits + string.punctuation + string.whitespace: |
| 69 | + self.__CIPHER_ESTORE[char] = self.__generateSecureText() |
| 70 | + |
| 71 | + def __generateSecureText(self) -> str: |
| 72 | + return ''.join(random.choices(string.ascii_letters + string.digits + string.hexdigits + string.octdigits, k=self.__ENDE_KEY_LEN)) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + print('\nWelcome to Custom Encryption & Decryption Program!') |
| 77 | + customEncrDecr = CustomEncrDecr() |
| 78 | + exitFlag = False |
| 79 | + while(not exitFlag): |
| 80 | + userOption = input('\n\nDo you already have a CIPHER TEXT ? \n\ta. Press \'c\' to continue with the default CIPHER TEXT \n\tb. Press \'y\' if you already have a CIPHER TEXT \n\tc. Press \'g\' to generate a CIPHER TEXT \n\td. Press \'r\' to report a BUG \n\nYou have chosen: ') |
| 81 | + match userOption: |
| 82 | + case 'y': |
| 83 | + print('\nPlease add the CIPHER TEXT into the \'cipher_text.txt\' file! then start the program!\n') |
| 84 | + exit() |
| 85 | + case 'g': |
| 86 | + customEncrDecr._generateCipherText() |
| 87 | + print('\nYour CIPHER TEXT has been generated successfully! and added into the \'cipher_text.txt\' file!') |
| 88 | + exitFlag = True |
| 89 | + case 'c': |
| 90 | + try: |
| 91 | + with open('cipher_text.txt', 'r') as file: |
| 92 | + cipherText = str(file.read()).strip() |
| 93 | + customEncrDecr._verifyCipherText(cipherText) |
| 94 | + exitFlag = True |
| 95 | + except Exception as exc: |
| 96 | + print("\n\n", exc) |
| 97 | + exitFlag = False |
| 98 | + print('\nCIPHER TEXT is INVALID! Make sure you have added a valid CIPHER TEXT inside the \'cipher_text.txt\' file!') |
| 99 | + case 'r': |
| 100 | + print('\nPlease send me a detail email at \'amitmanna0287@gmail.com\'. Keep the subject as \'BUG: Custom Encryption & Decryption Program\'. \n\nTo follow me use below social accounts: \n\ta. LinkedIn: https://www.linkedin.com/in/amitm0287/ \n\tb. GitHub: https://github.com/AmitM0287 \n\nThank you for time! Have a good day :)\n') |
| 101 | + exit() |
| 102 | + case _: |
| 103 | + print('\nPlease choose a valid option next time!') |
| 104 | + exitFlag = False |
| 105 | + while(not exitFlag): |
| 106 | + userOption = input('\n\nPlease choose one option from below: \n\ta. Press \'e\' to ENCRYPT a TEXT \n\tb. Press \'d\' to DECRYPT a ENCRYPTED TEXT \n\tc. Press \'q\' to QUIT the program ? \n\nYou have chosen: ') |
| 107 | + match userOption: |
| 108 | + case 'e': |
| 109 | + text = input('\nPlease enter your text : ') |
| 110 | + print('\nYour ENCRYPTED TEXT text is: ', customEncrDecr._encryptText(text), '\n') |
| 111 | + case 'd': |
| 112 | + try: |
| 113 | + _encryptedText = input('\nPlease enter your ENCRYPTED TEXT : ') |
| 114 | + print('\nYour DECRYPTED TEXT is: ', customEncrDecr._decryptText(_encryptedText)) |
| 115 | + except Exception as exc: |
| 116 | + print('\nENCRYPTED TEXT is INVALID! Make sure you are using the same CIPHER TEXT which you used at the time of ENCRYPTION!') |
| 117 | + case 'q': |
| 118 | + print('\nThanks for using custom encryption & decryption program! Have great day :)\n') |
| 119 | + exit() |
| 120 | + case _: |
| 121 | + print('\nPlease choose a valid option next time!') |
| 122 | + exitFlag = False if input('\nDo you want to continue to the program ? \n\ta. Press \'c\' to continue! \n\tb. Press \'any other key\' to quit the program! \n\nYou have chosen: ') == 'c' else True |
0 commit comments