angeldp

Caesar cipher

Aug 22nd, 2024
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  2. def encrypt(original_text, shift_amount):
  3.     original_text=original_text.lower()
  4.     deco=""
  5.     for letter in original_text:
  6.         if letter not in alphabet:
  7.             deco+=letter
  8.         else:
  9.             deco+= alphabet[(alphabet.index(letter) + shift_amount) % len(alphabet)]
  10.     print(deco)
  11. def decrypt(mensaje,salt):
  12.     deco = ""
  13.     for letter in mensaje:
  14.         if letter not in alphabet:
  15.             deco += letter
  16.         elif (alphabet.index(letter) - salt) < 0:
  17.             deco += alphabet[(alphabet.index(letter) - salt) + len(alphabet)]
  18.         else:
  19.             deco += alphabet[(alphabet.index(letter) - salt)]
  20.     print(deco)
  21. def caesar(direction):
  22.     if direction == 'e':
  23.         text = input("Type your message:\n").lower()
  24.         shift = int(input("Type the shift number:\n"))
  25.         encrypt(text, shift)
  26.     else:
  27.         text = input("Type your message:\n").lower()
  28.         shift = int(input("Type the shift number:\n"))
  29.         decrypt(text, shift)
  30.  
  31. u_choice = 'x'
  32. while u_choice != 'e' and u_choice != 'd' :
  33.     u_choice = input("Pulse 'd' para desencriptar o 'e' para encriptar: ").lower()
  34.     if u_choice == 'e' or u_choice == 'd':
  35.         caesar(u_choice)
  36.         option=input("¿Quiere más? (y/n): ").lower()
  37.         if option == 'y':
  38.             u_choice='x'
  39.     else:
  40.         print("Opción incorrecta")
Tags: 100DoC
Advertisement
Add Comment
Please, Sign In to add comment