Encrypting A String

The main purpose of these functions are to encrypt and decrypt a string (or change the string from one "language" into another). I'm guessing that Python already has something like this, but this is my implementation of the process. The code is simple, but it provides a simple solution to a well-known encryption scheme. As a recommendation, this may be better when used in conjunction with an encoding system (such as the base 256 to base 255 encoded that I submitted).


Why should you use this code? Good question -- tell me when you have an answer. :) On the more serious side, you have access to a simple encryption system that you can edit to comply with your purposes. Note: if you want to use the "named keys" feature, I suggest combining it with the "string_to_number()" function in some other module that I submitted ...

'''translator_module.py

The purpose of this module
is to provide functions
for translating strings.

This is a level 1 module.'''

#===================================
# Level 1 Functions: Core Algorithms
#===================================

def get_dictionary(name=None):
    '''get_dictionary(object)

    Import needed functions.
    Setup the random system.
    Create needed variables.
    Create the dictionary.
    Return the dictionary.'''
    from random import randint, seed
    seed(name)
    dictionary, list_one, list_two = list(), range(256), range(256)
    for index in range(256):
        index_one, index_two = randint(0, 255 - index), randint(0, 255 - index)
        dictionary.append((list_one[index_one], list_two[index_two]))
        del list_one[index_one], list_two[index_two]
    return dictionary

def get_key(dictionary, bit):
    '''get_key(list, bool)

    Create a key.
    Setup its values.
    Return the key.'''
    key = range(256)
    for item in dictionary:
        key[item[bit]] = item[not bit]
    return key

def translate(old_string, key):
    '''translate(string, list)

    Create a new string.
    Translate the old string into the new string.
    Return the new string.'''
    new_string = str()
    for character in old_string:
        new_string += chr(key[ord(character)])
    return new_string

#=======================================
# Level 2 Functions: Helpful Definitions
#=======================================

def encode(dictionary):
    '''encode(list)

    Return a key for encoding.'''
    return get_key(dictionary, True)

def decode(dictionary):
    '''decode(list)

    Return a key for decoding.'''
    return get_key(dictionary, False)

#==============================
# Level 2 Functions: Named Keys
#==============================

def get_encode(name):
    '''get_encode(object)

    Assert that the name is not None.
    Return a named encoding key.'''
    assert name is not None
    return get_key(get_dictionary(name), True)

def get_decode(name):
    '''get_decode(object)

    Assert that the name is not None.
    Return a named decoding key.'''
    assert name is not None
    return get_key(get_dictionary(name), False)

#================
# CGI: Print File
#================

if __name__ == '__main__':
    from sys import argv
    print 'Content-type: text/plain'
    print
    print file(argv[0]).read()

code.activestate.com ↗