-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #691 from AmitM0287/Main_AmitM_Feature_CustomEncry…
…ptionDecryption_21Jan CustomEncryptionDecryption Project
- Loading branch information
Showing
6 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SECRET_KEY = "https://github.com/AmitM0287" | ||
ENCRYPT_KEY_LEN = 7 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# CustomEncryptionDecryption | ||
# Overview | ||
CustomEncryptionDecryption is a simple Python program that allows users to encrypt and decrypt their data or text using a SECRET_KEY and CIPHER_TEXT. This program provides a basic and customizable encryption and decryption mechanism for securing sensitive information. | ||
|
||
# Prerequisites | ||
Before running the program, ensure you have Python installed on your system. Additionally, make sure to have the required dependencies by running: | ||
|
||
# bash | ||
# Copy code | ||
pip install -r requirements.txt | ||
|
||
# Getting Started | ||
Clone the Repository: | ||
|
||
# bash | ||
# Copy code | ||
git clone https://github.com/Mrinank-Bhowmick/python-beginner-projects.git | ||
cd projects/CustomEncryptionDecryption | ||
|
||
# Create Environment File: | ||
Make a copy of the .env.example file and rename it to .env. Edit the .env file to set your desired SECRET_KEY and CIPHER_TEXT values. | ||
|
||
# Install Dependencies: | ||
Install the required dependencies by running: | ||
|
||
# bash | ||
# Copy code | ||
pip install -r requirements.txt | ||
|
||
# Run the Program: | ||
Execute the following command to start the program: | ||
|
||
# bash | ||
# Copy code | ||
python script.py | ||
|
||
# Usage | ||
Upon running the program, you will be prompted to choose between encryption and decryption. Enter the necessary details, and the program will perform the requested operation using the provided SECRET_KEY and CIPHER_TEXT. | ||
|
||
# Contributing | ||
If you'd like to contribute to this project, please follow these steps: | ||
|
||
a. Fork the repository | ||
b. Create a new branch for your feature or bug fix | ||
c. Make your changes and commit them | ||
d. Push your changes to your fork | ||
e. Submit a pull request | ||
|
||
Thank you for contributing! | ||
|
||
# License | ||
This project is licensed under the MIT License. | ||
|
||
# Acknowledgements | ||
Special thanks to all contributors and users of CustomEncryptionDecryption! | ||
|
||
Thank you for using this program. If you have any issues or questions, feel free to open an issue or contact me. | ||
|
||
Happy encrypting and decrypting! 🚀 |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ZXlKaGJHY2lPaUpJVXpJMU5pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmhJam9pYWxaWVFqVXhWU0lzSW1JaU9pSXpNemQ1Y0VaRElpd2lZeUk2SW1aSFlVeEdjbTRpTENKa0lqb2liVE5FVG1SR2JDSXNJbVVpT2lJeFMybENTbkYzSWl3aVppSTZJakkzTldaS2JrSWlMQ0puSWpvaWFqSTJSVE5CTkNJc0ltZ2lPaUpwVEdFM1FqVlRJaXdpYVNJNklsaHVTelZwUm5JaUxDSnFJam9pTmtWbE1rYzFXaUlzSW1zaU9pSnFZVk5DUkhaMElpd2liQ0k2SWxFMk9WRkRRVFVpTENKdElqb2lZamQ1VFdFeVdTSXNJbTRpT2lJMU9URTJWV1V5SWl3aWJ5STZJbEJ4YVdabmR6SWlMQ0p3SWpvaU1HYzVhbkF5VmlJc0luRWlPaUp1ZG5OU1kzUXdJaXdpY2lJNkltSTFTek5rTTFjaUxDSnpJam9pZEVWSWMxaHNkaUlzSW5RaU9pSjNkMFpNU25ob0lpd2lkU0k2SW5wUU1ISnRXVzhpTENKMklqb2lNR3BHWTBjMk5DSXNJbmNpT2lKR05r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lKYU1WaGhhVEp5SWl3aUpTSTZJbFJoVHpaSVEyUWlMQ0ltSWpvaU1YaDRUMkpETUNJc0lpY2lPaUl3T0RNNVZGZzFJaXdpS0NJNklucDBVRGxCVERRaUxDSXBJam9pYVdNMU5qTlBlU0lzSWlvaU9pSjFWV1ExTnpGd0lpd2lLeUk2SW01UGRqTkxWbU1pTENJc0lqb2lUVXRFTUd4Qk9DSXNJaTBpT2lKMU16RmtUVE5FSWl3aUxpSTZJalZ5VlVJeGVuSWlMQ0l2SWpvaWJXdFNlRE4yY2lJc0lqb2lPaUkyWkVveFdYQTRJaXdpT3lJNklrZEtZVVV4U21JaUxDSThJam9pWmpObGFUUndkeUlzSWowaU9pSmFTMjlEVnpaVklpd2lQaUk2SW5OdVJuVldRVklpTENJX0lqb2lkR1l3TjNCaWN5SXNJa0FpT2lKNGRHMUZaRzVLSWl3aVd5STZJbUZVUm1Zd04yMGlMQ0pjWENJNklqUklTM2MwY0U4aUxDSmRJam9pZURBNFFrUXhaU0lzSWw0aU9pSklOSE5QTlUwMUlpd2lYeUk2SWpObFFUTm9OVEVpTENKZ0lqb2lPRFptZFVWME1pSXNJbnNpT2lJMWJUVXhRemhPSWl3aWZDSTZJaloxZFdoRmVFNGlMQ0o5SWpvaVZEaHNaVTh6T0NJc0luNGlPaUl3WmpWQ1l6STNJaXdpSUNJNklsSTJVMFUwWkdZaUxDSmNkQ0k2SWt3elRFazJlbW9pTENKY2JpSTZJbTA1UW1KbU1qTWlMQ0pjY2lJNkltNVVSREIzWlVRaUxDSmNkVEF3TUdJaU9pSllOVFpLUkZJeElpd2lYR1lpT2lKc1IzbHNiVFZFSW4wLkZZcTgyTUx3NXNqLTFqTXp6aXVKQUg3cnpWUTAyTDEzOHEtVnF1LTlyekE= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PyJWT==2.8.0 | ||
python-dotenv==1.0.0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import string | ||
import random | ||
import jwt | ||
import base64 | ||
import os | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
|
||
class CustomEncrDecr: | ||
def __init__(self) -> None: | ||
self.__SECRET_KEY = str(os.getenv('SECRET_KEY')) | ||
self.__ENDE_KEY_LEN = int(os.getenv('ENDE_KEY_LEN')) | ||
self.__keyStore = privateKeyStore(self.__ENDE_KEY_LEN) | ||
|
||
def _encryptText(self, text) -> str: | ||
encryptedText = '' | ||
for char in text: | ||
val = self.__keyStore._getCipherEStore().get(char) | ||
encryptedText += char if val is None else val | ||
encryptedText = base64.b64encode(encryptedText.encode('utf-8')).decode('utf-8') | ||
return encryptedText | ||
|
||
def _decryptText(self, encryptedText) -> str: | ||
decryptedText = '' | ||
encryptedText = base64.b64decode(encryptedText.encode('utf-8')).decode('utf-8') | ||
for idx in range(0, len(encryptedText), self.__ENDE_KEY_LEN): | ||
st = encryptedText[idx: idx + self.__ENDE_KEY_LEN] | ||
val = self.__keyStore._getCipherDStore().get(st) | ||
decryptedText += st if val is None else val | ||
return decryptedText | ||
|
||
def _generateCipherText(self) -> str: | ||
self.__keyStore._generateCipherEStore() | ||
self.__keyStore._generateCipherDStore() | ||
dataString = jwt.encode(self.__keyStore._getCipherEStore(), self.__SECRET_KEY, algorithm='HS256') | ||
cipherText = base64.b64encode(dataString.encode('utf-8')).decode('utf-8') | ||
with open('cipher_text.txt', 'w') as file: | ||
file.write(str(cipherText).strip()) | ||
|
||
def _verifyCipherText(self, cipherText='') -> None: | ||
dataString = base64.b64decode(cipherText.encode('utf-8')).decode('utf-8') | ||
cipherEstore = jwt.decode(dataString, self.__SECRET_KEY, algorithms=['HS256']) | ||
self.__keyStore._setCipherEStote(cipherEstore) | ||
self.__keyStore._generateCipherDStore() | ||
|
||
|
||
class privateKeyStore: | ||
def __init__(self, CIPHER_KEY) -> None: | ||
self.__CIPHER_ESTORE = {} | ||
self.__CIPHER_DSTORE = {} | ||
self.__ENDE_KEY_LEN = CIPHER_KEY | ||
|
||
def _getCipherEStore(self) -> dict: | ||
return self.__CIPHER_ESTORE | ||
|
||
def _getCipherDStore(self) -> dict: | ||
return self.__CIPHER_DSTORE | ||
|
||
def _setCipherEStote(self, cipherEstore) -> None: | ||
self.__CIPHER_ESTORE = cipherEstore | ||
|
||
def _generateCipherDStore(self) -> dict: | ||
for k, v in self.__CIPHER_ESTORE.items(): | ||
self.__CIPHER_DSTORE[v] = k | ||
|
||
def _generateCipherEStore(self) -> dict: | ||
for char in string.ascii_letters + string.digits + string.punctuation + string.whitespace: | ||
self.__CIPHER_ESTORE[char] = self.__generateSecureText() | ||
|
||
def __generateSecureText(self) -> str: | ||
return ''.join(random.choices(string.ascii_letters + string.digits + string.hexdigits + string.octdigits, k=self.__ENDE_KEY_LEN)) | ||
|
||
|
||
if __name__ == '__main__': | ||
print('\nWelcome to Custom Encryption & Decryption Program!') | ||
customEncrDecr = CustomEncrDecr() | ||
exitFlag = False | ||
while(not exitFlag): | ||
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: ') | ||
match userOption: | ||
case 'y': | ||
print('\nPlease add the CIPHER TEXT into the \'cipher_text.txt\' file! then start the program!\n') | ||
exit() | ||
case 'g': | ||
customEncrDecr._generateCipherText() | ||
print('\nYour CIPHER TEXT has been generated successfully! and added into the \'cipher_text.txt\' file!') | ||
exitFlag = True | ||
case 'c': | ||
try: | ||
with open('cipher_text.txt', 'r') as file: | ||
cipherText = str(file.read()).strip() | ||
customEncrDecr._verifyCipherText(cipherText) | ||
exitFlag = True | ||
except Exception as exc: | ||
print("\n\n", exc) | ||
exitFlag = False | ||
print('\nCIPHER TEXT is INVALID! Make sure you have added a valid CIPHER TEXT inside the \'cipher_text.txt\' file!') | ||
case 'r': | ||
print('\nPlease send me a detail email at \'[email protected]\'. 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') | ||
exit() | ||
case _: | ||
print('\nPlease choose a valid option next time!') | ||
exitFlag = False | ||
while(not exitFlag): | ||
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: ') | ||
match userOption: | ||
case 'e': | ||
text = input('\nPlease enter your text : ') | ||
print('\nYour ENCRYPTED TEXT text is: ', customEncrDecr._encryptText(text), '\n') | ||
case 'd': | ||
try: | ||
_encryptedText = input('\nPlease enter your ENCRYPTED TEXT : ') | ||
print('\nYour DECRYPTED TEXT is: ', customEncrDecr._decryptText(_encryptedText)) | ||
except Exception as exc: | ||
print('\nENCRYPTED TEXT is INVALID! Make sure you are using the same CIPHER TEXT which you used at the time of ENCRYPTION!') | ||
case 'q': | ||
print('\nThanks for using custom encryption & decryption program! Have great day :)\n') | ||
exit() | ||
case _: | ||
print('\nPlease choose a valid option next time!') | ||
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 |