ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: z_crypt.py
Submitter: Stephen Chappell (other recipes)
Last Updated: 2006/07/03
Version no: 1.0
Category: Algorithms

 

1 stars 1 vote(s)


Description:

The module show a way to simple data encryption.
Characters are mapped to a key via replacement.

Source: Text Source

'''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()

Discussion:

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.



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Treat the Win32 Registry ...

5. a friendly mkdir()

6. Wrapping template engine ...

7. Assignment in expression

8. Changing return value ...

9. Implementation of sets ...

10. bag collection class




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.