-2

私が書いたコードは、キーワードを使用してメッセージを暗号化する Vignere Cipher 暗号化プログラムです。私はこのコードを書き、それを実行したところ、暗号化されたメッセージを出力するだけで、想定されていたすべてのことを実行しました。以下の私のコードを参照してください。

    ans = False
print(""" *****Hello. Welcome to the Vignère Cipher Encryption Program*****
    ***This program uses a keyword that is repeated until it
    matches the same lenght of the message and then adds its
    numerical value to the numerical value of the message and
    outputs the encrypted message in alpha. 
    Please press:
    E to Encrypt
    D to Decrypt
    or  double tap enter to quit.
    """)

ans=input("What would you like to do now???")

if ans == "E":
    plaintext = input("Please enter a message to be encrypted: ").upper()
    keyword = input("Please enter a keyword to be used to encrypt a message (alpha only): ").upper()
    ciphered = " "
    for i in range (len(plaintext)):
        char = plaintext[i]
        alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
        if char.isupper():
            if ans == "E" :
                value = ord(char) + alphakeywordvalue 
                if value > ord("Z"): 
                    value -= 26
                    print ("Your encrypted text is:", ciphered)


elif ans == "D":
    plaintext = input("Please enter a message to be dencrypted: ").upper()
    keyword = input("Please enter a keyword to be used to dencrypt a message (alpha only(make sure that it is the same keyword used to encrypt the message)): ").upper()
    ciphered = " "
    for i in range (len(plaintext)):
        char = plaintext[i]
        alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
        if char.isupper():
            if ans == "D" :
                value = ord(char) - alphakeywordvalue
                if value <ord("A"):
                    value += 26
                ciphered += chr(value)
                print ("Your decrypted text is:", ciphered)
4

2 に答える 2

3

これはコードの書き方としては適切ではありません。非常に下品で読みにくい。必要に応じて、個々のパーツのメソッドを作成し、main() のようなセクションを個別に作成して、ユーザーとやり取りする必要があります。エンジンは隠して、車体は磨く。それらを別々に保管してください。はい、同じことを繰り返さないでください

さて、これが私の書き直したコードです。重要な部分にはコメントが含まれています。エラーはその後に説明されています。

def encrypt(message, key, direction='E'):
    # Look here. There are three arguments. Third one takes 'E' to encrypt
    # and anything else to decrypt. You can modify to handle more cases

    ciphered = "" # Initialize. You did it almost well
    for i in range (len(message)):
        char = message[i]
        alphakeywordvalue = ord(key[i%len(key)]) - ord("A")+1 # Perfect. We took the key
        if direction=='E': # To encrypt
            value = ord(char) + alphakeywordvalue 
        else: # To decrypt
            value = ord(char) - alphakeywordvalue 
        ciphered += chr(value) # chr is the inverse of ord. It gets the character back
        # You missed this line
    return ciphered

それでおしまい。これで、ユーザーまたは他のモジュールと対話するコードを作成できます。サンプルテストは次のとおりです。

message = "Hello World"
key = "abc"
print "ORIGINAL  : "+message

encoded_message = encrypt(message, key, 'E')
print "ENCRYPTED : "+encoded_message

plain_message = encrypt(encoded_message, key, 'D')
print "DECRYPTED : "+plain_message

出力は次のとおりです。 ここに画像の説明を入力

このメソッドを編集して、ASCII 範囲外の文字など、より多くのケースを処理できるようになりました。

于 2015-06-11T07:40:24.283 に答える
2

暗号化されたメッセージをどのように出力できますか。暗号化ルーチンはciphered空の文字列から変更されることはありません。

    if ans == "E":
        plaintext = input("Please enter a message to be encrypted: ").upper()
        keyword = input("Please enter a keyword to be used to encrypt a message (alpha only): ").upper()
1)->    ciphered = " "
        for i in range (len(plaintext)):
            char = plaintext[i]
            alphakeywordvalue = ord(keyword[i%len(keyword)]) - ord("A")+1
2)->        if char.isupper():
3)->            if ans == "E" :
                    value = ord(char) + alphakeywordvalue 
                    if value > ord("Z"): 
                        value -= 26
4)->                    print ("Your encrypted text is:", ciphered)
  1. ciphered空の文字列に設定され、変更されることはありません。
  2. すべての平文を upper() にしたので、常に char が upper であることがわかります。
  3. ans == "E"以前にテストしたのでわかります
  4. この print() はインデントされているため、ループのたびに印刷を試みます
于 2015-06-11T07:32:20.387 に答える