Subido por alexander machado ambrocio

Encriptado Python

Anuncio
Encriptado Python
Machado Ambrocio Emmanuel Alexander
Funcion de obtener los datos en consola
def ObtenerDatos():
res = True
data = ['','','']
while res:
validacion = False
#aqui se hacen las validaciones de los inputs
if validacion != True:
print(Fore.BLUE + "¿QUE ACCION QUIERES REALIZAR?" + Style.RESET_ALL)
data[0] = input().lower()
if len(data[0]) == 0 :
print(Fore.RED + 'Ingresa encriptar o desencriptar' + Style.RESET_ALL)
data[0] = input().lower()
print(Fore.BLUE + "Ingresa tu mensaje a encriptar" + Style.RESET_ALL)
data[1] = input().lower()
if len(data[1] ) == 0:
print(Fore.RED + 'Por favor Ingresa un mensaje' + Style.RESET_ALL)
data[1] = input().lower()
print(Fore.BLUE + "Ingresa la llave de encriptado" + Style.RESET_ALL)
data[2] = input().lower()
if len(data[2] ) == 0:
print(Fore.RED + ' Por favor Ingresa una llave' + Style.RESET_ALL)
data[2] = input().lower()
validacion = True
return data
Clase Encripter
class Encripter:
def __init__(self,mensaje,llave ):
self.mensaje = mensaje
self.llave = llave
def codificar(mensaje, llave):
alfabeto = "abcdefghijklmnopqrstuvwxyz"
alfabeto_mayusculas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
longitud_alfabeto = len(alfabeto)
codificado = ""
for letra in mensaje:
if not letra.isalpha() or letra.lower() == 'ñ':
codificado += letra
continue
valor_letra = ord(letra)
# si ingreso minuscular comienza en 97(a) y se usa el alfabeto en minúsculas
alfabeto_a_usar = alfabeto
limite = 97 # Pero si es mayúscula, comienza en 65(A) y se usa en mayúsculas
if letra.isupper():
limite = 65
alfabeto_a_usar = alfabeto_mayusculas
Encriptado Python
1
# muevo la letra de posicion
posicion = (valor_letra - limite + llave) % longitud_alfabeto
# Convierto el entero resultante a letra y lo concateno
codificado += alfabeto_a_usar[posicion]
return codificado
def decodificar(mensaje, llave):
alfabeto = "abcdefghijklmnopqrstuvwxyz"
alfabeto_mayusculas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
longitud_alfabeto = len(alfabeto)
decodificado = ""
for letra in mensaje:
if not letra.isalpha() or letra.lower() == 'ñ':
decodificado += letra
continue
valor_letra = ord(letra)
# Suponemos que es minúscula, así que esto comienza en 97(a) y se usará el alfabeto en minúsculas
alfabeto_a_usar = alfabeto
limite = 97 # Pero si es mayúscula, comienza en 65(A) y se usa en mayúsculas
if letra.isupper():
limite = 65
alfabeto_a_usar = alfabeto_mayusculas
# Rotamos la letra, ahora hacia la izquierda
posicion = (valor_letra - limite - llave) % longitud_alfabeto
# Convierto el entero resultante a letra y lo concateno
decodificado += alfabeto_a_usar[posicion]
return decodificado
Funcion principal para la aplicacion
print(Fore.GREEN + '______ _
_
_____ _____ __
__ _____
_______ _______ ______'+ Style.RESET_ALL)
print(Fore.GREEN + '| ____|| \ | | / ____|| __ \ \ \
/ /| __ \ |__
__|| ____|| __ "\"'+ Style.RESET_ALL)
print(Fore.GREEN + '| |__
| \| || |
| |__) | \ \_/ / | |__) |
| |
| |__
| |__) |'+ Style.RESET_ALL)
print(Fore.GREEN + '| __| | . ` || |
| _ /
\
/ | ___/
| |
| __| | _ /'+ Style.RESET_ALL)
print(Fore.GREEN + '| |____ | |\ || |____ | | \ \
| |
| |
| |
| |____ | | \ "\"'+ Style.RESET_ALL)
print(Fore.GREEN + '|______||_| \_| \_____||_| \_\
|_|
|_|
|_|
|______||_| \_"\"'+ Style.RESET_ALL)
print()
#aqui ejecuto el programa
while True:
datos = ObtenerDatos()
if datos[0] == "encriptar":
en1 = Encripter(datos[1],int(datos[2]))
res = Encripter.codificar(en1.mensaje,en1.llave)
print(res)
if datos[0] == 'desencriptar':
des = Encripter(datos[1],int(datos[2]))
res = Encripter.decodificar(des.mensaje,des.llave)
print(res)
print(Fore.WHITE + '¿Quieres cerrar el programa?' + Style.RESET_ALL)
salir = input().lower()
if salir.upper() == 'SI':
break
else:
print('')
Encriptado Python
2
Descargar