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

The main purpose of these functions are to allow encoding a string from base 256 to a special base 255. The function view the strings as numbers and simply change what base they are written in. This is not recommended for very long strings; otherwise, the encoding and deconding process can take a very long time to complete.

Python, 89 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
'''code_module.py

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

This is a level 1 module.'''

#==================================
# Level 1 Functions: String To Code
#==================================

def string_to_number(string):
    '''string_to_number(string)

    Create a starting number.
    Tranlate the string into the number.
    Return the number.'''
    number = 1
    for character in string:
        number *= 256
        number += ord(character)
    return number

def number_to_code(number):
    '''number_to_code(long)

    Create a starting string.
    Translate the number into the code.
    Return the string.'''
    code = str()
    while number != 0:
        code = chr(number % 255 + 1) + code
        number /= 255
    return code

#==================================
# Level 1 Functions: Code To String
#==================================

def code_to_number(code):
    '''code_to_number(string)

    Create a starting number.
    Tranlate the code into the number.
    Return the number.'''
    number = 0
    for character in code:
        number *= 255
        number += ord(character) - 1
    return number

def number_to_string(number):
    '''number_to_string(long)

    Create a starting string.
    Translate the number into the string.
    Return the string.'''
    string = str()
    while number > 1:
        string = chr(number % 256) + string
        number /= 256
    return string

#===============================
# Level 2 Functions: To And From
#===============================

def string_to_code(string):
    '''string_to_code(string)

    Returns a string converted to code.'''
    return number_to_code(string_to_number(string))

def code_to_string(code):
    '''code_to_string(string)

    Returns code converted to a string.'''
    return number_to_string(code_to_number(code))

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

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

If you want to invent an encryption system, maybe you can fustrate the people trying to break your scheme by throwing some of this code into the data transformation process. It would probably be pretty good as an encryption system all by itself. The string becomes unreadable; and unless the code breakers have an idea of what you are doing, they aren't going to find out what the string really says.