2012-03-01 104 views
2

這裏加密的消息是一個例子:使用循環暗號

  • 平原:ABCDEFGHIJKLMNOPQRSTUVWXYZ
  • 移= 4
  • 密碼:DEFGHIJKLMNOPQRSTUVWXYZABC

這裏是代碼:

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" 

回答

0

你被重新加密oding在每次循環的cyphered信,這會做的伎倆:

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 

然而,你可能想建立一個字典,原信和cyphered之一。然後你會循環它們並獲得加密的句子。例如:

cypher = {'a': 'x', 'b': 'y', ... } 
encoded = '' 
for c in phrase: 
    encoded += cypher[c] 
+0

謝謝FMC!我對字典不熟悉,但我會嘗試一下。 – Mavvy1981 2012-03-02 15:15:56