Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Cesar encriptado
- ###########################################################
- plaintext = 'como pez en el agua' #encrypted message
- shift = 3
- alphabet = "abcdefghijklmnopqrstuvwxyz"
- ciphertext = ""
- new_ind = 0 # this value will be changed later
- for i in plaintext:
- if i in alphabet:
- new_ind = alphabet.index(i) + shift
- ciphertext += alphabet[new_ind % (26)]
- else:
- ciphertext += i
- print("The ciphertext is: " + ciphertext)
- ###############################################################
- #Cesar desencriptacion
- import string
- from time import sleep
- alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"
- def decrypt():
- print("Welcome to Caesar Cipher Decryption.\n")
- encrypted_message = input("Enter the message you would like to decrypt: ").strip()
- print()
- key = int(input("Enter key to decrypt: "))
- decrypted_message = ""
- for c in encrypted_message:
- if c in alphabet:
- position = alphabet.find(c)
- new_position = (position - key) % 26
- new_character = alphabet[new_position]
- decrypted_message += new_character
- else:
- decrypted_message += c
- print("\nDecrypting your message...\n")
- sleep(2) # give an appearance of doing something complicated
- print("Stand by, almost finished...\n")
- sleep(2) # more of the same
- print("Your decrypted message is:\n")
- print(decrypted_message)
- decrypt()
- #########################################################################
- #Vigenere (ambos)
- LETRAS = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
- def main():
- mensaje = "como pez en el agua"
- myKey = "ventilado"
- accion = input("Mode: ")
- if accion == 'encriptar':
- traducido = cifrar_mensaje(myKey, mensaje)
- elif accion == 'descifrar':
- traducido = descifrar_mensaje(myKey, mensaje)
- print(traducido)
- def cifrar_mensaje(clave, mensa):
- return traductor_mensaje(clave, mensa, 'encriptar')
- def descifrar_mensaje(clave, mensa):
- return traductor_mensaje(clave, mensa, 'descifrar')
- def traductor_mensaje(clave, mensa, accion):
- traducido = []
- indice_clave = 0
- clave = clave.upper()
- for symbol in mensa:
- num = LETRAS.find(symbol.upper())
- if num != -1:
- if accion == 'encriptar':
- num += LETRAS.find(clave[indice_clave])
- elif accion == 'descifrar':
- num -= LETRAS.find(clave[indice_clave])
- num %= len(LETRAS)
- if symbol.isupper():
- traducido.append(LETRAS[num])
- elif symbol.islower():
- traducido.append(LETRAS[num].lower())
- indice_clave += 1
- if indice_clave == len(clave):
- indice_clave = 0
- else:
- traducido.append(symbol)
- return ('').join(traducido)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment