-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcesar_sifra.py
104 lines (90 loc) · 3.53 KB
/
cesar_sifra.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
def caesar_cipher_encrypt(text, key):
if key < 1 or key > 25:
print("Neispravan unos! Ključ mora biti u rasponu od 1 do 25.")
return None
encrypted_text = ""
for char in text:
if char.isalpha():
shift = (ord(char.lower()) - ord('a') + key) % 26
encrypted_char = chr(ord('a') + shift)
if char.isupper():
encrypted_char = encrypted_char.upper()
encrypted_text += encrypted_char
elif char.isdigit():
encrypted_char = str((int(char) + key) % 10)
encrypted_text += encrypted_char
else:
shift = (ord(char) + key) % 128
encrypted_char = chr(shift)
encrypted_text += encrypted_char
return encrypted_text
def caesar_cipher_decrypt(encrypted_text, key):
decrypted_text = ""
for char in encrypted_text:
if char.isalpha():
shift = (ord(char.lower()) - ord('a') - key) % 26
decrypted_char = chr(ord('a') + shift)
if char.isupper():
decrypted_char = decrypted_char.upper()
decrypted_text += decrypted_char
elif char.isdigit():
decrypted_char = str((int(char) - key) % 10)
decrypted_text += decrypted_char
else:
shift = (ord(char) - key) % 128
decrypted_char = chr(shift)
decrypted_text += decrypted_char
return decrypted_text
def get_hidden_input(prompt):
import msvcrt
print(prompt, end='', flush=True)
password = ''
while True:
char = msvcrt.getch()
char = char.decode('utf-8')
if char == '\r':
print()
return password
elif char == '\b':
password = password[:-1]
print('\b \b', end='', flush=True)
else:
password += char
print('*', end='', flush=True)
def get_valid_key_input(prompt):
while True:
key = get_hidden_input(prompt)
if key.isdigit() and 1 <= int(key) <= 25:
return int(key)
else:
print("Neispravan unos! Ključ mora biti cijeli broj u rasponu od 1 do 25.")
message = get_hidden_input("Unesi poruku: ")
key = get_valid_key_input("Unesi ključ za enkripciju (1-25): ")
while key is None:
message = get_hidden_input("Unesi poruku: ")
key = get_valid_key_input("Unesi ključ za enkripciju (1-25): ")
encrypted_message = caesar_cipher_encrypt(message, key)
print("Enkriptovana poruka:", encrypted_message)
decrypt_key = get_hidden_input("Unesi ključ za dekripciju: ")
decrypted_message = caesar_cipher_decrypt(encrypted_message, int(decrypt_key))
print("Dekriptovana šifra:", decrypted_message)
def main():
while True:
print("1. Enkripcija")
print("2. Izlaz")
choice = input("Unesi izbor: ")
if choice == "1":
message = get_hidden_input("Unesi poruku: ")
key = get_valid_key_input("Unesi ključ za enkripciju (1-25): ")
while key is None:
message = get_hidden_input("Unesi poruku: ")
key = get_valid_key_input("Unesi ključ za enkripciju (1-25): ")
encrypted_message = caesar_cipher_encrypt(message, key)
print("Enkriptovana poruka:", encrypted_message)
elif choice == "2":
print("Izlaz.")
break
else:
print("Neispravan izbor! Pokušaj ponovo.")
if __name__ == "__main__":
main()