Skip to content

Commit f59af82

Browse files
committed
Add text encryption script
1 parent 877933c commit f59af82

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

text_encryption/encryption_method.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import string
2+
import sys
3+
4+
# Caesar cipher encryption
5+
def caesar_cipher_encrypt(text, shift):
6+
encrypted_text = ""
7+
for char in text:
8+
if char.isalpha():
9+
alphabet = string.ascii_uppercase if char.isupper() else string.ascii_lowercase
10+
encrypted_char = alphabet[(alphabet.index(char) + shift) % len(alphabet)]
11+
encrypted_text += encrypted_char
12+
else:
13+
encrypted_text += char
14+
return encrypted_text
15+
16+
# Affine cipher encryption
17+
def affine_cipher_encrypt(text, a, b):
18+
encrypted_text = ""
19+
for char in text:
20+
if char.isalpha():
21+
alphabet = string.ascii_uppercase if char.isupper() else string.ascii_lowercase
22+
encrypted_char = alphabet[(a * alphabet.index(char) + b) % len(alphabet)]
23+
encrypted_text += encrypted_char
24+
else:
25+
encrypted_text += char
26+
return encrypted_text
27+
28+
# Substitution cipher encryption
29+
def substitution_cipher_encrypt(text, key):
30+
encrypted_text = ""
31+
for char in text:
32+
if char.isalpha():
33+
alphabet = string.ascii_uppercase if char.isupper() else string.ascii_lowercase
34+
substitution_alphabet = str.maketrans(alphabet, key.upper()) if char.isupper() else str.maketrans(alphabet, key.lower())
35+
encrypted_char = char.translate(substitution_alphabet)
36+
encrypted_text += encrypted_char
37+
else:
38+
encrypted_text += char
39+
return encrypted_text
40+
41+
# Transposition cipher encryption
42+
def transposition_cipher_encrypt(text, key):
43+
ciphertext = [''] * int(key)
44+
for column in range(int(key)):
45+
currentIndex = column
46+
while currentIndex < len(text):
47+
ciphertext[column] += text[currentIndex]
48+
currentIndex += int(key)
49+
50+
return ''.join(ciphertext)
51+
52+
def main():
53+
text = input("Enter the text to encrypt: ")
54+
55+
# Ask user for the encryption method
56+
print("Choose an encryption method:")
57+
print("1. Caesar cipher")
58+
print("2. Affine cipher")
59+
print("3. Substitution cipher")
60+
print("4. Transposition cipher")
61+
62+
try:
63+
choice = int(input("Enter your choice (1-4): "))
64+
except ValueError: #to handle wrong values
65+
print("Invlaid selection")
66+
sys.exit(0)
67+
68+
if choice == 1:
69+
70+
try:
71+
shift = int(input("Enter the shift value for Caesar cipher: "))
72+
encrypted_text = caesar_cipher_encrypt(text, shift)
73+
print("Caesar cipher (encryption):", encrypted_text)
74+
except ValueError:
75+
print("Invalid Input")
76+
77+
elif choice == 2:
78+
79+
try:
80+
a = int(input("Enter the value for 'a' in Affine cipher: "))
81+
b = int(input("Enter the value for 'b' in Affine cipher: "))
82+
encrypted_text = affine_cipher_encrypt(text, a, b)
83+
print("Affine cipher (encryption):", encrypted_text)
84+
85+
except ValueError:
86+
print("Invalid Input")
87+
88+
elif choice == 3:
89+
90+
key = input("Enter the substitution key: ")
91+
if len(key)==26:
92+
encrypted_text = substitution_cipher_encrypt(text, key)
93+
print("Substitution cipher (encryption):", encrypted_text)
94+
else:
95+
print("Key must have the same length as the number of characters in the alphabet (26).")
96+
97+
elif choice == 4:
98+
99+
try:
100+
transpose_key = input("Enter the transposition key (make sure its less than length of stirng): ")
101+
if int(transpose_key)>len(text):
102+
print("Key must be less than length of string")
103+
else:
104+
encrypted_text = transposition_cipher_encrypt(text, transpose_key)
105+
print("Transposition cipher (encryption):", encrypted_text)
106+
107+
except ValueError:
108+
print("Invalid Input")
109+
110+
else:
111+
print("Invalid choice. Please choose a number between 1 and 4.")
112+
113+
if __name__ == "__main__":
114+
main()

text_encryption/readme.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Encryption Script
2+
3+
This script allows you to encrypt text using various encryption methods.
4+
5+
## Installation
6+
7+
1. Make sure you have Python installed on your system.
8+
2. Clone this repository or download the script file.
9+
10+
## Usage
11+
12+
1. Open a terminal or command prompt.
13+
2. Navigate to the directory where the script is located.
14+
3. Run the following command: python script.py
15+
16+
Follow the prompts to enter the text and choose an encryption method.
17+
Encryption Methods
18+
19+
Caesar Cipher
20+
The Caesar cipher is a substitution cipher where each letter in the plaintext is shifted a certain number of positions down the alphabet.
21+
To choose the Caesar cipher, enter 1 when prompted.
22+
23+
Affine Cipher
24+
The Affine cipher is a substitution cipher that combines the Caesar cipher with multiplication and addition.
25+
To choose the Affine cipher, enter 2 when prompted.
26+
27+
Substitution Cipher
28+
The Substitution cipher is a method of encryption where each letter in the plaintext is replaced by another letter according to a fixed key.
29+
To choose the Substitution cipher, enter 3 when prompted. Note that the key must have the same length as the number of characters in the alphabet (26).
30+
31+
Transposition Cipher
32+
The Transposition cipher is a method of encryption that rearranges the letters of the plaintext to form the ciphertext.
33+
To choose the Transposition cipher, enter 4 when prompted. You will also be asked to enter a transposition key, which should be less than the length of the text.
34+
Note: If you enter an invalid choice or provide incorrect input, appropriate error messages will be displayed.
35+
36+
Example
37+
Here is an example of running the script:
38+
Enter the text to encrypt: Hello, World!
39+
Choose an encryption method:
40+
1. Caesar cipher
41+
2. Affine cipher
42+
3. Substitution cipher
43+
4. Transposition cipher
44+
Enter your choice (1-4): 3
45+
Enter the substitution key: QWERTYUIOPASDFGHJKLZXCVBNM
46+
Substitution cipher (encryption): ITSSG, KTSSG!

0 commit comments

Comments
 (0)