在Python中通过混乱加密和解密

时间:2015-03-17 00:45:15

标签: python random indexing random-seed

我正在编写一个加密(并最终将解密)给定字符串的程序。

加密函数有两个参数:字符串和种子值。

这是我到目前为止所做的:

def random_encipher(string,seed):
   random.seed(seed)
   alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

   #shuffle alphabet
   random.shuffle(alphabet)

   #assign index to each letter in alphabet
   for letter in alphabet:
       letter = ord(letter)-97

总结一下,基本上我正在改变字母表并为每个字母分配一个数字值(“a”= 0,“b”= 1,...)

以下是我需要帮助的地方:

我需要字符串[0] 打印为 alphabet [0] (这是随机字母,因此使用当前种子值,字母[0] = “E”)。

但是对于字符串的每个字母,不仅仅是零索引。

3 个答案:

答案 0 :(得分:1)

也许是这样的?

>>> import random
>>> def random_encipher(string,seed):
   random.seed(seed)
   alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

   #shuffle alphabet
   random.shuffle(alphabet)

   ciphers = []
   #assign index to each letter in alphabet
   for letter in string:
       index = ord(letter)-97
       cipher = alphabet[index]
       ciphers.append(cipher)
   return "".join(ciphers)

>>> random_encipher("foobar", 3)
'fwwqgc'

使用列表的要点是,由于字符串是不可变的,附加到字符串需要复制字符串,这是昂贵的。附加到列表并在最后合并元素是更好的选择(或使用StringIO)。

答案 1 :(得分:1)

你实际做的是在字母之间创建一个映射。

幸运的是,python实际上有一个易于使用的地图对象(dict):

所以,创建一个随机映射:

keys = string.ascii_lowercase
# this shuffles the string
values = ''.join(
    random.sample(string.ascii_lowercase, len(string.ascii_lowercase))
)
mapping = dict(zip(keys, values))

我们希望这种映射的反向用于解密,因此我们使用dict comprehension

reverse_mapping = {v: k for k, v in mapping.iteritems()}

现在只需要使用加密和解密映射:

def translate(s, mapping, missing=' '):
    return ''.join([mapping.get(c, missing) for c in s])

并使用翻译功能:

encrypted = translate("my string", mapping)
print encrypted
# verify deciphering works
decrypted = translate(encrypted, reverse_mapping)
print decrypted

整个事情在一起:

#!/usr/bin/env python

import string
import random
# ... set 'seed', or factor to a function
random.seed(seed)
keys = string.ascii_lowercase
values = ''.join(
    random.sample(string.ascii_lowercase, len(string.ascii_lowercase))
)

mapping = dict(zip(keys, values))
reverse_mapping = {v: k for k, v in mapping.iteritems()}

def translate(s, mapping, missing=' '):
    return ''.join([mapping.get(c, missing) for c in s])

encrypted = translate("my string", mapping)
print encrypted
# verify deciphering works
decrypted = translate(encrypted, reverse_mapping)
print decrypted

输出:

up jsqlao
my string

答案 2 :(得分:0)

以下是我最终使用的内容:

def random_encipher(s,n):

     random.seed(n)

     alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

     #shuffle alphabet
     random.shuffle(alphabet)
     print alphabet

     #build empty list
     list = []
     string = ""

     #build enciphered string
     for letter in s: 
         if letter.isalpha(): #if character is a letter, print as corresponding index in shuffled alphabet
             list.append(alphabet[ord(letter)-97],)
             word = string.join(list)
         else: #includes punctuation, spaces, etc.
             list.append(letter),
             word = string.join(list)

     print word

random_encipher(s,n)