2017-04-04 56 views
1

我正在積極學習Python(3.5)並且真正享受它。python crypter(字母轉換)

我想要熟悉字符串操作,因此決定製作一個基本的加密器/解密器,將字符串中的字符串向前移一位。

下面的代碼:

def encrypt(string): 
    string = string.replace('z', 'a') 
    string = string.replace('y', 'z') 
    string = string.replace('x', 'y') 
    string = string.replace('w', 'x') 
    string = string.replace('v', 'w') 
    string = string.replace('u', 'v') 
    string = string.replace('t', 'u') 
    string = string.replace('s', 't') 
    string = string.replace('r', 's') 
    string = string.replace('q', 'r') 
    string = string.replace('p', 'q') 
    string = string.replace('o', 'p') 
    string = string.replace('n', 'o') 
    string = string.replace('m', 'n') 
    string = string.replace('l', 'm') 
    string = string.replace('k', 'l') 
    string = string.replace('j', 'k') 
    string = string.replace('i', 'j') 
    string = string.replace('h', 'i') 
    string = string.replace('g', 'h') 
    string = string.replace('f', 'g') 
    string = string.replace('e', 'f') 
    string = string.replace('d', 'e') 
    string = string.replace('c', 'd') 
    string = string.replace('b', 'c') 
    string = string.replace('a', 'b') 
    return string 


def decrypt(string): 
    string = string.replace('b', 'a') 
    string = string.replace('c', 'b') 
    string = string.replace('d', 'c') 
    string = string.replace('e', 'd') 
    string = string.replace('f', 'e') 
    string = string.replace('g', 'f') 
    string = string.replace('h', 'g') 
    string = string.replace('i', 'h') 
    string = string.replace('j', 'i') 
    string = string.replace('k', 'j') 
    string = string.replace('l', 'k') 
    string = string.replace('m', 'l') 
    string = string.replace('n', 'm') 
    string = string.replace('o', 'n') 
    string = string.replace('p', 'o') 
    string = string.replace('q', 'p') 
    string = string.replace('r', 'q') 
    string = string.replace('s', 'r') 
    string = string.replace('t', 's') 
    string = string.replace('u', 't') 
    string = string.replace('v', 'u') 
    string = string.replace('w', 'v') 
    string = string.replace('x', 'w') 
    string = string.replace('y', 'x') 
    string = string.replace('z', 'y') 
    string = string.replace('a', 'z') 
    return string 

choice = input('Do you want to decrypt or encrypt a sentence? (d/e)') 
question = 'Give me a sentence to %s\n' 

if choice == 'd': 
    encrypted_str = input(question % 'decrypt') 
    decrypted_str = decrypt(encrypted_str) 
    print(decrypted_str) 

elif choice == 'e': 
    plaintext = input(question % 'encrypt') 
    encrypted_str = encrypt(plaintext) 
    print(encrypted_str) 

else: 
    print('That is not a valid option') 

我知道這是不是應該怎麼做,但我不知道是怎麼回事。

我遇到的問題是,如果我嘗試加密'zaaz',它會給我'bbbb'而不是'abba'。我知道我的錯在哪兒('z'被'a'和'a'替換爲'b'),它是什麼,但不是如何解決它。有人可能會建議一個更好的方法來做到這一點。

P.S.我看到有人使用模運算符將字符串作爲字符串進行文本換行或字符串索引,但我不知道如何在此處實現它。

有什麼建議嗎?

+0

你可能會覺得很有幫助的是,python中的'str'類型是可迭代的 - 你可以編寫像'myStr'中的字符一樣的代碼並單獨查看每個字符......來修復你當前擁有的代碼,而不是完全重新開始你可能想考慮一下! – jambrothers

回答

0
strssmall = 'abcdefghijklmnopqrstuvwxyz' 
strscaps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 

shift = 1 # How many characters need to shift 
def encrypt(inp): 
    data = [] 
    for i in inp: 
     if i.strip() and i in strssmall: 
      data.append(strssmall[(strssmall.index(i) + shift) % 26]) 
     elif i.strip() and i in strscaps: 
      data.append(strscaps[(strscaps.index(i) + shift) % 26]) 
     else: 
      data.append(i) 
    output = ''.join(data) 
    return output 
def decrypt(inp): 
    data = [] 
    for i in inp: 
     if i.strip() and i in strssmall: 
      data.append(strssmall[(strssmall.index(i) - shift) % 26]) 
     elif i.strip() and i in strscaps: 
      data.append(strscaps[(strscaps.index(i) - shift) % 26]) 
     else: 
      data.append(i) 
    output = ''.join(data) 
    return output 

choice = input('Do you want to decrypt or encrypt a sentence? (d/e) :') 
question = 'Give me a sentence to %s\n' 

if choice == 'd': 
    encrypted_str = input(question % 'decrypt') 
    decrypted_str = decrypt(encrypted_str) 
    print(decrypted_str) 

elif choice == 'e': 
    plaintext = input(question % 'encrypt') 
    encrypted_str = encrypt(plaintext) 
    print(encrypted_str) 

else: 
    print('That is not a valid option') 

此外,您還可以參觀link

+0

我已更正它,但與您的代碼(或您複製的代碼)略有不同。首先,我遺漏了'i.strip和'。其次,我將處理後的字符存儲在一個字符串中,而不是列表中。這些/是否會有任何問題? – madcode

+0

我沒有看到有所作爲,但驗證了您得到的結果。 i.strip()用於處理新的行和空格字符。 –

0

作爲一個建議, 看看蟒蛇的字符/ ORD功能(https://docs.python.org/2/library/functions.html#chrhttps://docs.python.org/2/library/functions.html#ord),基本上只接受一個字符,並將其轉換爲數字,在這裏你可以添加一個常數,轉換回一個使用ord的字符串。

然後,您可以循環訪問yourt代碼,使用for循環或列表理解。

我沒有顯示代碼,因爲你想學,但我可以很容易地提供一些幫助:)

0

一個簡單,易於理解做的方式這是通過使用dictionaries映射舊字符到新的,並且將它們joining它們之前單獨地變換每個字符一起,這樣例如:

encrypt_table = {'a': 'b', 'b': 'c', 'c': 'd', 'd': 'e', 'e': 'f', 'f': 'g', 
       'g': 'h', 'h': 'i', 'i': 'j', 'j': 'k', 'k': 'l', 'l': 'm', 
       'm': 'n', 'n': 'o', 'o': 'p', 'p': 'q', 'q': 'r', 'r': 's', 
       's': 't', 't': 'u', 'u': 'v', 'v': 'w', 'w': 'x', 'x': 'y', 
       'y': 'z', 'z': 'a'} 
encrypt_table.update([(k.upper(),v.upper()) for k,v in encrypt_table.items()]) # to include upper case letters 

decrypt_table = {v:k for k,v in encrypt_table.items() } # is just the inverse of the other table 

def encrypt(string): 
    return "".join(encrypt_table.get(c,c) for c in string) 

def decrypt_table(string): 
    return "".join(decrypt_table.get(c,c) for c in string) 

與密碼函數如顯示爲簡單的上方,使用字典的get方法來轉換的字符或不改變它,如果不轉型表

現在你得到正確的結果

>>> encrypt("zaaz") 
'abba' 
>>>