Created
October 4, 2014 00:11
-
-
Save seocam/7fe11088f9937a7d7397 to your computer and use it in GitHub Desktop.
Cifras em Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Header ^^^ por causa de acento no código fonte | |
import string | |
from random import shuffle | |
def cesar_letra(char, shift): | |
conjunto = list(string.ascii_letters) | |
conjunto += [u' ', u'é', u'!'] # u na frente da string por causa de acento | |
atual = conjunto.index(char) | |
return conjunto[(atual + shift) % len(conjunto)] | |
def cesar(msg, shift): | |
nova_msg = u'' # u na frente da string por causa de acento | |
for letra in msg: | |
nova_msg += cesar_letra(letra, shift) | |
return nova_msg | |
def init_alfabeto(): | |
original = list(string.ascii_letters) | |
embaralhada = original[:] | |
shuffle(embaralhada) | |
return original, embaralhada | |
def encripta(msg): | |
nova_msg = u'' # u na frente da string por causa de acento | |
for letra in msg: | |
index = original.index(letra) | |
nova_msg += embaralhada[index] | |
return nova_msg | |
def decripta(msg): | |
nova_msg = u'' | |
for letra in msg: | |
index = embaralhada.index(letra) | |
nova_msg += original[index] | |
return nova_msg | |
if __name__ == '__main__': | |
original, embaralhada = init_alfabeto() | |
msg = raw_input('Mensagem: ') | |
msg = msg.decode('UTF-8') # por causa de acento | |
step = raw_input('Step: ') | |
step = int(step) | |
msg_encriptada = cesar(msg, step) | |
print(u'Mensagem encriptada: {}'.format(msg_encriptada)) # u na frente da string por causa de acento |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment