2015-11-19 101 views
0

我想做一个加密明文消息的加密函数,但问题是,如果我输入一个关键字太大,过去'Z'然后它进入更大的统一码值。Python简单的加密程序:如何循环Z回到A

我的代码:

def encrypt(var1,var2): 
    var3 = "" 
    for i in range(0, len(var1)): 
     if ord(var1[i])>64 and ord(var1[i])<90: 
      var3=var3+chr((ord(var1[i])+var2)) 
     elif ord(var1[i])+var2>90: 
      ??? 
     else: 
      continue 
    return(var3) 

我如何得到它的循环 'Z' 回 'A'。我想我必须做出这样的陈述,但我不知道该如何陈述。

elif ord(var1[i])+var2>90: 
    ??? 
+0

我想你需要提供一些示例输入。 –

+0

var2 = 30,var1 = zzzz使它^^^^ –

回答

2

这是我的!我使用模运算符来包裹每26个数字(a-z之间的字母数)。我还分别处理小写字母的上部。

def encrypt(data, shift): 
    result = '' 
    for c in data: 
     c_num = ord(c) 

     # is the letter lower case a - z? 
     if (c_num >= ord('a')) and (c_num <= ord('z')): 
      # get the letter number from 0 - 26 
      c_num = c_num - ord('a') 
      # shift the number 
      c_num += shift 
      # wrap the number every 26 numbers 
      c_num = c_num % 26 
      # now increase a by the new amount 
      c_num += ord('a') 
      result += chr(c_num) 

     # is the letter upper case A - Z? 
     elif (c_num >= ord('A')) and (c_num <= ord('Z')): 
      # get the letter number from 0 - 26 
      c_num = c_num - ord('A') 
      # shift the number 
      c_num += shift 
      # wrap the number every 26 numbers 
      c_num = c_num % 26 
      # now increase a by the new amount 
      c_num += ord('A') 
      result += chr(c_num) 

    return result 

encrypt('aAbB', 2) 
'cCdD' 

encrypt('afZz', 2) 
'chBb' 

这里是代码高尔夫版本使用列表理解只是为了好玩!

def encrypt(data, shift): 
    return ''.join([chr(((ord(c) - ord('a') + shift) % 26) + ord('a')) if ord(c) in range(ord('a'), ord('z')+1) else chr(((ord(c) - ord('A') + shift) % 26) + ord('A')) for c in data]) 
0

一个直接的方法是检查你是否已经过去了超越Z,并且在这种情况下修改字符:

... 
if var1[i] >= 'A' and var1[i] <= 'Z': 
    translated_char = chr(ord(var1[i])+var2) 
    if translated_char > 'Z': 
     # If the resulting character is beyond Z, 
     # we go 26 characters back 
     translated_char = chr(ord(translated_char)-26) 
    # Append the translated character to the output string 
    var3 += translated_char 
... 

你可能要考虑更具描述性的变量名 - 你如果你在两个月后重新访问你的代码,会感谢你自己:-)

0

我会推荐使用模数运算符来做你想要的。在python中是%字符。在模数学中。 X%Y告诉我们X/Y的剩余部分是什么。例如。 27%26是1.使用这个你可以得到你想要的包装。这里是一个代码示例位,如果我们喂字符“A”进入我们的加密算法的13关键,我们得到“N”,如到现在为止加密单个字符

def encrypt_character(valToEncrypt, keyVal): 

     # Update the character to be our standard Alphabet mapping 
     # A -> 0; B->1 ... Z -> 25 
     x = ord(valToEncrypt) - ord('A') 

     # Perform the Encryption 
     retVal = (x + keyVal) % 26 

     # Translate back to the standard ASCII mapping of the character 
     # for display in python and translate it back into a string 
     retVal = chr(retVal + ord('A')) 

     return retVal 
    # end encrypt_character 

>>> encrypt_character("A", 13) 
'N' 

解密算法是非常相似的,除非你做减法而不是addtion

def decrypt_character(valToDecrypt, keyVal): 

     # Update the character to be our standard Alphabet mapping 
     # A -> 0; B->1 ... Z -> 25 
     x = ord(valToDecrypt) - ord('A') 

     retVal = (x - keyVal) % 26 

     # Translate back to the standard ASCII mapping of the character 
     # for display in python and translate it back into a string 
     retVal = chr(retVal + ord('A')) 

     return retVal 

要加密,你可以使用下面的函数的字符串: 从重新进口苏b def encrypt_message(消息,密钥): #将消息文本转换为纯文本,其中包含所有空格,并删除 #标点符号。 明文=子(R '[^ AZ]', '',message.upper()) 密文= “”

 charIndex = 0 

    # Encrypt the message 1 character at a time 
    while charIndex < len(plainText): 
     cipherText += \ 
      encrypt_character(plainText[charIndex], key) 
     charIndex += 1 
    return cipherText 

此功能可被称为:

>>> encrypt_message("HELLO World!", key=23) 
'EBIILTLOIA' 

解密函数与加密函数非常相似,除了调用解密实用程序而不是加密实用程序。

+0

好吧,但我怎么会使整个字符串,而不是一个字符? –

+0

通常,您需要遵循的过程是去除所有标点符号,空格,并将所有内容都转换为上(或下)情况。我会更新这个例子。 –