Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip optimization #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand All @@ -69,4 +109,4 @@ Did you find this information useful, then give it a star

Credits
-----------
All the credits to [kalebu](github.com/kalebu)
All the credits to [kalebu](github.com/kalebu)
55 changes: 34 additions & 21 deletions algorithms.py
Original file line number Diff line number Diff line change
@@ -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}")