def menu():
choice = input("press 1 to encode, 2 to decode, 9 to exit ")
return choice
def makekeycode(message):
key = input("what is the key? ").upper()
length = len(message)
keycode = ""
counter = 0
while length >0:
if counter == len(key):
counter = 0
keycode = keycode + key[counter]
counter = counter + 1
length = length - 1
print(keycode)
return keycode
def entermessage():
message = input("what is the message ").upper()
return message
def encodemessage(message, keycode):
ciphertext =""
alphabet= "abcdefghijklmnopqrtuvwxyz"
for i in range (len(message)):
character = message[i]
charcode = alphabet.find(character)
keycodechar =keycode[i]
keyletter = alphabet.find(keycodechar)
position = (charcode + keyletter)%25
cipherletter = alphabet[position]
ciphertext =ciphertext + cipherletter
return ciphertext
def decodemessage(ciphertext,keycode):
ciphertext =""
alphabet= "abcdefghijklmnopqrtuvwxyz"
for i in range (len(ciphertext)):
character = ciphertext[i]
charcode = alphabet.find(character)
keycodechar =keycode[i]
keyletter = alphabet.find(keycodechar)
position = (charcode - keyletter)%25
cipherletter = alphabet[position]
ciphertext =ciphertext - cipherletter
return message
def entercipher ():
ciphertext = input("enter the text to be decoded")
return ciphertext
def encode():
message = entermessage()
keycode = makekeycode(message)
ciphertext = encodemessage(message,keycode)
print(ciphertext)
def decode():
keycode = makekeycode(ciphertext)
message = decodemessage(ciphertext, keycode)
print (message)
def main():
mydictionary=("a:1","b:2","c:3","d:4","e:5","f:6","g:7","h:8","i:9","j:10","k:11","l:12","m:13","n:14","o:15","p:16","q:17","r:18","s:19","t:20","u:21","v:22","w:23","x:24","y:25","x:26")
print (mydictionary)
choice = 0
while choice !=9:
choice = int(menu())
if choice == 1:
encode()
elif choice == 2:
decode()
if __name__ == "__main__":
main()
嗨,我無法使我的**正常工作,我的編碼功能正常工作,但是我正在努力修復我的解碼功能.我不明白我要去**錯了.我希望能夠對我將要編碼的訊息進行解碼,但是由於解碼功能停止了該程式,因此無法正常工作.我在編碼**錯了嗎?謝謝你的幫助
問候
python字母移位 python移位運算的實現
密碼演算法程式設計實踐選的sha 1。在寫的過程中遇到一丟丟關於python移位的問題,記錄一下。sha 1其中第一步需要填充訊息。簡單闡述一下sha1填充訊息的過程 如輸入訊息 123 先轉成ascii碼 313233,訊息長度為3 8 24。即00110001 00110010 00110011...
python字母移位 演算法 字串移位
問題 乙個字串可以由另乙個字串移位得到,例如abcd,可以由bcad移位得到。問題分析 這個問題表面上說的是字串,但是其實更進一步可以理解為兩個字元陣列的元素是否一致。最簡單和直白的方式,無異於用兩層迴圈的方式來進行迴圈判斷。這是常規方案一。還有方案二,則是需要用到資料結構,例如,將乙個字串轉換成乙...
848 字母移位
有乙個由小寫字母組成的字串s,和乙個整數陣列shifts。我們將字母表中的下乙個字母稱為原字母的 移位 由於字母表是環繞的,z 將會變成 a 例如 shift a b shift t u 以及shift z a 對於每個shifts i x,我們會將s中的前i 1個字母移位x次。返回將所有這些移位都...