2017-04-18 52 views
0

你好,大家好我Python的初學者,試圖讓使用由我的示例中定義的加密密鑰,我做了這個代碼密匙,但它不工作:Python的自動加密程序

def encrypt(): 
    A = "f" 
    B = "d" 
    C = "z" 
    T = "x" 
    first = input() 
    print(first) 

我希望程序當用戶進入CAT輸入輸出應該是ZAX之類的函數,但是當我輸入甚至一個在我目前的計劃是打印 一個,因爲它是。

任何幫助,將不勝感激!

+0

使用'dict':'enc = {'A':'f','B':'d',...}',然後做'enc [first]'。 –

+3

你實際上沒有對輸入做任何事情。如果你想要改變,你需要在'first'上執行一些操作。 – FamousJameous

+0

@RocketHazmat好吧,讓我看看它是否有效! –

回答

1

您可以只使用string.translate方法。示例代碼是:

def encrypt(): 
    encryption_table = { 
     ord('A'):ord('f'), 
     ord('B'):ord('d'), 
     ord('C'):ord('z'), 
     ord('T'):ord('x'),       
    } 
    first = input() 
    print(first.translate(encryption_table)) 

對於此代碼,輸入CAT將返回zfx。

-3

這是一個簡單的加密和解密腳本,如果你想使用它。密碼部分可以省略,或者可以將其放入。加載欄也是如此。加載欄位於1-4行,密碼位於最後9行。如果你確實取出密碼,請確保呼叫開始。您還需要通過pip安裝tqdm。

import time 
from tqdm import * 
for i in tqdm(range(1000)): 
    time.sleep(0.001) 
def encrypt(some_string): 
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' 
    cipher = 'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA' 
    encryption = '' 
    for char in some_string: 
     if(alphabet.find(char) == -1): 
      encryption = encryption + char 
     else: 
      position = alphabet.index(char) 
      encryption = encryption + cipher[position] 
    return encryption 
def decrypt(some_string): 
    cipher = 'bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZa' 
    alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' 
    decryption = '' 
    for char in some_string: 
     if(cipher.find(char) == -1): 
      decryption = decryption + char 
     else: 
      position = cipher.index(char) 
      decryption = decryption + alphabet[position] 
    return decryption 
def restart(): 
    print('Would you like to restart.') 
    print('1: Yes.') 
    print('2: No.') 
    c = float(input('Type a 1 or a 2: ')) 
    if c == 1: 
     print('Ok you can restart.') 
     start() 
    if c == 2: 
     print('Sorry to see you leave.') 
     quit() 
    else: 
     print('Invalid response.') 
     restart() 
def start(): 
    print('1: Encryption') 
    print('2: Decryption') 
    e = float(input('Type a 1 or a 2: ')) 
    if e == 1: 
     print('Type your message here.') 
     e = str(input('Enter message to encrypt: ')) 
     print(encrypt(e)) 
     restart() 
    if e == 2: 
     print('Type your messed up message here.') 
     e = str(input('Enter your messed up message: ')) 
     print(decrypt(e)) 
     restart() 
    else: 
     print('Please type a 1 or a 2.') 
     start() 
def password(): 
    a = float(input('Enter the correct password please: ')) 
    if a == 5194703: 
     print('Correct!') 
     start() 
    else: 
     print('Invalid Password, try again!') 
     password() 
password() 
1

首先,你應該做一個字典映射你的來信替代,如:

my_dict = { 'A':'f', 'B':'d', 'C':'z', 'T':'x', ... } 

然後,你需要一個encrypt函數返回該字典的值:

def encrypt(data): 
    return ''.join(my_dict[d] if d in my_dict else d for d in data) 

而且與decrypt功能相反:

def decrypt(data): 
    my_dict_rev = dict((v,k) for k,v in my_dict.items()) 
    return ''.join(my_dict_rev[d] if d in my_dict_rev else d for d in data) 

現在讓我們來測試一下:

my_data = 'TEST DATA' 
enc_data = encrypt(my_data) 
dec_data = decrypt(enc_data) 

輸出:

print(my_data) 
print(enc_data) 
print(dec_data) 

TEST DATA 
xESx Dfxf 
TEST DATA