Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写Ceser密码Python_Python_Encryption_Caesar Cipher - Fatal编程技术网

如何编写Ceser密码Python

如何编写Ceser密码Python,python,encryption,caesar-cipher,Python,Encryption,Caesar Cipher,我不知道如何开始编写程序 input=input(“输入您想要加密的文本”) def密码文本(字母代码): 对于输入中的i: 编号\u代码=ord(i)+3 字母代码=chr(数字代码) 打印(字母代码) def纯文本(字母代码、常规文本): 对于输入中的i: 常规文本=字母代码-3 打印(常规文本) 打印(“加密文本”) 密码文本() 打印(“未加密文本”) 纯文本 对不起,我不知道该怎么开始。另外,请给出建议而不是答案。从数学上讲,如果我们在凯撒密码中看到加密技术,那么获得加密字母的公式是

我不知道如何开始编写程序

input=input(“输入您想要加密的文本”)
def密码文本(字母代码):
对于输入中的i:
编号\u代码=ord(i)+3
字母代码=chr(数字代码)
打印(字母代码)
def纯文本(字母代码、常规文本):
对于输入中的i:
常规文本=字母代码-3
打印(常规文本)
打印(“加密文本”)
密码文本()
打印(“未加密文本”)
纯文本

对不起,我不知道该怎么开始。另外,请给出建议而不是答案。

从数学上讲,如果我们在
凯撒密码
中看到
加密
技术,那么获得加密字母的公式是:

c = (x+n) mod 26, 
其中,
c
是加密字母的位置值,
x
是实际字母的位置值,
n
是移位。 类似地,要解密每个字母,我们使用以下公式:

c = (x-n) mod 26
您可以使用下面的代码了解如何实现凯撒密码:

def encrypt(plain_text, s):
    encrypted_text = ''
    for i in range(len(plain_text)):
        if plain_text[i] == ' ':
            encrypted_text = encrypted_text + plain_text[i]
        elif plain_text[i].isupper():
            encrypted_text = encrypted_text + chr((ord(plain_text[i])+s-65)%26+65)
        else:
            encrypted_text = encrypted_text + chr((ord(plain_text[i])+s-97)%26+97)
    return encrypted_text


def decrypt(encrypt_text, s):
    decrypted_text = ''
    for i in range(len(encrypt_text)):
        if encrypt_text[i] == ' ':
            decrypted_text = decrypted_text + encrypt_text[i]
        elif encrypt_text[i].isupper():
            decrypted_text = decrypted_text + chr((ord(encrypt_text[i])-s-65)%26+65)
        else:
            decrypted_text = decrypted_text + chr((ord(encrypt_text[i])-s-97)%26+97)
    return decrypted_text



plain_text = input("Input the text you would like encrypted:")
s = int(input("Enter shift:"))
encrypt_text = encrypt(plain_text, s)
print("Encrypted text: {}".format(encrypt_text))
print("Decrypted text: {}".format(decrypt(encrypt_text, s)))
样本输出:

Input the text you would like encrypted:Taj Mahal

Enter shift:3
Encrypted text: Wdm Pdkdo
Decrypted text: Taj Mahal

如果你不知道从哪里开始,用英语解释事情发生的顺序。然后将其转换为函数,并描述每个函数应该接受什么以及应该返回什么。