-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.py
143 lines (120 loc) · 4.97 KB
/
caesar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import click
import codecs
import os
# Define Caesar cipher encryption function
def caesar_cipher(text, shift, option):
if option == 'decrypt':
shift = -shift
encrypted_text = ""
for char in text:
if char.isalpha():
is_upper = char.isupper()
char = char.lower()
char_code = ord(char)
char_code = (char_code - ord('a') + shift) % 26 + ord('a')
if is_upper:
char_code = chr(char_code).upper()
else:
char_code = chr(char_code)
encrypted_text += char_code
else:
encrypted_text += char
return encrypted_text
# Define ROT13 encryption function using codecs
def rot13_cipher(text):
return codecs.encode(text, 'rot_13')
# Define Atbash cipher encryption function
def atbash_cipher(text):
encrypted_text = ""
for char in text:
if char.isalpha():
is_upper = char.isupper()
char = char.lower()
char_code = ord(char)
char_code = ord('z') - (char_code - ord('a'))
if is_upper:
char_code = chr(char_code).upper()
else:
char_code = chr(char_code)
encrypted_text += char_code
else:
encrypted_text += char
return encrypted_text
def crot13(text, shift, option):
if option == 'encrypt':
res = caesar_cipher(text, shift, option)
return rot13_cipher(res)
elif option == 'decrypt':
res = caesar_cipher(text, shift, option) # Decrypt using Caesar cipher with a negative shift
return rot13_cipher(res)
VERSION = '1.0'
encryption_algorithms = {
'caesar': caesar_cipher,
'rot13': rot13_cipher,
'atbash': atbash_cipher,
'crot13': crot13
}
@click.command()
@click.option('--file', type=click.File('r'), help='Input file to read text from.')
@click.option('--text', help='Text to encrypt or decrypt.')
@click.option('--shift', type=int, help='Shift value for Caesar cipher (default: 3).', default=3)
@click.option('--decrypt', is_flag=True, help='Decrypt the text (default is to encrypt).')
@click.option('--output', type=click.File('w'), help='Output file to write the result to.')
@click.option('--algorithm', type=click.Choice(['caesar', 'rot13', 'atbash', 'crot13'], case_sensitive=False), default='caesar', help='Encryption algorithm to use (default: Caesar cipher).')
@click.version_option(version=VERSION)
@click.option('--uninstall', is_flag=True, help='Uninstall the program.')
def main(file, text, shift, decrypt, output, algorithm, uninstall):
"""
Command-line tool for text encryption and decryption using various algorithms.
This tool supports multiple encryption algorithms, including Caesar cipher,
ROT13, Atbash cipher, and a custom crot13 algorithm.
Usage examples:
- Encrypt text using Caesar cipher:
$ caesar.py --text "Hello, World!" --shift 3
- Decrypt text using ROT13:
$ caesar.py --text "Uryyb, Jbeyq!" --algorithm rot13 --decrypt
- Encrypt text using the custom crot13 algorithm:
$ caesar.py --text "Hello, World!" --algorithm crot13 --shift 3
- Decrypt text using the custom crot13 algorithm:
$ caesar.py --text "Uryyb, Jbeyq!" --algorithm crot13 --decrypt --shift 3
- Encrypt text from a file and save the result to another file:
$ caesar.py --file input.txt --output encrypted.txt --shift 5
"""
if uninstall:
response = click.prompt('Are you sure you want to uninstall the program? (y/n)', type=str, default='n')
if response.lower() == 'y':
try:
os.remove('/usr/local/bin/caesar')
click.echo('Uninstalled successfully.')
except OSError:
click.echo('Only System Administrators can uninstall the program. Run the command with sudo.')
else:
click.echo('Uninstallation aborted.')
return
if file and text:
click.echo("Error: Please provide either '--file' or '--text', not both.")
return
if not file and not text:
text = click.prompt('Enter the text to encrypt/decrypt', hide_input=decrypt)
input_text = text # Initialize input_text with the value of text
try:
if file:
input_text = file.read()
if algorithm == 'caesar':
result = caesar_cipher(input_text, shift, 'encrypt' if not decrypt else 'decrypt')
elif algorithm == 'rot13':
result = rot13_cipher(input_text)
elif algorithm == 'atbash':
result = atbash_cipher(input_text)
elif algorithm == 'crot13':
result = crot13(input_text, shift, 'encrypt' if not decrypt else 'decrypt')
if output:
output.write(result)
output.close()
click.echo(f'Result has been written to the file: {output.name}')
else:
click.echo(f'Result: {result}')
except Exception as e:
click.echo(f"Error: {e}")
if __name__ == '__main__':
main()