Skip to content

Commit 63b122a

Browse files
committed
rebase: restructure the file
1 parent 09e1911 commit 63b122a

File tree

6 files changed

+117
-114
lines changed

6 files changed

+117
-114
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
venv/

README.md

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
1-
# Python Caesar Cipher
2-
3-
This repository contains a simple implementation of the Caesar Cipher algorithm in Python. The Caesar Cipher is a classic encryption technique where each letter in the plaintext is shifted by a fixed number of positions in the alphabet.
4-
5-
## Features
6-
7-
- Encrypt a plaintext message using a shift key.
8-
- Decrypt a ciphertext message using a shift key.
9-
- Handles both uppercase and lowercase letters.
10-
- Ignores non-alphabetic characters during encryption/decryption.
11-
12-
## How It Works
13-
14-
The Caesar Cipher shifts each letter in the message by a specified number of positions. For example, with a shift of `3`:
15-
- `A` becomes `D`
16-
- `B` becomes `E`
17-
- `Z` wraps around to `C`
18-
19-
## Usage
20-
21-
### Prerequisites
22-
23-
- Python 3.x installed on your system.
24-
25-
### Running the Script
26-
27-
1. Clone the repository:
28-
```bash
29-
git clone https://github.com/Darshan-KC/Python-Caesar-cipher.git
30-
cd Python-Caesar-cipher
31-
```
32-
33-
2. Run the script:
34-
```bash
35-
python caesar_cipher.py
36-
```
37-
38-
3. Follow the on-screen prompts to:
39-
- Choose between encryption and decryption.
40-
- Input the message and shift key.
41-
42-
### Example
43-
44-
**Input:**
45-
- Operation: Encrypt
46-
- Message: `Hello, World!`
47-
- Shift Key: `3`
48-
49-
**Output:**
50-
- Encrypted Message: `Khoor, Zruog!`
51-
52-
## File Structure
53-
54-
- `caesar_cipher.py`: The main script implementing the Caesar Cipher.
55-
56-
## Contributing
57-
58-
Contributions are welcome! If you find bugs or want to enhance the functionality, feel free to fork the repository and submit a pull request.
59-
60-
## License
61-
62-
This project is licensed under the [MIT License](LICENSE).
63-
64-
---
65-
66-
Happy coding! 😊
1+
# Python Caesar Cipher
2+
3+
This repository contains a simple implementation of the Caesar Cipher algorithm in Python. The Caesar Cipher is a classic encryption technique where each letter in the plaintext is shifted by a fixed number of positions in the alphabet.
4+
5+
## Features
6+
7+
- Encrypt a plaintext message using a shift key.
8+
- Decrypt a ciphertext message using a shift key.
9+
- Handles both uppercase and lowercase letters.
10+
- Ignores non-alphabetic characters during encryption/decryption.
11+
12+
## How It Works
13+
14+
The Caesar Cipher shifts each letter in the message by a specified number of positions. For example, with a shift of `3`:
15+
- `A` becomes `D`
16+
- `B` becomes `E`
17+
- `Z` wraps around to `C`
18+
19+
## Usage
20+
21+
### Prerequisites
22+
23+
- Python 3.x installed on your system.
24+
25+
### Running the Script
26+
27+
1. Clone the repository:
28+
```bash
29+
git clone https://github.com/Darshan-KC/Python-Caesar-cipher.git
30+
cd Python-Caesar-cipher
31+
```
32+
33+
2. Run the script:
34+
```bash
35+
python caesar_cipher.py
36+
```
37+
38+
3. Follow the on-screen prompts to:
39+
- Choose between encryption and decryption.
40+
- Input the message and shift key.
41+
42+
### Example
43+
44+
**Input:**
45+
- Operation: Encrypt
46+
- Message: `Hello, World!`
47+
- Shift Key: `3`
48+
49+
**Output:**
50+
- Encrypted Message: `Khoor, Zruog!`
51+
52+
## File Structure
53+
54+
- `caesar_cipher.py`: The main script implementing the Caesar Cipher.
55+
56+
## Contributing
57+
58+
Contributions are welcome! If you find bugs or want to enhance the functionality, feel free to fork the repository and submit a pull request.
59+
60+
## License
61+
62+
This project is licensed under the [MIT License](LICENSE).
63+
64+
---
65+
66+
Happy coding! 😊

caeser.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Caeser:
2+
def __init__(self, shift) -> None:
3+
self.shift = shift
4+
5+
def encrypt(self,message) ->str:
6+
shift = self.shift % 26
7+
result = ''
8+
for char in message:
9+
if 'A' <= char <= 'Z':
10+
result += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
11+
elif 'a' <= char <='z':
12+
result += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
13+
else:
14+
result += char
15+
return result
16+
17+
def decrypt(self,message) -> str:
18+
shift = self.shift % 26
19+
result = ''
20+
for char in message:
21+
if 'A' <= char <= 'Z':
22+
result += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
23+
elif 'a' <= char <= 'z':
24+
result += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))
25+
else:
26+
result += char
27+
return result

main.py

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,24 @@
1-
class Caeser:
2-
def __init__(self, shift) -> None:
3-
self.shift = shift
4-
5-
def encrypt(self,message) ->str:
6-
shift = self.shift % 26
7-
result = ''
8-
for char in message:
9-
if 'A' <= char <= 'Z':
10-
result += chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
11-
elif 'a' <= char <='z':
12-
result += chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
13-
else:
14-
result += char
15-
return result
16-
17-
def decrypt(self,message) -> str:
18-
shift = self.shift % 26
19-
result = ''
20-
for char in message:
21-
if 'A' <= char <= 'Z':
22-
result += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))
23-
elif 'a' <= char <= 'z':
24-
result += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))
25-
else:
26-
result += char
27-
return result
28-
29-
def main():
30-
# try:
31-
caeser = Caeser(9) # you can use shift value from user also
32-
choice = int(input("Choose mode: \n1. Encrypt \n2. Decrypt\n: "))
33-
if choice == 1:
34-
message = input("Enter the message to encrypt: ")
35-
result = caeser.encrypt(message)
36-
print(f"Encryption of '{message}' is '{result}'")
37-
38-
elif choice == 2:
39-
message = input("Enter the encypted message to decrypt: ")
40-
result = caeser.decrypt(message)
41-
print(f"Decryption of '{message}' is '{result}'")
42-
else:
43-
print("Invalid choice")
44-
45-
# except:
46-
# print("Exception occurs")
47-
48-
if __name__ == "__main__":
1+
# main.py
2+
from caeser import Caeser
3+
4+
def main():
5+
# try:
6+
caeser = Caeser(9) # you can use shift value from user also
7+
choice = int(input("Choose mode: \n1. Encrypt \n2. Decrypt\n: "))
8+
if choice == 1:
9+
message = input("Enter the message to encrypt: ")
10+
result = caeser.encrypt(message)
11+
print(f"Encryption of '{message}' is '{result}'")
12+
13+
elif choice == 2:
14+
message = input("Enter the encypted message to decrypt: ")
15+
result = caeser.decrypt(message)
16+
print(f"Decryption of '{message}' is '{result}'")
17+
else:
18+
print("Invalid choice")
19+
20+
# except:
21+
# print("Exception occurs")
22+
23+
if __name__ == "__main__":
4924
main()

test/__init__.py

Whitespace-only changes.

test/test_caeser.py

Whitespace-only changes.

0 commit comments

Comments
 (0)