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

SPICE has been in development since 2002. The first version was implemented on a graphing calculator, designed to work with base 27 numbers, and used hard coded keys. The program was ported to Java and later redesigned in Python. This implementation was ported directly from the standard code base.

PHP, 302 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
<!-- START CUSTOM CODE -->

<!-- START TEST FORM -->

<form action="SPICE.php" method="post">
 <p>Your key: <input type="text" name="key" /></p>
 <p>Your text: <input type="text" name="text" /></p>
 <p><input type="submit" /></p>
</form>

<!-- END TEST FORM -->

<!-- START SPICE CODE -->

<?php

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

# =======
# BLOCK 1
# =======

function crypt_major()
{
	$all = range("\x00", "\xFF");
	shuffle($all);
	$major_key = implode("", $all);
	return $major_key;
}

function crypt_minor()
{
	$sample = array();
	do
	{
		array_push($sample, 0, 1, 2, 3);
	} while (count($sample) != 256);
	shuffle($sample);
	$list = array();
	for ($index = 0; $index < 64; $index++)
	{
		$b12 = $sample[$index * 4] << 6;
		$b34 = $sample[$index * 4 + 1] << 4;
		$b56 = $sample[$index * 4 + 2] << 2;
		$b78 = $sample[$index * 4 + 3];
		array_push($list, $b12 + $b34 + $b56 + $b78);
	}
	$minor_key = implode("", array_map(chr, $list));
	return $minor_key;
}

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

# =======
# BLOCK 2
# =======

function named_major($name)
{
	srand(crc32($name));
	return crypt_major();
}

function named_minor($name)
{
	srand(crc32($name));
	return crypt_minor();
}

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

# =======
# BLOCK 3
# =======

function _check_major($key)
{
	if (is_string($key) && strlen($key) == 256)
	{
		foreach (range("\x00", "\xFF") as $char)
		{
			if (substr_count($key, $char) == 0)
			{
				return FALSE;
			}
		}
		return TRUE;
	}
	return FALSE;
}

function _check_minor($key)
{
	if (is_string($key) && strlen($key) == 64)
	{
		$indexs = array();
		foreach (array_map(ord, str_split($key)) as $byte)
		{
			foreach (range(6, 0, 2) as $shift)
			{
				array_push($indexs, ($byte >> $shift) & 3);
			}
		}
		$dict = array_count_values($indexs);
		foreach (range(0, 3) as $index)
		{
			if ($dict[$index] != 64)
			{
				return FALSE;
			}
		}
		return TRUE;
	}
	return FALSE;
}

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

# =======
# BLOCK 4
# =======

function _encode_map_1($major)
{
	return array_map(ord, str_split($major));
}

function _encode_map_2($minor)
{
	$map_2 = array(array(), array(), array(), array());
	$list = array();
	foreach (array_map(ord, str_split($minor)) as $byte)
	{
		foreach (range(6, 0, 2) as $shift)
		{
			array_push($list, ($byte >> $shift) & 3);
		}
	}
	for ($byte = 0; $byte < 256; $byte++)
	{
		array_push($map_2[$list[$byte]], chr($byte));
	}
	return $map_2;
}

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

# =======
# BLOCK 5
# =======

function _decode_map_1($minor)
{
	$map_1 = array();
	foreach (array_map(ord, str_split($minor)) as $byte)
	{
		foreach (range(6, 0, 2) as $shift)
		{
			array_push($map_1, ($byte >> $shift) & 3);
		}
	}
	return $map_1;
}

function _decode_map_2($major)
{
	$map_2 = array();
	$temp = array_map(ord, str_split($major));
	for ($byte = 0; $byte < 256; $byte++)
	{
		$map_2[$temp[$byte]] = chr($byte);
	}
	return $map_2;
}

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

# =======
# BLOCK 6
# =======

function _encode($string, $map_1, $map_2)
{
	$cache = "";
	foreach (str_split($string) as $char)
	{
		$byte = $map_1[ord($char)];
		foreach (range(6, 0, 2) as $shift)
		{
			$cache .= $map_2[($byte >> $shift) & 3][mt_rand(0, 63)];
		}
	}
	return $cache;
}

function _decode($string, $map_1, $map_2)
{
	$cache = "";
	$temp = str_split($string);
	for ($iter = 0; $iter < strlen($string) / 4; $iter++)
	{
		$b12 = $map_1[ord($temp[$iter * 4])] << 6;
		$b34 = $map_1[ord($temp[$iter * 4 + 1])] << 4;
		$b56 = $map_1[ord($temp[$iter * 4 + 2])] << 2;
		$b78 = $map_1[ord($temp[$iter * 4 + 3])];
		$cache .= $map_2[$b12 + $b34 + $b56 + $b78];
	}
	return $cache;
}

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

# =======
# BLOCK 7
# =======

function encode_string($string, $major, $minor)
{
	if (is_string($string))
	{
		if (_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);
		}
	}
	return FALSE;
}

