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"
4

1 に答える 1

0

暗号化された文字を各ループで再エンコードしています。これでうまくいきます。

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

ただし、元の文字と暗号化された文字を使用して辞書を設定することをお勧めします。次に、それらをループして、暗号化された文を取得します。例えば:

cypher = {'a': 'x', 'b': 'y', ... }
encoded = ''
for c in phrase:
  encoded += cypher[c]
于 2012-03-02T05:10:04.793 に答える