Skip to content

Commit 77951b8

Browse files
Added caesar hacker
1 parent 0e780de commit 77951b8

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Caesar Cipher Hacker
2+
3+
This Python script is designed to decode messages encrypted with the Caesar cipher by analyzing the text and determining the best shift value for decryption. It utilizes the Natural Language Toolkit (nltk) to evaluate the decoded messages based on recognized English words.
4+
5+
## Features
6+
7+
- Decodes Caesar cipher messages with varying shift values.
8+
- Automatically identifies the best shift for readability, thus cracking the shift value
9+
- Can work with text input or read from a specified file.
10+
11+
## Requirements
12+
13+
Before running the script, you need to set up the environment:
14+
15+
1. **Install Dependencies**: Ensure you have the required Python packages by running:
16+
```bash
17+
pip install nltk
18+
```
19+
2. **Download NLTK Resources**: Run `download_nltk_resources.py` to download necessary NLTK data files.
20+
21+
## Usage
22+
23+
1. Ensure you've completed the setup steps above.
24+
2. You can either:
25+
- Edit the `encoded_file.txt` to include your encoded message, or specify the file to be used (it can be a large text file).
26+
- Or, input the encoded text directly when prompted.
27+
28+
3. Run the script with:
29+
```bash
30+
python cipherhacker.py
31+
```
32+
33+
4. Follow the prompts:
34+
- Enter `file` to read from `encoded_file.txt`(default) or any other file.
35+
- Enter `text` to input the encoded message directly.
36+
37+
## Output
38+
39+
The script will display:
40+
- The shift value used for encoding.
41+
- The decoded message for the provided ciphertext.
42+
43+
## Example
44+
45+
To decode a message:
46+
1. Place your encoded text in `encoded_file.txt` or input any other txt file or have a sample ready to input.
47+
2. Run the script and follow the prompts.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import nltk
2+
from nltk.corpus import words
3+
from nltk import word_tokenize
4+
from nltk import pos_tag
5+
6+
# Load the set of English words
7+
english_words = set(words.words())
8+
9+
def decode_caesar(ciphertext, shift):
10+
decoded = []
11+
for char in ciphertext:
12+
if char.isalpha():
13+
shift_base = ord('a') if char.islower() else ord('A')
14+
decoded_char = chr((ord(char) - shift_base - shift) % 26 + shift_base)
15+
decoded.append(decoded_char)
16+
else:
17+
decoded.append(char) # Non-alpha characters remain unchanged
18+
return ''.join(decoded)
19+
20+
def evaluate_shift(decoded_message):
21+
tokens = word_tokenize(decoded_message)
22+
# Count recognized English words
23+
score = sum(1 for word in tokens if word.lower() in english_words)
24+
return score
25+
26+
def find_best_shift(ciphertext):
27+
best_shift = 0
28+
highest_score = 0
29+
30+
for shift in range(1, 26):
31+
decoded_message = decode_caesar(ciphertext, shift)
32+
score = evaluate_shift(decoded_message)
33+
34+
if score > highest_score:
35+
highest_score = score
36+
best_shift = shift
37+
38+
return best_shift
39+
40+
if __name__ == "__main__":
41+
while True:
42+
mode = input("Enter the type of input (file/text): ").strip().lower()
43+
44+
if mode == "file":
45+
filename = input("Enter the name of the file (or press Enter to use 'encoded_file.txt'): ")
46+
if not filename:
47+
filename = 'encoded_file.txt'
48+
49+
try:
50+
with open(filename, 'r') as file:
51+
encoded_text = file.read()
52+
break
53+
except FileNotFoundError:
54+
print(f"Error: The file '{filename}' was not found. Please try again.")
55+
56+
elif mode == "text":
57+
encoded_text = input("Enter the text: ")
58+
break
59+
60+
else:
61+
print("Invalid mode selected. Please enter 'file' or 'text'.")
62+
63+
64+
65+
66+
best_shift = find_best_shift(encoded_text)
67+
print(f'The shift used was: {best_shift}')
68+
print(f'Decoded message: {decode_caesar(encoded_text,best_shift)}')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import nltk
2+
3+
# Download the necessary NLTK resources
4+
nltk.download('punkt')
5+
nltk.download('averaged_perceptron_tagger')
6+
nltk.download('words')
7+
nltk.download('punkt_tab')
8+
9+
print("NLTK resources downloaded successfully.")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Wh ghm ptbm mh lmkbdx mbee max bkhg bl ahm, unm ftdx bm ahm ur lmkbdbgz.

0 commit comments

Comments
 (0)