0
爲什麼Vigenere密碼只能正確加密部分消息?我們開始寫凱撒密碼,並使用爲凱撒創建的幫助函數,畢業於Vigenere密碼。我的alphabet_position和rotate_character函數似乎正在工作,但是我的Vigenere的加密函數只是正確地返回了加密消息的一部分。
例如, 打印加密( 'BaRFoo',巴茲) 應返回: 'CaQGon' 竟回答道: 'CakGo \ X88'爲什麼Vigenere密碼只能正確加密部分消息?
下面是到目前爲止我的代碼:
import string
def alphabet_position(letter):
return (ord(letter))
def rotate_character(char,rot):
encoded = ""
# loop over characters for ord
char_ord = alphabet_position(char)
# non-alphabet characters
if (char_ord > 90 and char_ord < 97) or char_ord < 65 or char_ord >122:
encoded = encoded + char
return encoded
else:
# set each according to whether upper or lower case
# ord of "Z" is 90, so 91 for upper range and ord of "A" is 65 so
uppercase boundary
if char.isupper():
asciirange = 91
asciibound = 65
else:
# ord of "z" is 122 so 123 for upper range and ord of "a" is 97
so lowercase boundary
asciirange = 123
asciibound = 97
enc_char = ((char_ord + rot) % asciirange)
if enc_char < asciibound:
enc_char = (enc_char + asciibound)
encoded = encoded + (chr(enc_char))
else:
encoded = encoded + (chr(enc_char))
return (encoded)
def encrypt(text,keyword):
encoded_text = ""
key_start = 0
# find rot
alpha = string.ascii_letters
for char in text:
# check if char is a letter
if char.isalpha():
key_num = (alphabet_position(keyword[key_start]))
# convert key_num to a letter to find its index
rot = alpha.find(chr(key_num))
encoded_text += (rotate_character(char,rot))
if key_start == (len(keyword)-1):
key_start = 0
else:
key_start += 1
else:
encoded_text += char
return encoded_text
嗨!歡迎來到堆棧溢出,請閱讀[問]。我認爲你需要更具體一點關於你的代碼問題是什麼 – Mick
謝謝米克。我重新寫了我的問題更具體:) – gtg