Saltar al contenido

ejemplo de código python de cifrado caesar

Ejemplo 1: cifrado césar de python

def caesar_encrypt():
    word = input('Enter the plain text: ')
    c = ''
    for i in word:
        if (i == ' '):
            c += ' '
        else:
            c += (chr(ord(i) + 3))
    return c

def caesar_decrypt():
    word = input('Enter the cipher text: ')
    c = ''
    for i in word:
        if (i == ' '):
            c += ' '
        else:
            c += (chr(ord(i) - 3))
    return c
  
plain = 'hello'
cipher = caesar_encrypt(plain)
decipher = caesar_decrypt(cipher)

Ejemplo 2: cifrado césar de python

plaintext = input("Please enter your plaintext: ")
shift = input("Please enter your key: ")
alphabet = "abcdefghijklmnopqrstuvwxyz"
ciphertext = ""

# shift value can only be an integer
while isinstance(int(shift), int) == False:
  # asking the user to reenter the shift value
  shift = input("Please enter your key (integers only!): ")

shift = int(shift)
  
new_ind = 0 # this value will be changed later
for i in plaintext:
  if i.lower() in alphabet:
    new_ind = alphabet.index(i) + shift
    ciphertext += alphabet[new_ind % 26]
  else:
    ciphertext += i    
print("The ciphertext is: " + ciphertext)

Ejemplo 3: pitón cifrado césar

def cc_encrypt(msg: str,key: int) -> str:
    encrypted_msg = ''

    try:
        for char in msg:
            encrypted_msg += str(chr(ord(char)+int(key)))
    except:
        print(Exception)
        pass
    
    return encrypted_msg

def cc_decrypt(msg: str,key: int) -> str:
    decrypted_msg = ''

    try:
        for char in msg:
            decrypted_msg += chr(ord(char)-int(key))
    except:
        print(Exception)
        pass

    return decrypted_msg

  
message = 'Hello World!'
key = 9
print(f'Caesar Cipher:nEncrypted: {cc_encrypt(message,key)}nDecrypted: {cc_decrypt(cc_encrypt(message,key),key)}')

Ejemplo 4: cifrado césar en python

def encrypt(text,s):
result = ""
   # transverse the plain text
   for i in range(len(text)):
      char = text[i]
      # Encrypt uppercase characters in plain text
      
      if (char.isupper()):
         result += chr((ord(char) + s-65) % 26 + 65)
      # Encrypt lowercase characters in plain text
      else:
         result += chr((ord(char) + s - 97) % 26 + 97)
      return result
#check the above function
text = "CEASER CIPHER DEMO"
s = 4

print "Plain Text : " + text
print "Shift pattern : " + str(s)
print "Cipher: " + encrypt(text,s)
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *