JoaquinFioriti

Algoritmos encriptado/ desencpriptado

Nov 30th, 2021 (edited)
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. #Cesar encriptado
  2. ###########################################################
  3. plaintext = 'como pez en el agua' #encrypted message
  4. shift = 3
  5. alphabet = "abcdefghijklmnopqrstuvwxyz"
  6. ciphertext = ""
  7.  
  8.  
  9. new_ind = 0  # this value will be changed later
  10. for i in plaintext:
  11.     if i in alphabet:
  12.         new_ind = alphabet.index(i) + shift
  13.         ciphertext += alphabet[new_ind % (26)]
  14.     else:
  15.         ciphertext += i
  16. print("The ciphertext is: " + ciphertext)
  17.  
  18.  
  19. ###############################################################
  20. #Cesar desencriptacion
  21.  
  22. import string
  23. from time import sleep
  24.  
  25. alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"
  26.  
  27. def decrypt():
  28.    
  29.     print("Welcome to Caesar Cipher Decryption.\n")
  30.     encrypted_message = input("Enter the message you would like to decrypt: ").strip()
  31.     print()
  32.     key = int(input("Enter key to decrypt: "))
  33.    
  34.     decrypted_message = ""
  35.  
  36.     for c in encrypted_message:
  37.  
  38.         if c in alphabet:
  39.             position = alphabet.find(c)
  40.             new_position = (position - key) % 26
  41.             new_character = alphabet[new_position]
  42.             decrypted_message += new_character
  43.         else:
  44.             decrypted_message += c
  45.  
  46.     print("\nDecrypting your message...\n")
  47.     sleep(2) # give an appearance of doing something complicated
  48.     print("Stand by, almost finished...\n")
  49.     sleep(2) # more of the same
  50.     print("Your decrypted message is:\n")
  51.     print(decrypted_message)
  52.  
  53. decrypt()
  54.  
  55. #########################################################################
  56. #Vigenere (ambos)
  57. LETRAS = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  58.  
  59.  
  60. def main():
  61.     mensaje = "como pez en el agua"
  62.     myKey = "ventilado"
  63.     accion = input("Mode: ")
  64.  
  65.     if accion == 'encriptar':
  66.         traducido = cifrar_mensaje(myKey, mensaje)
  67.     elif accion == 'descifrar':
  68.         traducido = descifrar_mensaje(myKey, mensaje)
  69.     print(traducido)
  70.  
  71.  
  72. def cifrar_mensaje(clave, mensa):
  73.     return traductor_mensaje(clave, mensa, 'encriptar')
  74.  
  75.  
  76. def descifrar_mensaje(clave, mensa):
  77.     return traductor_mensaje(clave, mensa, 'descifrar')
  78.  
  79.  
  80. def traductor_mensaje(clave, mensa, accion):
  81.     traducido = []
  82.     indice_clave = 0
  83.     clave = clave.upper()
  84.  
  85.     for symbol in mensa:
  86.         num = LETRAS.find(symbol.upper())
  87.         if num != -1:
  88.             if accion == 'encriptar':
  89.                 num += LETRAS.find(clave[indice_clave])
  90.             elif accion == 'descifrar':
  91.                 num -= LETRAS.find(clave[indice_clave])
  92.             num %= len(LETRAS)
  93.             if symbol.isupper():
  94.                 traducido.append(LETRAS[num])
  95.             elif symbol.islower():
  96.                 traducido.append(LETRAS[num].lower())
  97.             indice_clave += 1
  98.             if indice_clave == len(clave):
  99.                 indice_clave = 0
  100.  
  101.         else:
  102.             traducido.append(symbol)
  103.     return ('').join(traducido)
  104.  
  105.  
  106. if __name__ == '__main__':
  107.     main()
  108.  
Add Comment
Please, Sign In to add comment