凯撒';s密码使用python,需要一点帮助
我试图在使用python时制作一个“凯撒密码”。这就是我目前所拥有的。有人能告诉我这是什么样子吗?我走的方向对吗?我错过了什么?例如,当我运行程序说(josh很酷)我不会在同一行上得到密码。当我执行凯撒';s密码使用python,需要一点帮助,python,Python,我试图在使用python时制作一个“凯撒密码”。这就是我目前所拥有的。有人能告诉我这是什么样子吗?我走的方向对吗?我错过了什么?例如,当我运行程序说(josh很酷)我不会在同一行上得到密码。当我执行main(3) 但它把每个字母都换了一行。我怎样才能做到它在一条线上 def main(k): if k<0 or k>231: print "complaint" raise SystemExit Input = raw_input("
main(3)
但它把每个字母都换了一行。我怎样才能做到它在一条线上
def main(k):
if k<0 or k>231:
print "complaint"
raise SystemExit
Input = raw_input("Please enter Plaintext to Cipher")
for x in range(len(Input)):
letter=Input[x]
if letter.islower():
x=ord(letter)
x=x+k
if x>122:
x=x-122+97
print chr(x),
if letter.isupper():
x=ord(letter)
x=x+k
if x>90:
x=x-90+65
print chr(x),
def干管(k):
如果k231:
打印“投诉”
升起系统出口
输入=原始输入(“请输入明文密码”)
对于范围内的x(len(输入)):
字母=输入[x]
如果字母.islower():
x=ord(字母)
x=x+k
如果x>122:
x=x-122+97
打印chr(x),
如果字母.isupper():
x=ord(字母)
x=x+k
如果x>90:
x=x-90+65
打印chr(x),
在每个打印语句后加一个逗号;它仍然会在字符之间留出一个空格,但它们都在同一行上。如果您需要在不带空格的情况下打印它们,请将它们全部构建到一个字符串中,并在最后打印。除非出现语法错误,否则代码似乎可以正常工作 但是,我冒昧地删除了所有副本,并对其进行了清理:
first = raw_input("Please enter Plaintext to Cipher: ")
k = int(raw_input("Please enter the shift: "))
result = ''
for second in first:
x=ord(second)
x=x+k
if x>90 and x<122:
x=x-26
elif x>122:
x=x-26
result += chr(x)
print first
print result
first=raw\u输入(“请输入明文密码:”)
k=int(原始输入(“请输入班次:”)
结果=“”
第二个在第一:
x=ord(秒)
x=x+k
如果x>90和x122:
x=x-26
结果+=chr(x)
先印
打印结果
此外,“first”和“second”对于这些变量来说是非常糟糕的名称。
“输入”和“字母”可能更好。这段代码应该可以很好地工作。它还处理任意偏移,包括负偏移
phrase = raw_input("Please enter plaintext to Cipher: ")
shift = int(raw_input("Please enter shift: "))
result = ''
for char in phrase:
x = ord(char)
if char.isalpha():
x = x + shift
offset = 65
if char.islower():
offset = 97
while x < offset:
x += 26
while x > offset+25:
x -= 26
result += chr(x)
print result
phrase=raw\u输入(“请输入明文密码:”)
shift=int(原始输入(“请输入shift:”)
结果=“”
对于短语中的字符:
x=ord(字符)
如果char.isalpha():
x=x+shift
偏移量=65
如果char.islower():
偏移量=97
当x偏移量+25时:
x-=26
结果+=chr(x)
打印结果
另一种方法是使用稍有不同的密码,简单地在所有字符(大写和小写,甚至所有ascii>0x20)之间旋转
phrase = raw_input("Please enter plaintext to Cipher: ")
shift = int(raw_input("Please enter shift: "))
result = ''
for char in phrase:
x = ord(char)
x = x + shift
while x < 32:
x += 96
while x > 127:
x -= 96
result += chr(x)
print result
phrase=raw\u输入(“请输入明文密码:”)
shift=int(原始输入(“请输入shift:”)
结果=“”
对于短语中的字符:
x=ord(字符)
x=x+shift
当x<32时:
x+=96
当x>127时:
x-=96
结果+=chr(x)
打印结果
这里有一个不同的方法来展示我们如何以一种非常干净的方式处理这个问题。我们定义一个输入字母表和一个输出字母表,然后定义一个翻译表,并使用unicode.translate()
进行实际的加密
import string
# Blatantly steal Lennart's UI design
first = unicode(raw_input("Please enter Plaintext to Cipher: "), "UTF-8")
k = int(raw_input("Please enter the shift: "))
in_alphabet = unicode(string.ascii_lowercase)
out_alphabet = in_alphabet[k:] + in_alphabet[:k]
translation_table = dict((ord(ic), oc) for ic, oc in zip(in_alphabet, out_alphabet))
print first.translate(translation_table)
它可以根据需要扩展为大写字母。我喜欢kaizer.se的答案,但我认为我可以使用以下函数简化它:
import string
first = raw_input("Please enter Plaintext to Cipher: ")
k = int(raw_input("Please enter the shift: "))
shifted_lowercase = ascii_lowercase[k:] + ascii_lowercase[:k]
translation_table = maketrans(ascii_lowercase, shifted_lowercase)
print first.translate(translation_table)
我认为,没有Umlauts和类似设备的非常简单的三班制解决方案是:
def caesar(inputstring):
shifted=string.lowercase[3:]+string.lowercase[:3]
return "".join(shifted[string.lowercase.index(letter)] for letter in inputstring)
反过来说:
def brutus(inputstring):
shifted=string.lowercase[-3:]+string.lowercase[:-3]
return "".join(shifted[string.lowercase.index(letter)] for letter in inputstring)
使用它:
caesar("xerxes")
对于Python 3.3,请尝试使用ord()、chr()和.isalpha函数:
m = input("What is your message?: ")
s = int(input("What is the shift?: "))
for i in m:
if i.isalpha():
if (ord(i)+s)>90:
print(chr(ord(i)+s-26),end=""),
elif chr(ord(i)+s-26)<65:
print("The shift is invalid")
else:
print(chr(ord(i)+s),end=""),
else:
pass
m=input(“您的消息是什么?”)
s=int(输入(“移位是什么?:”)
对于我在m:
如果i.isalpha():
如果(作战需求文件(i)+s)>90:
打印(chr(ord(i)+s-26),结束=”),
elif chr(ord(i)+s-26)这是一条单行线
>>> brutus=lambda message,cipher,direction:''.join([chr((ord(letter)+(cipher*direction))%256) for letter in message])
>>> encrypted= brutus('Message to be encrypted',14,1) #direction=1 for encryption
>>> encrypted
'[s\x81\x81ous.\x82}.ps.s|q\x80\x87~\x82sr'
>>> brutus(encrypted,14,-1) # direction=-1 for decryption
'Message to be encrypted'
>>>
您缺少对问题的清晰解释,还有一个额外的问题,首先您缺少正确的语法…好的,谢谢。刚刚意识到我发布的版本中有我所有的语法错误。我有一个已经清理过了,但是谢谢你的帮助顺便问一下,你说的“请输入班次”是什么意思:“你的代码不处理标点符号。”。原始代码忽略了标点符号,因为它既不是大写字母也不是小写字母。在您的示例中,您添加了k而不考虑。@Josh:Cesar Cipher通过将字母移动到确定的数字来进行加密。杰夫:没错,我不处理标点符号,乔希的原始代码也不处理。这在典型的ceasar密码中是没有定义的。原来的代码在大写和小写之间确实有所不同,并且添加了k。我也这么做。我如何将它们构建成单个字符串?@Josh:在Python中,您可以构建一个类似于在打印中使用格式字符的新字符串,例如:new_str=“string part:%s,Int part:%d”%(some_string,some_Int)如果需要将其放入循环中,首先将字符串初始化为“”,然后在循环中使用+=将其添加到该字符串中。这个问题只需要一个字符串转换的应用程序。干得好:)
>>> brutus=lambda message,cipher,direction:''.join([chr((ord(letter)+(cipher*direction))%256) for letter in message])
>>> encrypted= brutus('Message to be encrypted',14,1) #direction=1 for encryption
>>> encrypted
'[s\x81\x81ous.\x82}.ps.s|q\x80\x87~\x82sr'
>>> brutus(encrypted,14,-1) # direction=-1 for decryption
'Message to be encrypted'
>>>