The module show a way to simple data encryption. Characters are mapped to a key via replacement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | '''Support module for translating strings.
This module provides several functions
for definitions, keys, and transforms.'''
__version__ = 1.3
################################################################################
import random
def definition(name=None):
'Returns a valid definition.'
random.seed(name)
definition, list_one, list_two = str(), range(256), range(256)
for index in range(256):
index_one, index_two = random.randrange(256 - index), random.randrange(256 - index)
definition += chr(list_one[index_one]) + chr(list_two[index_two])
del list_one[index_one], list_two[index_two]
return definition
def key(definition, select):
'Returns a valid key.'
key = range(256)
for index in range(256):
key[ord(definition[index * 2 + int(bool(select))])] = definition[index * 2 + int(not bool(select))]
return ''.join(key)
def transform(key, string):
'Returns a valid transformation.'
return string.translate(key)
################################################################################
if __name__ == '__main__':
import sys
print 'Content-Type: text/plain'
print
print file(sys.argv[0]).read()
|
Though simple, this is a good method for encrypting data if the data is compressed before or after the encryption process. The functions provided by this recipe are meant to be used in a larger encryption scheme. Definitions are specially constructed strings that come from the defintion function (which can produce "named" definitions). Keys can be extracted from the definitions by using the key function. The select argument should either be True or False. Data encrypted with "True" of a definition can be decrypted with the "False" of a definition and visa-versa.