Caesar cypher in Python

蹲街弑〆低调 提交于 2019-12-13 09:54:16

问题


I am new to python and I want to write a program that asks for a line of input (each number will be separated by a single space.) It would use a simple letter-number substitution cipher. Each letter will be appointed a number. So 1 = a, 2 = b and 3 = c until it reaches 26 = z. From there, however, the cipher would continue on so; 27 = a, 28 = b etc. 0's will be a space. The program will only use 0's and positive numbers. It would also print out the decryption of the message. For example:

Please type a code: 8 5 12 12 15

hello

and another example:

Please type a code: 16 25 20 8 14 0 9 19 0 3 15 15 12

python is cool

At the moment i have tried turning the alphabet into a list like this;

    n = int(input("Please type a code: ")
    x =['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']

...and then referring back to it later. However, im not exactly sure how this would work. I've also tried using the .replace() function. Like this:

    n = int(input("Please type a code: ")
    n = n.replace('1','a') #all the way until z and then loop it.
    print(n)

and so and and so on. But again, i have no idea how to do this properly. Any help would be greatly appreciated. Thanks.


回答1:


Use split()

numbers = input("Please type a code: ").split()

# ['16', '25', '20', '8', '14', '0', '9', ',19', '0', '3', '15', '15', '12']

Use for .. in ..

for num in numbers:
    print( x[int(num)] )

If you use 0 as space you have to add space at the begginig of list

x = [' ', 'a', 'b', 'c', .... ]

now x[0] == ' '




回答2:


Just create a function such as the following:

>>> def numerate(char):
...    alpha = 'abcdefghijklmnopqrstuvwxyz'
...    place = alpha.index(char)
...    return alpha
...
>>> sent = raw_input('Enter your sentence to be encrypted: ')
Enter your sentence to be encrypted: python is so cool
>>> encrypted = []
>>> for k in sent:
...    encrypted.append(numerate(k))
...
>>> encrypted
[15, 24, 19, 7, 14, 13, 26, 8, 18, 26, 18, 14, 26, 2, 14, 14, 11]

Now you just have to reverse this to 'denumerate', or decode it.




回答3:


You can simplify much of the code by using map.

def caesar_cipher(plaintext, shift):
    plaintext = plaintext.lower()
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    shift_function = lambda x: (alphabet.index(x) + shift) % 26

    ciphertext = map(shift_function, ciphertext)
    return ciphertext

The only weird portion is the lambda. The lambda just changes the character to the number representing its position in the alphabet and applies the shift mod 26 to ensure wrap around. The reversal process is very similar.

Hope this helps!




回答4:


Hope this helps!this is my simple Caesar cypher code

Encryption code

l=[' ','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']
demeg=raw_input("plz enter your message for encryption:")
key=raw_input('Enter your encryption key(key must be a number): ')
m=list(demeg)
le=len(m)
message=[]
j=0
while le>0:
    p=[i for i,x in enumerate(l) if x == m[j]]
    p=p[0]
    p=p+int(key)
    p=str(p)
    message.append(p)
    j+=1
    le-=1

message=' '.join(message)   
print message

this is my decryption code

l=[' ','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']
get=raw_input('Please type a code for decryption: ')
key=raw_input('Enter your secret decryption key: ')
st=str(get)
m=st.split()
n=len(m)
i=0
message=[]
while n>0:

    s=m[i]
    s=int(s)
    s=s-int(key)
    if s>26:
        s=s-26
    word=l[s]
    message.append(word)
    i+=1
    n-=1
meg=''.join(message)
print meg 


来源:https://stackoverflow.com/questions/22059435/caesar-cypher-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!