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

This module provides access to a standardized implementation of SPICE (Stephen's Power-Inspired, Computerized Encryption).

This code has just been revised and rewritten. As such it may still contain bugs that were not found after testing the code.

Python, 363 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
'''Module that implements SPICE.

This module provides access to a standardized implementation
of SPICE (Stephen's Power-Inspired, Computerized Encryption).'''

################################################################################

__version__ = '$Revision: 0 $'
__date__ = 'April 19, 2008'
__author__ = 'Stephen "Zero" Chappell <my.bios@gmail.com>'
__credits__ = '''\
T. Parker, for testing code that led to this module.
A. Baddeley, for contributing to the random module.
R. Hettinger, for adding support for two core generators.'''

################################################################################

import random as _random
import sys as _sys

################################################################################

def crypt_major():
    'Create a new Major Key.'
    return ''.join(map(chr, _crypt.sample(xrange(256), 256)))

def crypt_minor():
    'Create a new Minor Key.'
    sample = _crypt.sample(range(4) * 64, 256)
    array = []
    for index in xrange(64):
        bits_12 = sample[index * 4] << 6
        bits_34 = sample[index * 4 + 1] << 4
        bits_56 = sample[index * 4 + 2] << 2
        bits_78 = sample[index * 4 + 3]
        array.append(bits_12 + bits_34 + bits_56 + bits_78)
    return ''.join(map(chr, array))

################################################################################

def named_major(name):
    'Create a named Major Key.'
    _namer.seed(name)
    return ''.join(map(chr, _namer.sample(xrange(256), 256)))

def named_minor(name):
    'Create a named Minor Key.'
    _namer.seed(name)
    sample = _namer.sample(range(4) * 64, 256)
    array = []
    for index in xrange(64):
        bits_12 = sample[index * 4] << 6
        bits_34 = sample[index * 4 + 1] << 4
        bits_56 = sample[index * 4 + 2] << 2
        bits_78 = sample[index * 4 + 3]
        array.append(bits_12 + bits_34 + bits_56 + bits_78)
    return ''.join(map(chr, array))

################################################################################
    
def encode_string(string, major, minor):
    'Return an encrypted string.'
    assert isinstance(string, str)
    _check_major(major)
    _check_minor(minor)
    map_1 = _encode_map_1(major)
    map_2 = _encode_map_2(minor)
    return _encode(string, map_1, map_2)

def decode_string(string, major, minor):
    'Return a decrypted string.'
    assert isinstance(string, str) and len(string) % 4 == 0
    _check_major(major)
    _check_minor(minor)
    map_1 = _decode_map_1(minor)
    map_2 = _decode_map_2(major)
    return _decode(string, map_1, map_2)

################################################################################

def encode_file(source, destination, major, minor):
    'Encrypt a file from source to destination.'
    _check_major(major)
    _check_minor(minor)
    map_1 = _encode_map_1(major)
    map_2 = _encode_map_2(minor)
    string = source.read(2 ** 20 / 5)
    while string:
        destination.write(_encode(string, map_1, map_2))
        string = source.read(2 ** 20 / 5)

def decode_file(source, destination, major, minor):
    'Decrypt a file from source to destination.'
    _check_major(major)
    _check_minor(minor)
    map_1 = _decode_map_1(minor)
    map_2 = _decode_map_2(major)
    string = source.read(2 ** 20 / 5 * 4)
    while string:
        tail_len = len(string) % 4
        if tail_len == 0:
            destination.write(_decode(string, map_1, map_2))
            string = source.read(2 ** 20 / 5 * 4)
        else:
            destination.write(_decode(string[:-tail_len], map_1, map_2))
            return string[-tail_len:]
    return ''

################################################################################

class File_Crypt:

    'File_Crypt(major, minor, name, mode) -> File_Crypt'
    
    def __init__(self,  major, minor, name, mode):
        'Initialize the File_Crypt object.'
        _check_major(major)
        _check_minor(minor)
        self.__em1 = _encode_map_1(major)
        self.__em2 = _encode_map_2(minor)
        self.__dm1 = _decode_map_1(minor)
        self.__dm2 = _decode_map_2(major)
        assert len(mode) == 1 and mode in 'raw'
        self.__file = open(name, mode + 'b', 0)
        self.tail = ''

    def read(self, size=-1):
        'Read and decrypt from file.'
        string = self.__file.read(size * 4)
        tail_len = len(string) % 4
        if tail_len:
            self.tail = string[-tail_len:]
            return _decode(string[:-tail_len], self.__dm1, self.__dm2)
        else:
            return _decode(string, self.__dm1, self.__dm2)

    def write(self, string):
        'Encrypt and write to file.'
        self.__file.write(_encode(string, self.__em1, self.__em2))

    def seek(self, offset, whence=0):
        'Seek to virtual positon in file.'
        self.__file.seek(offset * 4, whence)
        offset = self.__file.tell() / 4
        self.__file.seek(offset * 4)

    def tell(self):
        'Return the virtual position in file.'
        return self.__file.tell() / 4

    def close(self):
        'Close the File_Crypt object.'
        self.__file.close()

################################################################################

class Socket_Crypt:

    'Socket_Crypt(major, minor, socket) -> Socket_Crypt'

    def __init__(self, major, minor, socket):
        'Initialize the Socket_Crypt object.'
        _check_major(major)
        _check_minor(minor)
        self.__em1 = _encode_map_1(major)
        self.__em2 = _encode_map_2(minor)
        self.__dm1 = _decode_map_1(minor)
        self.__dm2 = _decode_map_2(major)
        self.__major = major
        self.__minor = minor
        self.__socket = socket
        self.__tail = ''
        self.__tails = {}

    def accept(self):
        'Return a new Socket_Crypt and address.'
        conn, address = self.__socket.accept()
        return Socket_Crypt(self.__major, self.__minor, conn), address

    def recv(self, size, flags=0):
        'Receive and decrypt off socket.'
        string = self.__tail + self.__socket.recv(size * 4, flags)
        tail_len = len(string) % 4
        if tail_len:
            self.__tail = string[-tail_len:]
            return _decode(string[:-tail_len], self.__dm1, self.__dm2)
        else:
            self.__tail = ''
            return _decode(string, self.__dm1, self.__dm2)

    def recvfrom(self, size, flags=0):
        'Receive datagram and decrypt off socket.'
        string, address = self.__socket.recvfrom(size * 4, flags)
        string = self.__tails.get(address, '') + string
        tail_len = len(string) % 4
        if tail_len:
            self.__tails[address] = string[-tail_len:]
            string = _decode(string[:-tail_len], self.__dm1, self.__dm2)
            return string, address
        else:
            if address in self.__tails:
                del self.__tails[address]
            string = _decode(string, self.__dm1, self.__dm2)
            return string, address

    def send(self, string, flags=0):
        'Encrypt and send on socket.'
        string = _encode(string, self.__em1, self.__em2)
        sent = self.__socket.send(string, flags)
        offset = sent % 4
        if offset:
            string = string[sent:][:4-offset]
            sent += len(string)
            while string:
                string = string[self.__socket.send(string, flags):]
        return sent / 4

    def sendall(self, string, flags=0):
        'Encrypt and send all on socket.'
        string = _encode(string, self.__em1, self.__em2)
        return self.__socket.sendall(string, flags)

    def sendto(self, string, address, flags=0):
        'Encrypt and send datagram on socket.'
        string = _encode(string, self.__em1, self.__em2)
        sent = self.__socket.sendto(string, flags, address)
        offset = sent % 4
        if offset:
            string = string[sent:][:4-offset]
            sent += len(string)
            while string:
                string = string[self.socket.sentto(string, flags, address):]
        return sent / 4

    def makefile(self, mode='r', bufsize=-1):
        'Return a file-like object.'
        return self

    def read(self, size=-1):
        'Read and decrypt from socket.'
        if size < 0:
            cache = ''
            while True:
                temp = self.recv(2 ** 10)
                if temp:
                    cache += temp
                else:
                    return cache
        else:
            return self.recv(size)

    def readline(self, size=-1):
        'Dummy attribute for cPickle.'
        raise NotImplementedError

    def write(self, string):
        'Encrypt and write to socket.'
        self.sendall(string)

################################################################################

class String_Crypt:

    'String_Crypt(major, minor) -> String_Crypt'

    def __init__(self, major, minor):
        'Initialize the String_Crypt object.'
        _check_major(major)
        _check_minor(minor)
        self.__em1 = _encode_map_1(major)
        self.__em2 = _encode_map_2(minor)
        self.__dm1 = _decode_map_1(minor)
        self.__dm2 = _decode_map_2(major)

    def encode(self, string):
        'Return an encrypted string.'
        assert isinstance(string, str)
        return _encode(string, self.__em1, self.__em2)

    def decode(self, string):
        'Return a decrypted string.'
        assert isinstance(string, str) and len(string) % 4 == 0
        return _decode(string, self.__dm1, self.__dm2)

################################################################################

_crypt = _random.SystemRandom()
_namer = _random.Random()

################################################################################

def _check_major(key):
    'Private module function.'
    assert isinstance(key, str) and len(key) == 256
    for character in map(chr, xrange(256)):
        assert character in key

def _check_minor(key):
    'Private module function.'
    assert isinstance(key, str) and len(key) == 64
    indexs = []
    for byte in map(ord, key):
        for shift in xrange(6, -2, -2):
            indexs.append((byte >> shift) & 3)
    for index in xrange(4):
        assert indexs.count(index) == 64

def _encode_map_1(major):
    'Private module function.'
    return map(ord, major)

def _encode_map_2(minor):
    'Private module function.'
    map_2 = [[], [], [], []]
    array = []
    for byte in map(ord, minor):
        for shift in xrange(6, -2, -2):
            array.append((byte >> shift) & 3)
    for byte, index in enumerate(array):
        map_2[index].append(chr(byte))
    return map_2

def _decode_map_1(minor):
    'Private module function.'
    map_1 = []
    for byte in map(ord, minor):
        for shift in xrange(6, -2, -2):
            map_1.append((byte >> shift) & 3)
    return map_1

def _decode_map_2(major):
    'Private module function.'
    map_2 = [None] * 256
    for byte, index in enumerate(map(ord, major)):
        map_2[index] = chr(byte)
    return map_2

def _encode(string, map_1, map_2):
    'Private module function.'
    cache = ''
    for character in string:
        byte = map_1[ord(character)]
        for shift in xrange(6, -2, -2):
            cache += map_2[(byte >> shift) & 3][_crypt.randrange(64)]
    return cache

def _decode(string, map_1, map_2):
    'Private module function.'
    cache = ''
    iterator = iter(string)
    for byte in iterator:
        bits_12 = map_1[ord(byte)] << 6
        bits_34 = map_1[ord(iterator.next())] << 4
        bits_56 = map_1[ord(iterator.next())] << 2
        bits_78 = map_1[ord(iterator.next())]
        cache += map_2[bits_12 + bits_34 + bits_56 + bits_78]
    return cache

################################################################################

if __name__ == '__main__':
    _sys.stdout.write('Content-Type: text/plain\n\n')
    _sys.stdout.write(file(_sys.argv[0]).read())