function decode_string($string, $major, $minor)
{
	if (is_string($string) && strlen($string) % 4 == 0)
	{
		if (_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);
		}
	}
	return FALSE;
}

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

?>

<!-- END SPICE CODE -->

<!-- START TEST CODE -->

<?php

# BLOCK 1 TEST
/*$test11 = crypt_major();
$test12 = crypt_minor();
echo "<br>" . bin2hex($test11) . "<br>";
echo "<br>" . bin2hex($test12) . "<br>";*/

# BLOCK 2 TEST
/*$test21 = named_major($_POST['key']);
$test22 = named_minor($_POST['key']);
echo "<br>" . bin2hex($test21) . "<br>";
echo "<br>" . bin2hex($test22) . "<br>";*/

# BLOCK 3 TEST
/*$test31 = named_major($_POST['key']);
$test32 = named_minor($_POST['key']);
echo "<br>" . _check_major($test31) . "<br>";
echo "<br>" . _check_minor($test32) . "<br>";*/

# BLOCK 4 TEST
/*$test41 = named_major($_POST['key']);
$test42 = named_minor($_POST['key']);
echo "<br>" . print_r(_encode_map_1($test41), TRUE) . "<br>";
echo "<br>" . print_r(_encode_map_2($test42), TRUE) . "<br>";*/

# BLOCK 5 TEST
/*$test51 = named_major($_POST['key']);
$test52 = named_minor($_POST['key']);
echo "<br>" . print_r(_decode_map_1($test52), TRUE) . "<br>";
echo "<br>" . print_r(_decode_map_2($test51), TRUE) . "<br>";*/

# BLOCK 6 & 7 TEST
/*$test671 = named_major($_POST['key']);
$test672 = named_minor($_POST['key']);
echo "<br>" . bin2hex($test671) . "<br>";
echo "<br>" . bin2hex($test672) . "<br>";
if (strlen($_POST['text']) != 0)
{
	$test673 = encode_string($_POST['text'], $test671, $test672);
	$test674 = decode_string($test673, $test671, $test672);
	echo "<br>" . bin2hex($test673) . "<br>";
	echo "<br>" . $test674 . "<br>";
}*/

?>

<!-- END TEST CODE -->

<!-- END CUSTOM CODE -->

2 comments

Stephen Chappell (author) 13 years, 7 months ago  # | flag
Description of Algorithm
========================

1. The foundation behind the algorithm was first conceived back in 2002.

2. SPICE was created by Stephen Paul Chappell (myself).

3. SPICE is an acronym for Stephen's Power-Inspired, Computerized
Encryption.

4. A normal replacement cipher can be defeated by using statistical
analysis to figure out the key. SPICE was designed to foil attacks to
the cipher text from statistical analysis.

5. The output from the base 27 algorithm led to a 3 times increase in
the data. The output from the current SPICE implementation is designed
for base 256 data and leads to a 4 times increase in the data. The
implementation that you have encodes the binary data in hex and
therefore leads to 8 times the original data.

Description of API
==================

---------------------
str crypt_major(void)
---------------------
Arguments: None
Returns: Major Key (binary string)
Reason: Call this to create a randomly generated major key.

---------------------
str crypt_minor(void)
---------------------
Arguments: None
Returns: Minor Key (binary string)
Reason: Call this to create a randomly generated minor key.

--------------------------
str named_major(str $name)
--------------------------
Arguments: Name of the key (string)
          -- may be binary
          -- may be different from a "named" minor key
Returns: Major Key (binary string)
Reason: Call this to create a "named" major key.

--------------------------
str named_minor(str $name)
--------------------------
Arguments: Name of the key (string)
          -- may be binary
          -- may be different from a "named" major key
Returns: Minor Key (binary string)
Reason: Call this to create a "named" minor key.

----------------------------------------------------
bool _check_major(str $key)
bool _check_minor(str $key)
array _encode_map_1(str $major)
array _enocde_map_2(str $minor)
array _decode_map_1(str $minor)
array _decode_map_2(str $major)
str _encode(str $string, array $map_1, array $map_2)
str _decode(str $string, array $map_1, array $map_2)
----------------------------------------------------
Reason: These are private functions.
       Use "encode_string" and "decode_string" instead.

--------------------------------------------------------------
str_or_bool encode_string(str $string, str $major, str $minor)
--------------------------------------------------------------
Arguments: String to encrypt
          Major key
          Minor key
Returns: if error: FALSE
        else: cipher text (binary string)
Reason: This is the public encryption function.

(comment continued...)

Stephen Chappell (author) 13 years, 7 months ago  # | flag

(...continued from previous comment)

--------------------------------------------------------------
str_or_bool decode_string(str $string, str $major, str $minor)
--------------------------------------------------------------
Arguments: String to decrypt
          Major key
          Minor key
Returns: if error: FALSE
        else: plain text (string -- may be binary)
Reason: This is the public decryption function.
Created by Stephen Chappell on Wed, 30 Apr 2008 (MIT)
PHP recipes (51)
Stephen Chappell's recipes (233)

Required Modules

  • (none specified)

Other Information and Tasks