2013-12-15 4 views
0

안녕하세요,이 스크립트에 문제가 있습니다. 이 스크립트는 기본적으로 입력을 요구하고 사용자가 입력 한 내용을 암호화 한 다음 사용자는 다시 돌아가 암호화 된 메시지를 입력하여 메시지의 암호를 해독 할 수 있습니다. 스크립트의 암호화 부분은 잘 작동하지만, 문제를 일으키는 해독 부분 일뿐입니다. 여기에 사전에 코드 감사 당연히입니다 : D파이썬에서 암호화 알고리즘을 반대로 교환

def encrypt(key, msg): 
    encryped = [] 
    for i, c in enumerate(msg): 
     key_c = ord(key[i % len(key)]) 
     msg_c = ord(c) 
     encryped.append(chr((msg_c + key_c) % 127)) 
    return ''.join(encryped) 

def decrypt(key, encryped): 
    msg2 = [] 
    for i, c in enumerate(encryped): 
     key_c = ord(key[i % len(key)]) 
     enc_c = ord(c) 
     msg.append(chr((enc_c - key_c) % 127)) 
    return ''.join(msg) 

welcome = str(input("Press 1 to encrypt or anything else to decrypt: ")) 
if welcome in ['1', '1']: 
    var = input("Please type in what you want to encrypt: ") 
    if __name__ == '__main__': 
     key = 'This is the key' 
     msg = var 
     encrypted = encrypt(key, msg) 
     decrypted = decrypt(key, encrypted) 
    print ('Please take down the key and encryption message to decrypt this message at a later stage') 
    print ('This is what you want to encrypt:' , repr(msg)) 
    print ('This is the encryption/decryption key:', repr(key)) 
    print ('This is the encrypted message:', repr(encrypted)) 

else: 
    var2 = input("Please enter your encrypted message that you would like to decrypt: ") 
    if __name__ == '__main__': 
     key = 'This is the key' 
     msg = var2 
     decrypted = decrypt(key, var2) 
    print ('This is the decrypted message:', repr(decrypted)) 
+0

암호화 된 메시지의 예를 들려 줄 수 있습니까? – RageCage

+0

암호화 된 경우 'Hello World'가 다음과 같이 표시됩니다. "\ x1dNV'O \ nkO'fD" – AbdulNaji

답변

0

은 약간 단순화 된 버전 :

def encrypt(key, msg): 
    encrypted = [] 
    for i, c in enumerate(msg): 
     key_c = ord(key[i % len(key)]) 
     msg_c = ord(c) 
     encrypted.append(chr((msg_c + key_c) % 127)) 
    return ''.join(encrypted) 

def decrypt(key, encryped): 
    msg = [] 
    for i, c in enumerate(encryped): 
     key_c = ord(key[i % len(key)]) 
     enc_c = ord(c) 
     msg.append(chr((enc_c - key_c) % 127)) 
    return ''.join(msg) 

if __name__ == '__main__': 
    welcome = input('Press 1 to encrypt or anything else to decrypt: ') 
    key = 'This is the key' 
    if welcome == '1': 
     msg = input('Please type in what you want to encrypt: ') 
     encrypted = encrypt(key, msg) 
     print('Please take down the key and encryption message to decrypt this message at a later stage') 
     print('This is what you want to encrypt:', repr(msg)) 
     print('This is the encryption/decryption key:', repr(key)) 
     print('This is the encrypted message:', repr(encrypted)) 
    else: 
     msg = input('Please enter your encrypted message that you would like to decrypt: ') 
     msg = msg.encode('utf-8').decode('unicode_escape') 
     decrypted = decrypt(key, msg) 
     print('This is the decrypted message:', repr(decrypted)) 

암호화 된 텍스트를 그들이 해독 입력에 탈출 진수 리터럴로 처음 32 ASCII 문자가 포함되어 있기 때문에 그러한 것으로 해석되지 않습니다. 이스케이프 된 리터럴을 처리하려면 msg.encode("utf-8").decode("unicode_escape")을 사용할 수 있습니다 (제안 된대로 here). ascii을 사용하면 암호화 된 텍스트가 최대 127 자의 ASCII 문자 만 포함하므로 인코딩 할 수 있습니다.

관련 문제