Skip to content

Instantly share code, notes, and snippets.

@bergpb
Last active March 20, 2020 22:54
Show Gist options
  • Select an option

  • Save bergpb/3b46ec7f0e59cb37a087c7212ed5560c to your computer and use it in GitHub Desktop.

Select an option

Save bergpb/3b46ec7f0e59cb37a087c7212ed5560c to your computer and use it in GitHub Desktop.
Cifra de César em Python
import string
from random import randint
texto_cifrado = ''
texto_original = ''
deslocamento = randint(1, 26)
alfabeto = list(string.ascii_lowercase)
print('Deslocamento igual a: {}'.format(deslocamento))
texto = input('Insira texto a ser cifrado, ou aperte enter para texto padrão: ')
if texto == '':
texto = 'este e um texto de exemplo'
for i in range(0, len(texto)):
try:
posicao = alfabeto.index(texto[i])
nova_posicao = posicao+deslocamento
if((nova_posicao) >= 26):
nova_posicao-=26
texto_cifrado += alfabeto[nova_posicao]
except:
texto_cifrado += ' '
print('Texto cifrado: {}'.format(texto_cifrado))
for i in range(0, len(texto_cifrado)):
try:
posicao = alfabeto.index(texto_cifrado[i])
texto_original += alfabeto[posicao-deslocamento]
except:
texto_original += ' '
print('Texto original: {}'.format(texto_original))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment