使用循环cyph加密消息

2024-05-03 11:10:12 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是一个例子:

  • 普通:ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • 班次=4
  • 在密码:defghijklmnopqrstuvxyzabc在

代码如下:

print ("This is a cyclic cipher program that will encrypt messages.")

#phrase = input("Please enter a phrase to encrypt.")
phrase = "ABCDEFG"
#shift_value = int(input ("Please enter a shift value between 1 - 5."))
shift_value = 1
encoded_phrase = ""
ascii_codes = 0
x = ""
#accepted_ascii_codes = range(65,90) and range(97,122)

for c in phrase:
ascii_codes = ord(c) # find ascii codes for each charcter in phrase
ascii_codes = ascii_codes + shift_value # add an integer (shift value) to ascii codes
phrase_rest = chr(ascii_codes) # convert ascii codes back to characters
encoded_phrase = encoded_phrase + c # stores the phrase character in a new variable
encoded_phrase = encoded_phrase.replace(c,phrase_rest) # replace original character

print (phrase) # prints "ABCDEFG"
print (encoded_phrase) # prints "HHHHHHH"

Tags: toininputshiftvalueasciirangecodes
0条回答
网友
1楼 · 发布于 2024-05-03 11:10:12

你在每个循环上重新编码你的加密字母,这将达到以下目的:

for c in phrase:
  ascii_codes = ord(c) # find ascii codes for each charcter in phrase
  ascii_codes = ascii_codes + shift_value # add an integer (shift value) to ascii codes
  phrase_rest = chr(ascii_codes) # convert ascii codes back to characters
  encoded_phrase = encoded_phrase + phrase_rest # stores the phrase character in a new variable

但是,您可能需要建立一个字典,其中包含原始字母和带密码的字母。然后你就可以循环一下,得到一个有密码的句子。例如:

^{pr2}$

相关问题 更多 >