-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (69 loc) · 1.79 KB
/
Copy pathmain.py
File metadata and controls
87 lines (69 loc) · 1.79 KB
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# CIFRADO CÉSAR
import sys, time
# MÉTODO DE CIFRADO CÉSAR
def coded():
file = input("Nombre del archivo a cifrar => ")
try:
txt = open(file, "r")
text = txt.read()
except Exception as e:
print("Asegurese de que el archivo se encuentre en la misma carpeta :/")
time.sleep(5)
sys.exit()
key = input("Escriba la clave(8 chr min) => ")
ciphered_text = get_cipher(key, text)
txt.close()
f = open("cifrado.txt", "w")
try:
f.write(ciphered_text)
except UnicodeEncodeError as e:
print("Encontramos un carácter que no puede ser encriptado :/")
print("Carácteres no permitidos: ° - ´ - ¿ ")
f.close()
def get_cipher(key, plain_text):
cif = ""
for index, val in enumerate(plain_text):
c = ord(val) ^ ord(key[7])
cif += str(chr(c))
return cif
# MÉTODO DE DESIFRADO CÉSAR
def decoded():
try:
key = input("Escriba la clave => ")
f = open("cifrado.txt", "r")
ctext = f.read()
except FileNotFoundError as e:
print("Asegurese de escribir 'cifrado.txt'")
time.sleep(5)
sys.exit()
plain_text = ""
for index, val in enumerate(ctext):
c = ord(val) ^ ord(key[7])
plain_text += str(chr(c))
mensaje = open("mensaje_desifrado.txt", "w")
mensaje.write(plain_text)
mensaje.close()
if __name__ == "__main__":
print("")
print("************************************")
print("***** SISTEMA DE CIFRADO CÉSAR *****")
print("************************************")
print(" ~ SELECCIONE UNA OPCIÓN ~ ")
print("")
print("1. Cifrar")
print("2. Desifrar")
print("3. Salir")
print("")
option = input("=> ")
if option == str(1):
coded()
elif option == str(2):
decoded()
elif option == str(3):
sys.exit()
else:
print("La opción no se encuentra en el menú. SELECCIONE 1, 2 o 3")
time.sleep(5)
sys.exit()