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

This program has been documented. It is designed to encode and decode files (for encryption purposes). It uses a simple encryption method.

Python, 246 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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from pickle import Pickler, Unpickler   # for saving and loading keys
from random import randint, seed        # for generating keys
from time import time                   # for randomizing keys

def main():
    '''Executes program.

    Shows welcome and menu.
    Gets selection.
    Executes correct part of the program.'''
    show_heading('Welcome to CRYPT')
    print
    exit = False
    while not exit:
        answer = menu()
        if answer is 1:
            create_key()
        elif answer is 2:
            encode_file()
        elif answer is 3:
            decode_file()
        elif answer is 4:
            exit = True
        else:
            raise bug

def show_heading(heading):
    '''Displays headings.

    Prints a heading.
    Underlines the heading.'''
    print heading
    underline = ''
    for line in range(len(heading)):
        underline += '-'
    print underline

def menu():
    '''Handles menu.

    Shows the menu.
    Get and returns a selection.'''
    show_menu()
    return get_answer()

def show_menu():
    '''Displays menu.

    Shows the heading.
    Prints out all selections from the menu.'''
    show_heading('MENU')
    print '(1) Create Key'
    print '(2) Encode File'
    print '(3) Decode File'
    print '(4) Exit Program'
    print

def get_answer():
    '''Receives selection.

    Gets an answer.
    Handles errors.
    Returns answer.'''
    while True:
        try:
            answer = int(raw_input('Please make a selection: '))
            if answer is 1 or answer is 2 or answer is 3 or answer is 4:
                print
                return answer
        except:
            pass
        print 'You must enter 1, 2, 3, or 4 as a selection.'

def create_key():
    '''Generates a key.

    Shows the heading.
    Gets a destination file.
    Creates a key.
    Saves the key in the file.
    Flush the buffer.
    Closes the file.
    Prints confirmation.'''
    show_heading('CREATE KEY')
    key_file = get_destination('What will be the name of the key file?')
    key_data = create_key_data()
    Pickler(key_file).dump(key_data)
    key_file.flush()
    key_file.close()
    print 'The key has been created at the location specified.'
    print

def get_destination(prompt):
    '''Gets destination file.

    Prompts for filename.
    Handles errors.
    Returns file.'''
    while True:
        try:
            key = file(raw_input(prompt + ' '), 'wb')
            return key
        except:
            print 'Please enter a different filename.'

def create_key_data():
    '''Creates a key.

    Generates to lists.
    Creates an area for the key.
    Randomizes the random system.
    Loops 265 times doing the following:
        Creates two different indexs valid for list_one and list_two.
        Creates a tuple of two different and unique numbers.
        Deletes the two number from the two lists.
    Returns the key.'''
    list_one = range(256)
    list_two = range(256)
    key = []
    seed(time())
    for index in range(256):
        index_one = randint(0, 255 - index)
        index_two = randint(0, 255 - index)
        key.append((list_one[index_one], list_two[index_two]))
        del list_one[index_one], list_two[index_two]
    return key

def encode_file():
    '''Executes decoding subsection.

    Shows the heading of this area.
    Gets the source file for encoding.
    Gets the destination file.
    Handles errors.
    Gets a key.
    Executes the main encoding system.
    Prints a confirmation.'''
    show_heading('ENCODE FILE')
    source = get_source('What is the name of the source file?')
    destination = get_destination('What will be the name of the destination file?')
    bad_key = True
    while bad_key:
        try:
            key = get_key()
            work_encode(source, destination, key)
            bad_key = False
        except:
            print 'Please enter a different filename.'
    print 'The encoded file has been created at the location specified.'
    print

def get_source(prompt):
    '''Gets a source file.

    Gets the file.
    Handles errors.
    Returns the file.'''
    while True:
        try:
            source = file(raw_input(prompt + ' '), 'rb')
            return source
        except:
            pass
        print 'Please enter a different filename.'

def get_key():
    '''Gets key data.

    Gets the source of the key.
    Loads the data of the key.
    Closes the key file.
    Returns the key.'''
    key_file = get_source('What is the name of the key file?')
    key = Unpickler(key_file).load()
    key_file.close()
    return key

def work_encode(source, destination, key):
    '''Encodes and saves a file.

    Creates buffers for a new file and a "quick key."
    Generates the "quick key."
    Encodes the old file onto the new file buffer.
    Writes the buffer to the destination.
    Closes the open files.'''
    new_file = ''
    encode_key = range(256)
    for index in range(256):
        encode_key[key[index][0]] = key[index][1]
    for line in source:
        for byte in line:
            new_file += chr(encode_key[ord(byte)])
    destination.write(new_file)
    source.close()
    destination.flush()
    destination.close()

def decode_file():
    '''Decodes a file.

    Shows the heading.
    Gets the source file.
    Gets the destination file.
    Handles errors.
    Gets a key.
    Does the decoding.
    Prints a confirmation.'''
    show_heading('DECODE FILE')
    source = get_source('What is the name of the source file?')
    destination = get_destination('What will be the name of the destination file?')
    bad_key = True
    while bad_key:
        try:
            key = get_key()
            work_decode(source, destination, key)
            bad_key = False
        except:
            print 'Please enter a different filename.'
    print 'The decoded file has been created at the location specified.'
    print

def work_decode(source, destination, key):
    '''Does decoding.

    Creates buffers.
    Generates "quick key."
    Decodes source to buffer.
    Writes buffer to destination.
    Closes files.'''
    new_file = ''
    decode_key = range(256)
    for index in range(256):
        decode_key[key[index][1]] = key[index][0]
    for line in source:
        for byte in line:
            new_file += chr(decode_key[ord(byte)])
    destination.write(new_file)
    source.close()
    destination.flush()
    destination.close()

class bug:                              # for custom exceptions
    pass

if __name__ == '__main__':              # for executing if needed
    main()

This program will help make your files secure. It is suggested that compressed file be used with this program to prevent the file contents from being easily accessed.