Python 凯撒密码 [已关闭]

问题描述 投票:0回答:3

我需要为凯撒密码创建一个代码。我需要使用 for 循环来检查文本的每个字符,使用 ALPHABET、key 和 String 函数查找移位字符,并将其添加到“encipheredTextSoFar”的末尾。需要帮助,尚未完成,但我被困住了。

编辑:

我终于明白了,这是我的最终代码:

def circularShift(text, key):
    text = text.upper()
    cipher = ""
    for letter in text:
        shifted = ord(letter) + key
        if shifted < 65:
            shifted += 26
        if shifted > 90:
            shifted -= 26
        cipher += chr(shifted)
    return cipher

print (circularShift("MOLLOY", 3))
print (circularShift("PROORB", -3))
python caesar-cipher
3个回答
1
投票

简单实现使用:

string.maketrans

 import string
 upper_case = string.uppercase
 trans = lambda x,n:string.maketrans(x,x[n:]+x[:n])
 def ceaser(text,n):
    print text.upper().translate(trans(upper_case,n))
 ceaser("MOLLOY",3)
 ceaser("HELLO",6)

输出:

PROORB
KHOOR

0
投票

Python源代码:

# setting status default value to 'y'
status = 'y'

# started a while loop until status will change from 'y' to 'n'
while status == 'y':
    # requesting which word to cipher.
    word = str(raw_input("Word: "))
    # how many position to shift each letter
    shift = int(input("Shift: "))
    # added new line to have pretty output
    print

    # declaring cipher variable to be of type list
    cipher = list()
    # iterating trough each character in word
    for item in word:
        # translanting each characher in word to new character
        # and appending it to cipher list
        cipher.append(chr(ord(item)+shift))

    # printing word
    print word
    # printing ciphered word
    print ''.join(cipher)

    print
    # do We need to cipher another word? yes or no?
    status = raw_input("Repeat? [y|n]: ")
    print    

输出:

>>> 
Word: HelLo
Shift: 1

HelLo
IfmMp

Repeat? [y|n]: y

Word: AbCdEF
Shift: 2

AbCdEF
CdEfGH

Repeat? [y|n]: y

Word: abc
Shift: -1

abc
`ab

Repeat? [y|n]: y

Word: 456GHh
Shift: -2

456GHh
234EFf

Repeat? [y|n]: n

>>> 

0
投票
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']

def encrypt(texted, shifted):
    crypted_message = ''
    for letter in texted:
        if letter in alphabet:
            index_in_alphabet = alphabet.index(letter)
            crypted_message += alphabet[(index_in_alphabet + shifted) % 26]
        else:
            crypted_message += letter
    print(f"message crypté : {crypted_message}")

def decrypt(texted, shifted):
    decrypted_message = ''
    for letter in texted:
        if letter in alphabet:
            index_in_alphabet = alphabet.index(letter)
            decrypted_message += alphabet[(index_in_alphabet - shifted) % 26]
        else:
            decrypted_message += letter
    print(f"message decrypté : {decrypted_message}")

request = 'yes'
while request == 'yes':
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt : \n")
    while direction != "encode" and direction != "decode":
        print("Entrez soit 'encode' ou 'decode'")
        direction = input("Type 'encode' to encrypt, type 'decode' to decrypt : \n")

    text = input("Type your message:\n").lower()
    shift = int(input("Type the shift number:\n"))
    while shift > 26:
        print("Enterez un chiffre entre 0 et 25")
        shift = int(input("Type the shift number:\n"))

    if direction == 'encode':
        encrypt(texted=text, shifted=shift)
    else:
        decrypt(texted=text, shifted=shift)
    request = input("Type 'yes' if u want go agin . 'no' if else :\n")
© www.soinside.com 2019 - 2024. All rights reserved.