Welcome, guest | Sign In | My Account | Store | Cart

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).

Python, 96 lines
 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'''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()

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 ...

2 comments

N L 16 years, 2 months ago  # | flag

Security. It might be good to point out that the encryption method used here isn't very secure. For the record it's a single letter substitution cipher. It's simple to implement and likewise simple to break (which I'm sure the author is aware of).

If let's say 6% of the letters in the data was an A before encryption and A get's translated to K, then 6% of the encrypted letters will be a K. Since these statistical properties are preserved it's possible to work the process backwards and recover the plain text without having the key.

Another thing - calling seed with a string (as I assume name is) will seed the random number generator with the hash of the string. Since the hash is a python int this might reduce the information contained in the string in case a long key is used. There will be only 2^32 possible keys and the encryption is easily breakable with brute-force (trying every possible key). For encryption purposes it's not a good idea to pass a string to seed (or use the built-in seed at all).

Stephen Chappell (author) 16 years, 2 months ago  # | flag

That is why it is suggested that the "string_to_number()" function is recommended for use. Yes, this is not very secure; however, this would probably be an effective encryption system if, for example, it were used on an executeable. It might also be effective on a compressed file.