diff --git a/README.md b/README.md index c867d20..742b548 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,46 @@ In this repository I have implemented two simple function just do that, which ta Samewise to decryption, you are going to specify the secret number and then it will recieve your encrypted text input and then render to you decrypted text output +# ASCII-based Encryption/Decryption + +This Python script provides simple ASCII-based encryption and decryption functionalities. The code has been improved to enhance key handling, error checking, and overall robustness. + +## Improvements + +### Key Handling +- The `key` parameter now accepts both strings and bytes, providing more flexibility for different types of keys. +- The key is automatically encoded if it is provided as a string. + +### Non-ASCII Characters +- The script now handles non-ASCII characters more gracefully. + +### Error Handling +- Error checks have been added to ensure that both the text and the key are not empty. + +### Modulo Operation +- A modulo operation has been introduced to prevent overflow issues during character shifting. This ensures that the character values stay within the valid ASCII range. + +## Usage + +```python +from itertools import cycle + +def encrypt(text, key='0'): + # ... (implementation details) + +def decrypt(text, key='0'): + # ... (implementation details) + +# Example Usage: +original_message = "Hello, World!" +encryption_key = "secretkey" + +encrypted_message = encrypt(original_message, encryption_key) +print(f"Encrypted: {encrypted_message}") + +decrypted_message = decrypt(encrypted_message, encryption_key) +print(f"Decrypted: {decrypted_message}") + Demo ------------ @@ -69,4 +109,4 @@ Did you find this information useful, then give it a star Credits ----------- -All the credits to [kalebu](github.com/kalebu) \ No newline at end of file +All the credits to [kalebu](github.com/kalebu) diff --git a/algorithms.py b/algorithms.py index ae913df..288bc5c 100644 --- a/algorithms.py +++ b/algorithms.py @@ -1,26 +1,39 @@ -# =============================================================== -# ============ FUNCTION TO DO ASCII BASED ENCRYPTION =========== -# ============ BASED ON A CERTAIN KEY =========== -# ================================================================ +from itertools import cycle +def encrypt(text, key='0'): + if not text: + raise ValueError("Text should not be empty") + if not key: + raise ValueError("Key should not be empty") + + key = key.encode() if isinstance(key, str) else key + encrypted_text = "" + + for char, key_char in zip(text, cycle(key)): + encrypted_text += chr((ord(char) + ord(key_char)) % 128) + + return encrypted_text -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]) +def decrypt(text, key='0'): + if not text: + raise ValueError("Text should not be empty") + if not key: + raise ValueError("Key should not be empty") + + key = key.encode() if isinstance(key, str) else key + decrypted_text = "" + + for char, key_char in zip(text, cycle(key)): + decrypted_text += chr((ord(char) - ord(key_char)) % 128) + + return decrypted_text +# Example Usage: +original_message = "Hello, World!" +encryption_key = "secretkey" -# =================================================================== -# ============= FUNCTION TO DO ASCII BASED DECRYPTION =============== -# ============= BASED ON A CERTAIN KEY =============== -# =================================================================== +encrypted_message = encrypt(original_message, encryption_key) +print(f"Encrypted: {encrypted_message}") - -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]) +decrypted_message = decrypt(encrypted_message, encryption_key) +print(f"Decrypted: {decrypted_message}")