@@ -4,6 +4,6 @@ |
||
| 4 | 4 | $up = -100; |
| 5 | 5 | $ut = 50; |
| 6 | 6 | for ($i = 0; $i <= 255; $i++) { |
| 7 | - $cw[chr($i)] = 600; |
|
| 7 | + $cw[chr($i)] = 600; |
|
| 8 | 8 | } |
| 9 | 9 | ?> |
@@ -115,181 +115,181 @@ |
||
| 115 | 115 | $record['offset'] = $this->read_ulong(); |
| 116 | 116 | $record['length'] = $this->read_ulong(); |
| 117 | 117 | $this->tables[$record['tag']] = $record; |
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param integer[] $x |
|
| 124 | - * @param integer[] $y |
|
| 125 | - */ |
|
| 126 | - function sub32($x, $y) { |
|
| 127 | - $xlo = $x[1]; |
|
| 128 | - $xhi = $x[0]; |
|
| 129 | - $ylo = $y[1]; |
|
| 130 | - $yhi = $y[0]; |
|
| 131 | - if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
|
| 132 | - $reslo = $xlo - $ylo; |
|
| 133 | - if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
| 134 | - $reshi = $xhi - $yhi; |
|
| 135 | - $reshi = $reshi & 0xFFFF; |
|
| 136 | - return array($reshi, $reslo); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - function calcChecksum($data) { |
|
| 140 | - if (strlen($data) % 4) { $data .= str_repeat("\0", (4 - (strlen($data) % 4))); } |
|
| 141 | - $hi = 0x0000; |
|
| 142 | - $lo = 0x0000; |
|
| 143 | - for ($i = 0; $i < strlen($data); $i += 4) { |
|
| 144 | - $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]); |
|
| 145 | - $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]); |
|
| 146 | - $hi += $lo >> 16; |
|
| 147 | - $lo = $lo & 0xFFFF; |
|
| 148 | - $hi = $hi & 0xFFFF; |
|
| 149 | - } |
|
| 150 | - return array($hi, $lo); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param string $tag |
|
| 155 | - */ |
|
| 156 | - function get_table_pos($tag) { |
|
| 157 | - $offset = $this->tables[$tag]['offset']; |
|
| 158 | - $length = $this->tables[$tag]['length']; |
|
| 159 | - return array($offset, $length); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - function seek($pos) { |
|
| 163 | - $this->_pos = $pos; |
|
| 164 | - fseek($this->fh, $this->_pos); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @param integer $delta |
|
| 169 | - */ |
|
| 170 | - function skip($delta) { |
|
| 171 | - $this->_pos = $this->_pos + $delta; |
|
| 172 | - fseek($this->fh, $this->_pos); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param string $tag |
|
| 177 | - */ |
|
| 178 | - function seek_table($tag, $offset_in_table = 0) { |
|
| 179 | - $tpos = $this->get_table_pos($tag); |
|
| 180 | - $this->_pos = $tpos[0] + $offset_in_table; |
|
| 181 | - fseek($this->fh, $this->_pos); |
|
| 182 | - return $this->_pos; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - function read_tag() { |
|
| 186 | - $this->_pos += 4; |
|
| 187 | - return fread($this->fh, 4); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - function read_short() { |
|
| 191 | - $this->_pos += 2; |
|
| 192 | - $s = fread($this->fh, 2); |
|
| 193 | - $a = (ord($s[0]) << 8) + ord($s[1]); |
|
| 194 | - if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } |
|
| 195 | - return $a; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - function unpack_short($s) { |
|
| 199 | - $a = (ord($s[0]) << 8) + ord($s[1]); |
|
| 200 | - if ($a & (1 << 15)) { |
|
| 201 | - $a = ($a - (1 << 16)); |
|
| 202 | - } |
|
| 203 | - return $a; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - function read_ushort() { |
|
| 207 | - $this->_pos += 2; |
|
| 208 | - $s = fread($this->fh, 2); |
|
| 209 | - return (ord($s[0]) << 8) + ord($s[1]); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - function read_ulong() { |
|
| 213 | - $this->_pos += 4; |
|
| 214 | - $s = fread($this->fh, 4); |
|
| 215 | - // if large uInt32 as an integer, PHP converts it to -ve |
|
| 216 | - return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - function get_ushort($pos) { |
|
| 220 | - fseek($this->fh, $pos); |
|
| 221 | - $s = fread($this->fh, 2); |
|
| 222 | - return (ord($s[0]) << 8) + ord($s[1]); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - function get_ulong($pos) { |
|
| 226 | - fseek($this->fh, $pos); |
|
| 227 | - $s = fread($this->fh, 4); |
|
| 228 | - // iF large uInt32 as an integer, PHP converts it to -ve |
|
| 229 | - return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - function pack_short($val) { |
|
| 233 | - if ($val < 0) { |
|
| 234 | - $val = abs($val); |
|
| 235 | - $val = ~$val; |
|
| 236 | - $val += 1; |
|
| 237 | - } |
|
| 238 | - return pack("n", $val); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * @param string $value |
|
| 243 | - */ |
|
| 244 | - function splice($stream, $offset, $value) { |
|
| 245 | - return substr($stream, 0, $offset).$value.substr($stream, $offset + strlen($value)); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * @param string|null $stream |
|
| 250 | - * @param integer $offset |
|
| 251 | - */ |
|
| 252 | - function _set_ushort($stream, $offset, $value) { |
|
| 253 | - $up = pack("n", $value); |
|
| 254 | - return $this->splice($stream, $offset, $up); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - function _set_short($stream, $offset, $val) { |
|
| 258 | - if ($val < 0) { |
|
| 259 | - $val = abs($val); |
|
| 260 | - $val = ~$val; |
|
| 261 | - $val += 1; |
|
| 262 | - } |
|
| 263 | - $up = pack("n", $val); |
|
| 264 | - return $this->splice($stream, $offset, $up); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - function get_chunk($pos, $length) { |
|
| 268 | - fseek($this->fh, $pos); |
|
| 269 | - if ($length < 1) { return ''; } |
|
| 270 | - return (fread($this->fh, $length)); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * @param string $tag |
|
| 275 | - */ |
|
| 276 | - function get_table($tag) { |
|
| 277 | - list($pos, $length) = $this->get_table_pos($tag); |
|
| 278 | - if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
|
| 279 | - fseek($this->fh, $pos); |
|
| 280 | - return (fread($this->fh, $length)); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * @param string $tag |
|
| 285 | - * @param null|string $data |
|
| 286 | - */ |
|
| 287 | - function add($tag, $data) { |
|
| 288 | - if ($tag == 'head') { |
|
| 289 | - $data = $this->splice($data, 8, "\0\0\0\0"); |
|
| 290 | - } |
|
| 291 | - $this->otables[$tag] = $data; |
|
| 292 | - } |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param integer[] $x |
|
| 124 | + * @param integer[] $y |
|
| 125 | + */ |
|
| 126 | + function sub32($x, $y) { |
|
| 127 | + $xlo = $x[1]; |
|
| 128 | + $xhi = $x[0]; |
|
| 129 | + $ylo = $y[1]; |
|
| 130 | + $yhi = $y[0]; |
|
| 131 | + if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
|
| 132 | + $reslo = $xlo - $ylo; |
|
| 133 | + if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
| 134 | + $reshi = $xhi - $yhi; |
|
| 135 | + $reshi = $reshi & 0xFFFF; |
|
| 136 | + return array($reshi, $reslo); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + function calcChecksum($data) { |
|
| 140 | + if (strlen($data) % 4) { $data .= str_repeat("\0", (4 - (strlen($data) % 4))); } |
|
| 141 | + $hi = 0x0000; |
|
| 142 | + $lo = 0x0000; |
|
| 143 | + for ($i = 0; $i < strlen($data); $i += 4) { |
|
| 144 | + $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]); |
|
| 145 | + $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]); |
|
| 146 | + $hi += $lo >> 16; |
|
| 147 | + $lo = $lo & 0xFFFF; |
|
| 148 | + $hi = $hi & 0xFFFF; |
|
| 149 | + } |
|
| 150 | + return array($hi, $lo); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param string $tag |
|
| 155 | + */ |
|
| 156 | + function get_table_pos($tag) { |
|
| 157 | + $offset = $this->tables[$tag]['offset']; |
|
| 158 | + $length = $this->tables[$tag]['length']; |
|
| 159 | + return array($offset, $length); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + function seek($pos) { |
|
| 163 | + $this->_pos = $pos; |
|
| 164 | + fseek($this->fh, $this->_pos); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @param integer $delta |
|
| 169 | + */ |
|
| 170 | + function skip($delta) { |
|
| 171 | + $this->_pos = $this->_pos + $delta; |
|
| 172 | + fseek($this->fh, $this->_pos); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param string $tag |
|
| 177 | + */ |
|
| 178 | + function seek_table($tag, $offset_in_table = 0) { |
|
| 179 | + $tpos = $this->get_table_pos($tag); |
|
| 180 | + $this->_pos = $tpos[0] + $offset_in_table; |
|
| 181 | + fseek($this->fh, $this->_pos); |
|
| 182 | + return $this->_pos; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + function read_tag() { |
|
| 186 | + $this->_pos += 4; |
|
| 187 | + return fread($this->fh, 4); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + function read_short() { |
|
| 191 | + $this->_pos += 2; |
|
| 192 | + $s = fread($this->fh, 2); |
|
| 193 | + $a = (ord($s[0]) << 8) + ord($s[1]); |
|
| 194 | + if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } |
|
| 195 | + return $a; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + function unpack_short($s) { |
|
| 199 | + $a = (ord($s[0]) << 8) + ord($s[1]); |
|
| 200 | + if ($a & (1 << 15)) { |
|
| 201 | + $a = ($a - (1 << 16)); |
|
| 202 | + } |
|
| 203 | + return $a; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + function read_ushort() { |
|
| 207 | + $this->_pos += 2; |
|
| 208 | + $s = fread($this->fh, 2); |
|
| 209 | + return (ord($s[0]) << 8) + ord($s[1]); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + function read_ulong() { |
|
| 213 | + $this->_pos += 4; |
|
| 214 | + $s = fread($this->fh, 4); |
|
| 215 | + // if large uInt32 as an integer, PHP converts it to -ve |
|
| 216 | + return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + function get_ushort($pos) { |
|
| 220 | + fseek($this->fh, $pos); |
|
| 221 | + $s = fread($this->fh, 2); |
|
| 222 | + return (ord($s[0]) << 8) + ord($s[1]); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + function get_ulong($pos) { |
|
| 226 | + fseek($this->fh, $pos); |
|
| 227 | + $s = fread($this->fh, 4); |
|
| 228 | + // iF large uInt32 as an integer, PHP converts it to -ve |
|
| 229 | + return (ord($s[0]) * 16777216) + (ord($s[1]) << 16) + (ord($s[2]) << 8) + ord($s[3]); // 16777216 = 1<<24 |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + function pack_short($val) { |
|
| 233 | + if ($val < 0) { |
|
| 234 | + $val = abs($val); |
|
| 235 | + $val = ~$val; |
|
| 236 | + $val += 1; |
|
| 237 | + } |
|
| 238 | + return pack("n", $val); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * @param string $value |
|
| 243 | + */ |
|
| 244 | + function splice($stream, $offset, $value) { |
|
| 245 | + return substr($stream, 0, $offset).$value.substr($stream, $offset + strlen($value)); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * @param string|null $stream |
|
| 250 | + * @param integer $offset |
|
| 251 | + */ |
|
| 252 | + function _set_ushort($stream, $offset, $value) { |
|
| 253 | + $up = pack("n", $value); |
|
| 254 | + return $this->splice($stream, $offset, $up); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + function _set_short($stream, $offset, $val) { |
|
| 258 | + if ($val < 0) { |
|
| 259 | + $val = abs($val); |
|
| 260 | + $val = ~$val; |
|
| 261 | + $val += 1; |
|
| 262 | + } |
|
| 263 | + $up = pack("n", $val); |
|
| 264 | + return $this->splice($stream, $offset, $up); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + function get_chunk($pos, $length) { |
|
| 268 | + fseek($this->fh, $pos); |
|
| 269 | + if ($length < 1) { return ''; } |
|
| 270 | + return (fread($this->fh, $length)); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * @param string $tag |
|
| 275 | + */ |
|
| 276 | + function get_table($tag) { |
|
| 277 | + list($pos, $length) = $this->get_table_pos($tag); |
|
| 278 | + if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
|
| 279 | + fseek($this->fh, $pos); |
|
| 280 | + return (fread($this->fh, $length)); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * @param string $tag |
|
| 285 | + * @param null|string $data |
|
| 286 | + */ |
|
| 287 | + function add($tag, $data) { |
|
| 288 | + if ($tag == 'head') { |
|
| 289 | + $data = $this->splice($data, 8, "\0\0\0\0"); |
|
| 290 | + } |
|
| 291 | + $this->otables[$tag] = $data; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | 294 | |
| 295 | 295 | |
@@ -163,19 +163,19 @@ |
||
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /** |
| 166 | - * Get MD5 as binary string |
|
| 167 | - * @param string $string |
|
| 168 | - */ |
|
| 166 | + * Get MD5 as binary string |
|
| 167 | + * @param string $string |
|
| 168 | + */ |
|
| 169 | 169 | function _md5_16($string) |
| 170 | 170 | { |
| 171 | 171 | return pack('H*', md5($string)); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
| 175 | - * Compute O value |
|
| 176 | - * @param string $user_pass |
|
| 177 | - * @param string $owner_pass |
|
| 178 | - */ |
|
| 175 | + * Compute O value |
|
| 176 | + * @param string $user_pass |
|
| 177 | + * @param string $owner_pass |
|
| 178 | + */ |
|
| 179 | 179 | function _Ovalue($user_pass, $owner_pass) |
| 180 | 180 | { |
| 181 | 181 | $tmp = $this->_md5_16($owner_pass); |
@@ -38,563 +38,563 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class Cipher_DES extends Cipher |
| 40 | 40 | { |
| 41 | - /** @type integer BYTES_BLOCK The block size, in bytes */ |
|
| 42 | - const BYTES_BLOCK = 8; // 64 bits |
|
| 41 | + /** @type integer BYTES_BLOCK The block size, in bytes */ |
|
| 42 | + const BYTES_BLOCK = 8; // 64 bits |
|
| 43 | 43 | |
| 44 | - /** @type integer BYTES_KEY The key size, in bytes */ |
|
| 45 | - const BYTES_KEY = 8; // 64 bits |
|
| 44 | + /** @type integer BYTES_KEY The key size, in bytes */ |
|
| 45 | + const BYTES_KEY = 8; // 64 bits |
|
| 46 | 46 | |
| 47 | - /** @type array $sub_keys The permutated subkeys */ |
|
| 48 | - protected $sub_keys = array(); |
|
| 47 | + /** @type array $sub_keys The permutated subkeys */ |
|
| 48 | + protected $sub_keys = array(); |
|
| 49 | 49 | |
| 50 | - /* |
|
| 50 | + /* |
|
| 51 | 51 | * Tables initialized in the initTables() |
| 52 | 52 | */ |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @type array $_pc1 Permutated choice 1 (PC1), |
|
| 56 | - * This should be considered a constant |
|
| 57 | - */ |
|
| 58 | - protected static $_pc1 = array(); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @type array $_pc2 Permutated choice 2 (PC2), |
|
| 62 | - * This should be considered a constant |
|
| 63 | - */ |
|
| 64 | - protected static $_pc2 = array(); |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @type array $_key_sched The key schedule, |
|
| 68 | - * This should be considered a constant |
|
| 69 | - */ |
|
| 70 | - protected static $_key_sched = array(); |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @type array $_ip The Initial Permutation (IP), |
|
| 74 | - * This should be considered a constant |
|
| 75 | - */ |
|
| 76 | - private static $_ip = array(); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @type array $_e The Expansion table (E), |
|
| 80 | - * This should be considered a constant |
|
| 81 | - */ |
|
| 82 | - private static $_e = array(); |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @type array $_s The Substitution Box (S), |
|
| 86 | - * This should be considered a constant |
|
| 87 | - */ |
|
| 88 | - private static $_s = array(); |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @type array $_p The Permutation table (P), |
|
| 92 | - * This should be considered a constant |
|
| 93 | - */ |
|
| 94 | - private static $_p = array(); |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @type array $_ip The The Final Permutation table (FP), |
|
| 98 | - * This should be considered a constant |
|
| 99 | - */ |
|
| 100 | - private static $_fp = array(); |
|
| 101 | - |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Constructor, used only when calling this class directly |
|
| 105 | - * for classes that extend this class, call __construct1() |
|
| 106 | - * |
|
| 107 | - * @param string $key The key used for Encryption/Decryption |
|
| 108 | - * @return void |
|
| 109 | - */ |
|
| 110 | - public function __construct($key) |
|
| 111 | - { |
|
| 112 | - // set the DES key |
|
| 113 | - parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY); |
|
| 114 | - |
|
| 115 | - // initialize variables |
|
| 116 | - $this->initTables(); |
|
| 117 | - |
|
| 118 | - // DES requires that data is 64 bits |
|
| 119 | - $this->blockSize(self::BYTES_BLOCK); |
|
| 120 | - |
|
| 121 | - // create the 16 rounds of 56 bit keys |
|
| 122 | - $this->keyPermutation(); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Second Constructor, used only by child classes that extend this class |
|
| 128 | - * |
|
| 129 | - * @param string $cipher The name of the cipher extending this class |
|
| 130 | - * @param string $key The key used for Encryption/Decryption |
|
| 131 | - * @param integer $key_byte_sz The required byte size of the extending cipher |
|
| 132 | - * @return void |
|
| 133 | - */ |
|
| 134 | - protected function __construct1($cipher, $key, $key_byte_sz) |
|
| 135 | - { |
|
| 136 | - // set the key and key size |
|
| 137 | - parent::__construct($cipher, $key, $key_byte_sz); |
|
| 138 | - |
|
| 139 | - // initialize variables |
|
| 140 | - $this->initTables(); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Destructor |
|
| 146 | - * |
|
| 147 | - * @return void |
|
| 148 | - */ |
|
| 149 | - public function __destruct() |
|
| 150 | - { |
|
| 151 | - parent::__destruct(); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Encrypt plain text data using DES |
|
| 157 | - * |
|
| 158 | - * @return boolean Returns true |
|
| 159 | - */ |
|
| 160 | - public function encrypt(&$text) |
|
| 161 | - { |
|
| 162 | - $this->operation(parent::ENCRYPT); |
|
| 163 | - return $this->des($text); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Decrypt a DES encrypted string |
|
| 169 | - * |
|
| 170 | - * @return boolean Returns true |
|
| 171 | - */ |
|
| 172 | - public function decrypt(&$text) |
|
| 173 | - { |
|
| 174 | - $this->operation(parent::DECRYPT); |
|
| 175 | - return $this->des($text); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * This is where the actual encrypt/decryption takes place. Since |
|
| 181 | - * encryption and decryption are the same algorithm in DES, we only |
|
| 182 | - * need one function to do both. |
|
| 183 | - * |
|
| 184 | - * @param string $data The string to be encrypted or decrypted |
|
| 185 | - * @return boolean Returns true |
|
| 186 | - */ |
|
| 187 | - protected function des(&$data) |
|
| 188 | - { |
|
| 189 | - $l = array(); |
|
| 190 | - $r = array(); |
|
| 191 | - |
|
| 192 | - // step two: Initial Permutation (IP) of plaintext |
|
| 193 | - $data = $this->ip($data); |
|
| 194 | - |
|
| 195 | - // divide the permuted block IP into a left half L0 of 32 bits, |
|
| 196 | - // and a right half R0 of 32 bits |
|
| 197 | - $l[0] = substr($data, 0, 32); |
|
| 198 | - $r[0] = substr($data, 32, 32); |
|
| 199 | - |
|
| 200 | - for ($n = 1; $n <= 16; ++$n) |
|
| 201 | - { |
|
| 202 | - $l[$n] = $r[$n - 1]; |
|
| 203 | - |
|
| 204 | - if ($this->operation() == parent::DECRYPT) { |
|
| 205 | - $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); |
|
| 206 | - } else { |
|
| 207 | - $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // XOR F with Ln |
|
| 211 | - $r[$n] = $this->xorBin($l[$n - 1], $f); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
|
| 215 | - // L[16] and R[16] so that it becomes R[16]L[16] |
|
| 216 | - $data = $r[16].$l[16]; |
|
| 217 | - |
|
| 218 | - // now do the final permutation |
|
| 219 | - $data = $this->fp($data); |
|
| 220 | - $data = parent::bin2Str($data); |
|
| 221 | - |
|
| 222 | - return true; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * The Key permutation, based on tables $_pc1 and $_pc2 |
|
| 228 | - * Create 16 subkeys, each of which is 48-bits long. |
|
| 229 | - * |
|
| 230 | - * @return void |
|
| 231 | - */ |
|
| 232 | - private function keyPermutation() |
|
| 233 | - { |
|
| 234 | - $this->sub_keys = array(); |
|
| 235 | - $pc1m = array(); |
|
| 236 | - $pcr = array(); |
|
| 237 | - $c = array(); |
|
| 238 | - $d = array(); |
|
| 239 | - |
|
| 240 | - // convert the key to binary |
|
| 241 | - $binkey = parent::str2Bin($this->key()); |
|
| 242 | - |
|
| 243 | - // reduce the key down to 56bits based on table $_pc1 |
|
| 244 | - for ($i = 0; $i < 56; ++$i) { |
|
| 245 | - $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // split $pc1m in half (C0 and D0) |
|
| 249 | - $c[0] = array_slice($pc1m, 0, 28); |
|
| 250 | - $d[0] = array_slice($pc1m, 28, 28); |
|
| 251 | - |
|
| 252 | - // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
| 253 | - // where 1 <= n <= 16 |
|
| 254 | - for ($i = 1; $i <= 16; ++$i) |
|
| 255 | - { |
|
| 256 | - // now set the next Cn and Dn as the previous Cn and Dn |
|
| 257 | - $c[$i] = $c[$i - 1]; |
|
| 258 | - $d[$i] = $d[$i - 1]; |
|
| 259 | - |
|
| 260 | - for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) |
|
| 261 | - { |
|
| 262 | - // do a left shift, move each bit one place to the left, |
|
| 263 | - // except for the first bit, which is cycled to the end |
|
| 264 | - // of the block. |
|
| 265 | - $c[$i][] = array_shift($c[$i]); |
|
| 266 | - $d[$i][] = array_shift($d[$i]); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
| 270 | - // following permutation table to each of the concatenated |
|
| 271 | - // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
| 272 | - // of these. |
|
| 273 | - $CnDn = array_merge($c[$i], $d[$i]); |
|
| 274 | - $this->sub_keys[$i - 1] = ""; |
|
| 275 | - for ($j = 0; $j < 48; ++$j) { |
|
| 276 | - $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; |
|
| 277 | - } |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - // the sub_keys are created, we are done with the key permutation |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Initial Permutation (IP) |
|
| 286 | - * Now we encode each 64-bit block of data. There is an initial permutation IP of |
|
| 287 | - * the 64 bits of the message data M. This rearranges the bits according to the |
|
| 288 | - * following table, where the entries in the table show the new arrangement of the |
|
| 289 | - * bits from their initial order. The 58th bit of M becomes the first bit of IP. |
|
| 290 | - * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last |
|
| 291 | - * bit of IP. |
|
| 292 | - * |
|
| 293 | - * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): |
|
| 294 | - * The initial permution was used to make it easier to load plain text and cipher text |
|
| 295 | - * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP |
|
| 296 | - * are not necessary in software implementations and do not affect the security. However, |
|
| 297 | - * the IP and FP are part of the DES standard and not implementing it would deviate from |
|
| 298 | - * the standard, so we will do it here in phpCrypt. |
|
| 299 | - * |
|
| 300 | - * @param string $text |
|
| 301 | - * @return string the Initial Permutation (IP) |
|
| 302 | - */ |
|
| 303 | - private function ip($text) |
|
| 304 | - { |
|
| 305 | - $text = parent::str2Bin($text); |
|
| 306 | - $ip = ""; |
|
| 307 | - |
|
| 308 | - // loop through the 64 bit block, ordering it occording to $_ip |
|
| 309 | - for ($i = 0; $i < 64; ++$i) { |
|
| 310 | - $ip .= $text[self::$_ip[$i] - 1]; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - return $ip; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. |
|
| 319 | - * This is done by using a selection table that repeats some of the bits in Rn-1. We'll |
|
| 320 | - * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
|
| 321 | - * block, and a 48 bit output block. |
|
| 322 | - * |
|
| 323 | - * @param string $r 32 bit binary, each bit in an array element |
|
| 324 | - * @param string $k 48 bit binary string |
|
| 325 | - * @return string 48 bit binary string |
|
| 326 | - */ |
|
| 327 | - private function f($r, $k) |
|
| 328 | - { |
|
| 329 | - $bin = parent::xorBin($k, $this->E($r)); |
|
| 330 | - |
|
| 331 | - // create a 32-bit string from $bits by passing it through the S-Boxes |
|
| 332 | - $bin = $this->s($bin); |
|
| 333 | - |
|
| 334 | - // now send permute $bin as defined by table self::$_p |
|
| 335 | - $bin = $this->p($bin); |
|
| 336 | - |
|
| 337 | - return $bin; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of |
|
| 343 | - * 6 bits each, are obtained by selecting the bits in its inputs in order according |
|
| 344 | - * to the self::$_e[] table. |
|
| 345 | - * This is only used in the F() function |
|
| 346 | - * |
|
| 347 | - * @param string $r 32 bit binary, each bit in an array element |
|
| 348 | - * @return string 48 bit binary string |
|
| 349 | - */ |
|
| 350 | - private function e($r) |
|
| 351 | - { |
|
| 352 | - $e = ""; |
|
| 353 | - for ($i = 0; $i < 48; ++$i) { |
|
| 354 | - $e .= $r[self::$_e[$i] - 1]; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - return $e; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * S-Box |
|
| 363 | - * Take a 48-bit string from F() and run it through the S-Boxes, this requires |
|
| 364 | - * us to break up the 48-bit string into 8 groups of 6 bits before sending it |
|
| 365 | - * through the S-Boxes |
|
| 366 | - * |
|
| 367 | - * @param string $bits The 48-bit string from F() to be processed |
|
| 368 | - * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes |
|
| 369 | - */ |
|
| 370 | - private function s($bits) |
|
| 371 | - { |
|
| 372 | - $s = ""; |
|
| 373 | - |
|
| 374 | - for ($i = 0; $i <= 42; $i += 6) |
|
| 375 | - { |
|
| 376 | - $sbits = substr($bits, $i, 6); |
|
| 377 | - |
|
| 378 | - // we need to determine the S-Box column number and row number |
|
| 379 | - // from the 6 bit string passed in, this is done using the following method: |
|
| 380 | - // The First & Last bits represent a number between 0-3, used to determine which row |
|
| 381 | - // The middle 4 bits represent a number between 0-15, used to determine the column |
|
| 382 | - $row = bindec("{$sbits[0]}{$sbits[5]}"); |
|
| 383 | - $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); |
|
| 384 | - |
|
| 385 | - // determine the position in the S-BOX, S-Box table is in self::$_s[] |
|
| 386 | - $pos = ($row * 16) + $col; |
|
| 387 | - |
|
| 388 | - // get the integer from the S-Box and convert it to binary |
|
| 389 | - $bin = decbin(self::$_s[($i / 6)][$pos]); |
|
| 390 | - $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - return $s; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Permutation P |
|
| 399 | - * The permutation P is defined in self::$_p. P() returns a 32-bit output |
|
| 400 | - * from a 32-bit input from a binary string from the S-BOX by permuting |
|
| 401 | - * the bits of the input block. |
|
| 402 | - * This is only used inside of F() function |
|
| 403 | - * |
|
| 404 | - * @param string $s A 32-bit string originating from being passed through S-Box |
|
| 405 | - * @return string A 32-bit string, which is $s permuted through table self::$_p |
|
| 406 | - */ |
|
| 407 | - private function p($s) |
|
| 408 | - { |
|
| 409 | - $p = ""; |
|
| 410 | - for ($i = 0; $i < 32; ++$i) { |
|
| 411 | - $p .= $s[self::$_p[$i] - 1]; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - return $p; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Final Permutation (FP) |
|
| 420 | - * Read the comment about IP and FP being unecessary in software implmented DES (though |
|
| 421 | - * we will do it to follow the DES standard). |
|
| 422 | - * |
|
| 423 | - * @param string $bin A 64-bit binary string |
|
| 424 | - * @return string A 64-bit binary string that has been run through self::$_fp[] table |
|
| 425 | - */ |
|
| 426 | - private function fp($bin) |
|
| 427 | - { |
|
| 428 | - $fp = ""; |
|
| 429 | - for ($i = 0; $i < 64; ++$i) { |
|
| 430 | - $fp .= $bin[self::$_fp[$i] - 1]; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - return $fp; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Initialize all the tables, this function is called inside the constructor |
|
| 439 | - * |
|
| 440 | - * @return void |
|
| 441 | - */ |
|
| 442 | - private function initTables() |
|
| 443 | - { |
|
| 444 | - // permuted choice 1 (PC1) |
|
| 445 | - // these values are chars and should be run through chr() when used |
|
| 446 | - self::$_pc1 = array( |
|
| 447 | - 57, 49, 41, 33, 25, 17, 9, |
|
| 448 | - 1, 58, 50, 42, 34, 26, 18, |
|
| 449 | - 10, 2, 59, 51, 43, 35, 27, |
|
| 450 | - 19, 11, 3, 60, 52, 44, 36, |
|
| 451 | - 63, 55, 47, 39, 31, 23, 15, |
|
| 452 | - 7, 62, 54, 46, 38, 30, 22, |
|
| 453 | - 14, 6, 61, 53, 45, 37, 29, |
|
| 454 | - 21, 13, 5, 28, 20, 12, 4 |
|
| 455 | - ); |
|
| 456 | - |
|
| 457 | - // permuted choice 2 (PC2) |
|
| 458 | - // these values are chars and should be run through chr() when used |
|
| 459 | - self::$_pc2 = array( |
|
| 460 | - 14, 17, 11, 24, 1, 5, |
|
| 461 | - 3, 28, 15, 6, 21, 10, |
|
| 462 | - 23, 19, 12, 4, 26, 8, |
|
| 463 | - 16, 7, 27, 20, 13, 2, |
|
| 464 | - 41, 52, 31, 37, 47, 55, |
|
| 465 | - 30, 40, 51, 45, 33, 48, |
|
| 466 | - 44, 49, 39, 56, 34, 53, |
|
| 467 | - 46, 42, 50, 36, 29, 32 |
|
| 468 | - ); |
|
| 469 | - |
|
| 470 | - // initial permutation (IP) |
|
| 471 | - self::$_ip = array( |
|
| 472 | - 58, 50, 42, 34, 26, 18, 10, 2, |
|
| 473 | - 60, 52, 44, 36, 28, 20, 12, 4, |
|
| 474 | - 62, 54, 46, 38, 30, 22, 14, 6, |
|
| 475 | - 64, 56, 48, 40, 32, 24, 16, 8, |
|
| 476 | - 57, 49, 41, 33, 25, 17, 9, 1, |
|
| 477 | - 59, 51, 43, 35, 27, 19, 11, 3, |
|
| 478 | - 61, 53, 45, 37, 29, 21, 13, 5, |
|
| 479 | - 63, 55, 47, 39, 31, 23, 15, 7 |
|
| 480 | - ); |
|
| 481 | - |
|
| 482 | - // expansion (E) |
|
| 483 | - self::$_e = array( |
|
| 484 | - 32, 1, 2, 3, 4, 5, |
|
| 485 | - 4, 5, 6, 7, 8, 9, |
|
| 486 | - 8, 9, 10, 11, 12, 13, |
|
| 487 | - 12, 13, 14, 15, 16, 17, |
|
| 488 | - 16, 17, 18, 19, 20, 21, |
|
| 489 | - 20, 21, 22, 23, 24, 25, |
|
| 490 | - 24, 25, 26, 27, 28, 29, |
|
| 491 | - 28, 29, 30, 31, 32, 1 |
|
| 492 | - ); |
|
| 493 | - |
|
| 494 | - // substition box (S) |
|
| 495 | - self::$_s = array( |
|
| 496 | - /* S1 */ |
|
| 497 | - array( |
|
| 498 | - 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
| 499 | - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
| 500 | - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
| 501 | - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
| 502 | - ), |
|
| 503 | - |
|
| 504 | - /* S2 */ |
|
| 505 | - array( |
|
| 506 | - 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
| 507 | - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
| 508 | - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
| 509 | - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
| 510 | - ), |
|
| 511 | - |
|
| 512 | - /* S3 */ |
|
| 513 | - array( |
|
| 514 | - 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
| 515 | - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
| 516 | - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
| 517 | - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
| 518 | - ), |
|
| 519 | - |
|
| 520 | - /* S4 */ |
|
| 521 | - array( |
|
| 522 | - 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
| 523 | - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
| 524 | - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
| 525 | - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
| 526 | - ), |
|
| 527 | - |
|
| 528 | - /* S5 */ |
|
| 529 | - array( |
|
| 530 | - 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
| 531 | - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
| 532 | - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
| 533 | - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
| 534 | - ), |
|
| 535 | - |
|
| 536 | - /* S6 */ |
|
| 537 | - array( |
|
| 538 | - 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
| 539 | - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
| 540 | - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
| 541 | - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
| 542 | - ), |
|
| 543 | - |
|
| 544 | - /* S7 */ |
|
| 545 | - array( |
|
| 546 | - 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
| 547 | - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
| 548 | - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
| 549 | - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
| 550 | - ), |
|
| 551 | - |
|
| 552 | - /* S8 */ |
|
| 553 | - array( |
|
| 554 | - 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
| 555 | - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
| 556 | - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
| 557 | - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
| 558 | - ) |
|
| 559 | - ); |
|
| 560 | - |
|
| 561 | - // permutation (P) |
|
| 562 | - self::$_p = array( |
|
| 563 | - 16, 7, 20, 21, |
|
| 564 | - 29, 12, 28, 17, |
|
| 565 | - 1, 15, 23, 26, |
|
| 566 | - 5, 18, 31, 10, |
|
| 567 | - 2, 8, 24, 14, |
|
| 568 | - 32, 27, 3, 9, |
|
| 569 | - 19, 13, 30, 6, |
|
| 570 | - 22, 11, 4, 25 |
|
| 571 | - ); |
|
| 572 | - |
|
| 573 | - // final permutation (FP) |
|
| 574 | - self::$_fp = array( |
|
| 575 | - 40, 8, 48, 16, 56, 24, 64, 32, |
|
| 576 | - 39, 7, 47, 15, 55, 23, 63, 31, |
|
| 577 | - 38, 6, 46, 14, 54, 22, 62, 30, |
|
| 578 | - 37, 5, 45, 13, 53, 21, 61, 29, |
|
| 579 | - 36, 4, 44, 12, 52, 20, 60, 28, |
|
| 580 | - 35, 3, 43, 11, 51, 19, 59, 27, |
|
| 581 | - 34, 2, 42, 10, 50, 18, 58, 26, |
|
| 582 | - 33, 1, 41, 9, 49, 17, 57, 25 |
|
| 583 | - ); |
|
| 584 | - |
|
| 585 | - // key schedule used in KeyPermutation() |
|
| 586 | - self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Indicates this is a block cipher |
|
| 592 | - * |
|
| 593 | - * @return integer Returns Cipher::BLOCK |
|
| 594 | - */ |
|
| 595 | - public function type() |
|
| 596 | - { |
|
| 597 | - return parent::BLOCK; |
|
| 598 | - } |
|
| 54 | + /** |
|
| 55 | + * @type array $_pc1 Permutated choice 1 (PC1), |
|
| 56 | + * This should be considered a constant |
|
| 57 | + */ |
|
| 58 | + protected static $_pc1 = array(); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @type array $_pc2 Permutated choice 2 (PC2), |
|
| 62 | + * This should be considered a constant |
|
| 63 | + */ |
|
| 64 | + protected static $_pc2 = array(); |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @type array $_key_sched The key schedule, |
|
| 68 | + * This should be considered a constant |
|
| 69 | + */ |
|
| 70 | + protected static $_key_sched = array(); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @type array $_ip The Initial Permutation (IP), |
|
| 74 | + * This should be considered a constant |
|
| 75 | + */ |
|
| 76 | + private static $_ip = array(); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @type array $_e The Expansion table (E), |
|
| 80 | + * This should be considered a constant |
|
| 81 | + */ |
|
| 82 | + private static $_e = array(); |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @type array $_s The Substitution Box (S), |
|
| 86 | + * This should be considered a constant |
|
| 87 | + */ |
|
| 88 | + private static $_s = array(); |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @type array $_p The Permutation table (P), |
|
| 92 | + * This should be considered a constant |
|
| 93 | + */ |
|
| 94 | + private static $_p = array(); |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @type array $_ip The The Final Permutation table (FP), |
|
| 98 | + * This should be considered a constant |
|
| 99 | + */ |
|
| 100 | + private static $_fp = array(); |
|
| 101 | + |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Constructor, used only when calling this class directly |
|
| 105 | + * for classes that extend this class, call __construct1() |
|
| 106 | + * |
|
| 107 | + * @param string $key The key used for Encryption/Decryption |
|
| 108 | + * @return void |
|
| 109 | + */ |
|
| 110 | + public function __construct($key) |
|
| 111 | + { |
|
| 112 | + // set the DES key |
|
| 113 | + parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY); |
|
| 114 | + |
|
| 115 | + // initialize variables |
|
| 116 | + $this->initTables(); |
|
| 117 | + |
|
| 118 | + // DES requires that data is 64 bits |
|
| 119 | + $this->blockSize(self::BYTES_BLOCK); |
|
| 120 | + |
|
| 121 | + // create the 16 rounds of 56 bit keys |
|
| 122 | + $this->keyPermutation(); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Second Constructor, used only by child classes that extend this class |
|
| 128 | + * |
|
| 129 | + * @param string $cipher The name of the cipher extending this class |
|
| 130 | + * @param string $key The key used for Encryption/Decryption |
|
| 131 | + * @param integer $key_byte_sz The required byte size of the extending cipher |
|
| 132 | + * @return void |
|
| 133 | + */ |
|
| 134 | + protected function __construct1($cipher, $key, $key_byte_sz) |
|
| 135 | + { |
|
| 136 | + // set the key and key size |
|
| 137 | + parent::__construct($cipher, $key, $key_byte_sz); |
|
| 138 | + |
|
| 139 | + // initialize variables |
|
| 140 | + $this->initTables(); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Destructor |
|
| 146 | + * |
|
| 147 | + * @return void |
|
| 148 | + */ |
|
| 149 | + public function __destruct() |
|
| 150 | + { |
|
| 151 | + parent::__destruct(); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Encrypt plain text data using DES |
|
| 157 | + * |
|
| 158 | + * @return boolean Returns true |
|
| 159 | + */ |
|
| 160 | + public function encrypt(&$text) |
|
| 161 | + { |
|
| 162 | + $this->operation(parent::ENCRYPT); |
|
| 163 | + return $this->des($text); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Decrypt a DES encrypted string |
|
| 169 | + * |
|
| 170 | + * @return boolean Returns true |
|
| 171 | + */ |
|
| 172 | + public function decrypt(&$text) |
|
| 173 | + { |
|
| 174 | + $this->operation(parent::DECRYPT); |
|
| 175 | + return $this->des($text); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * This is where the actual encrypt/decryption takes place. Since |
|
| 181 | + * encryption and decryption are the same algorithm in DES, we only |
|
| 182 | + * need one function to do both. |
|
| 183 | + * |
|
| 184 | + * @param string $data The string to be encrypted or decrypted |
|
| 185 | + * @return boolean Returns true |
|
| 186 | + */ |
|
| 187 | + protected function des(&$data) |
|
| 188 | + { |
|
| 189 | + $l = array(); |
|
| 190 | + $r = array(); |
|
| 191 | + |
|
| 192 | + // step two: Initial Permutation (IP) of plaintext |
|
| 193 | + $data = $this->ip($data); |
|
| 194 | + |
|
| 195 | + // divide the permuted block IP into a left half L0 of 32 bits, |
|
| 196 | + // and a right half R0 of 32 bits |
|
| 197 | + $l[0] = substr($data, 0, 32); |
|
| 198 | + $r[0] = substr($data, 32, 32); |
|
| 199 | + |
|
| 200 | + for ($n = 1; $n <= 16; ++$n) |
|
| 201 | + { |
|
| 202 | + $l[$n] = $r[$n - 1]; |
|
| 203 | + |
|
| 204 | + if ($this->operation() == parent::DECRYPT) { |
|
| 205 | + $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); |
|
| 206 | + } else { |
|
| 207 | + $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // XOR F with Ln |
|
| 211 | + $r[$n] = $this->xorBin($l[$n - 1], $f); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
|
| 215 | + // L[16] and R[16] so that it becomes R[16]L[16] |
|
| 216 | + $data = $r[16].$l[16]; |
|
| 217 | + |
|
| 218 | + // now do the final permutation |
|
| 219 | + $data = $this->fp($data); |
|
| 220 | + $data = parent::bin2Str($data); |
|
| 221 | + |
|
| 222 | + return true; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * The Key permutation, based on tables $_pc1 and $_pc2 |
|
| 228 | + * Create 16 subkeys, each of which is 48-bits long. |
|
| 229 | + * |
|
| 230 | + * @return void |
|
| 231 | + */ |
|
| 232 | + private function keyPermutation() |
|
| 233 | + { |
|
| 234 | + $this->sub_keys = array(); |
|
| 235 | + $pc1m = array(); |
|
| 236 | + $pcr = array(); |
|
| 237 | + $c = array(); |
|
| 238 | + $d = array(); |
|
| 239 | + |
|
| 240 | + // convert the key to binary |
|
| 241 | + $binkey = parent::str2Bin($this->key()); |
|
| 242 | + |
|
| 243 | + // reduce the key down to 56bits based on table $_pc1 |
|
| 244 | + for ($i = 0; $i < 56; ++$i) { |
|
| 245 | + $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // split $pc1m in half (C0 and D0) |
|
| 249 | + $c[0] = array_slice($pc1m, 0, 28); |
|
| 250 | + $d[0] = array_slice($pc1m, 28, 28); |
|
| 251 | + |
|
| 252 | + // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
| 253 | + // where 1 <= n <= 16 |
|
| 254 | + for ($i = 1; $i <= 16; ++$i) |
|
| 255 | + { |
|
| 256 | + // now set the next Cn and Dn as the previous Cn and Dn |
|
| 257 | + $c[$i] = $c[$i - 1]; |
|
| 258 | + $d[$i] = $d[$i - 1]; |
|
| 259 | + |
|
| 260 | + for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) |
|
| 261 | + { |
|
| 262 | + // do a left shift, move each bit one place to the left, |
|
| 263 | + // except for the first bit, which is cycled to the end |
|
| 264 | + // of the block. |
|
| 265 | + $c[$i][] = array_shift($c[$i]); |
|
| 266 | + $d[$i][] = array_shift($d[$i]); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
| 270 | + // following permutation table to each of the concatenated |
|
| 271 | + // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
| 272 | + // of these. |
|
| 273 | + $CnDn = array_merge($c[$i], $d[$i]); |
|
| 274 | + $this->sub_keys[$i - 1] = ""; |
|
| 275 | + for ($j = 0; $j < 48; ++$j) { |
|
| 276 | + $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + // the sub_keys are created, we are done with the key permutation |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Initial Permutation (IP) |
|
| 286 | + * Now we encode each 64-bit block of data. There is an initial permutation IP of |
|
| 287 | + * the 64 bits of the message data M. This rearranges the bits according to the |
|
| 288 | + * following table, where the entries in the table show the new arrangement of the |
|
| 289 | + * bits from their initial order. The 58th bit of M becomes the first bit of IP. |
|
| 290 | + * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last |
|
| 291 | + * bit of IP. |
|
| 292 | + * |
|
| 293 | + * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): |
|
| 294 | + * The initial permution was used to make it easier to load plain text and cipher text |
|
| 295 | + * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP |
|
| 296 | + * are not necessary in software implementations and do not affect the security. However, |
|
| 297 | + * the IP and FP are part of the DES standard and not implementing it would deviate from |
|
| 298 | + * the standard, so we will do it here in phpCrypt. |
|
| 299 | + * |
|
| 300 | + * @param string $text |
|
| 301 | + * @return string the Initial Permutation (IP) |
|
| 302 | + */ |
|
| 303 | + private function ip($text) |
|
| 304 | + { |
|
| 305 | + $text = parent::str2Bin($text); |
|
| 306 | + $ip = ""; |
|
| 307 | + |
|
| 308 | + // loop through the 64 bit block, ordering it occording to $_ip |
|
| 309 | + for ($i = 0; $i < 64; ++$i) { |
|
| 310 | + $ip .= $text[self::$_ip[$i] - 1]; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + return $ip; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. |
|
| 319 | + * This is done by using a selection table that repeats some of the bits in Rn-1. We'll |
|
| 320 | + * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
|
| 321 | + * block, and a 48 bit output block. |
|
| 322 | + * |
|
| 323 | + * @param string $r 32 bit binary, each bit in an array element |
|
| 324 | + * @param string $k 48 bit binary string |
|
| 325 | + * @return string 48 bit binary string |
|
| 326 | + */ |
|
| 327 | + private function f($r, $k) |
|
| 328 | + { |
|
| 329 | + $bin = parent::xorBin($k, $this->E($r)); |
|
| 330 | + |
|
| 331 | + // create a 32-bit string from $bits by passing it through the S-Boxes |
|
| 332 | + $bin = $this->s($bin); |
|
| 333 | + |
|
| 334 | + // now send permute $bin as defined by table self::$_p |
|
| 335 | + $bin = $this->p($bin); |
|
| 336 | + |
|
| 337 | + return $bin; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of |
|
| 343 | + * 6 bits each, are obtained by selecting the bits in its inputs in order according |
|
| 344 | + * to the self::$_e[] table. |
|
| 345 | + * This is only used in the F() function |
|
| 346 | + * |
|
| 347 | + * @param string $r 32 bit binary, each bit in an array element |
|
| 348 | + * @return string 48 bit binary string |
|
| 349 | + */ |
|
| 350 | + private function e($r) |
|
| 351 | + { |
|
| 352 | + $e = ""; |
|
| 353 | + for ($i = 0; $i < 48; ++$i) { |
|
| 354 | + $e .= $r[self::$_e[$i] - 1]; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + return $e; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * S-Box |
|
| 363 | + * Take a 48-bit string from F() and run it through the S-Boxes, this requires |
|
| 364 | + * us to break up the 48-bit string into 8 groups of 6 bits before sending it |
|
| 365 | + * through the S-Boxes |
|
| 366 | + * |
|
| 367 | + * @param string $bits The 48-bit string from F() to be processed |
|
| 368 | + * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes |
|
| 369 | + */ |
|
| 370 | + private function s($bits) |
|
| 371 | + { |
|
| 372 | + $s = ""; |
|
| 373 | + |
|
| 374 | + for ($i = 0; $i <= 42; $i += 6) |
|
| 375 | + { |
|
| 376 | + $sbits = substr($bits, $i, 6); |
|
| 377 | + |
|
| 378 | + // we need to determine the S-Box column number and row number |
|
| 379 | + // from the 6 bit string passed in, this is done using the following method: |
|
| 380 | + // The First & Last bits represent a number between 0-3, used to determine which row |
|
| 381 | + // The middle 4 bits represent a number between 0-15, used to determine the column |
|
| 382 | + $row = bindec("{$sbits[0]}{$sbits[5]}"); |
|
| 383 | + $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); |
|
| 384 | + |
|
| 385 | + // determine the position in the S-BOX, S-Box table is in self::$_s[] |
|
| 386 | + $pos = ($row * 16) + $col; |
|
| 387 | + |
|
| 388 | + // get the integer from the S-Box and convert it to binary |
|
| 389 | + $bin = decbin(self::$_s[($i / 6)][$pos]); |
|
| 390 | + $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + return $s; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Permutation P |
|
| 399 | + * The permutation P is defined in self::$_p. P() returns a 32-bit output |
|
| 400 | + * from a 32-bit input from a binary string from the S-BOX by permuting |
|
| 401 | + * the bits of the input block. |
|
| 402 | + * This is only used inside of F() function |
|
| 403 | + * |
|
| 404 | + * @param string $s A 32-bit string originating from being passed through S-Box |
|
| 405 | + * @return string A 32-bit string, which is $s permuted through table self::$_p |
|
| 406 | + */ |
|
| 407 | + private function p($s) |
|
| 408 | + { |
|
| 409 | + $p = ""; |
|
| 410 | + for ($i = 0; $i < 32; ++$i) { |
|
| 411 | + $p .= $s[self::$_p[$i] - 1]; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + return $p; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Final Permutation (FP) |
|
| 420 | + * Read the comment about IP and FP being unecessary in software implmented DES (though |
|
| 421 | + * we will do it to follow the DES standard). |
|
| 422 | + * |
|
| 423 | + * @param string $bin A 64-bit binary string |
|
| 424 | + * @return string A 64-bit binary string that has been run through self::$_fp[] table |
|
| 425 | + */ |
|
| 426 | + private function fp($bin) |
|
| 427 | + { |
|
| 428 | + $fp = ""; |
|
| 429 | + for ($i = 0; $i < 64; ++$i) { |
|
| 430 | + $fp .= $bin[self::$_fp[$i] - 1]; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + return $fp; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * Initialize all the tables, this function is called inside the constructor |
|
| 439 | + * |
|
| 440 | + * @return void |
|
| 441 | + */ |
|
| 442 | + private function initTables() |
|
| 443 | + { |
|
| 444 | + // permuted choice 1 (PC1) |
|
| 445 | + // these values are chars and should be run through chr() when used |
|
| 446 | + self::$_pc1 = array( |
|
| 447 | + 57, 49, 41, 33, 25, 17, 9, |
|
| 448 | + 1, 58, 50, 42, 34, 26, 18, |
|
| 449 | + 10, 2, 59, 51, 43, 35, 27, |
|
| 450 | + 19, 11, 3, 60, 52, 44, 36, |
|
| 451 | + 63, 55, 47, 39, 31, 23, 15, |
|
| 452 | + 7, 62, 54, 46, 38, 30, 22, |
|
| 453 | + 14, 6, 61, 53, 45, 37, 29, |
|
| 454 | + 21, 13, 5, 28, 20, 12, 4 |
|
| 455 | + ); |
|
| 456 | + |
|
| 457 | + // permuted choice 2 (PC2) |
|
| 458 | + // these values are chars and should be run through chr() when used |
|
| 459 | + self::$_pc2 = array( |
|
| 460 | + 14, 17, 11, 24, 1, 5, |
|
| 461 | + 3, 28, 15, 6, 21, 10, |
|
| 462 | + 23, 19, 12, 4, 26, 8, |
|
| 463 | + 16, 7, 27, 20, 13, 2, |
|
| 464 | + 41, 52, 31, 37, 47, 55, |
|
| 465 | + 30, 40, 51, 45, 33, 48, |
|
| 466 | + 44, 49, 39, 56, 34, 53, |
|
| 467 | + 46, 42, 50, 36, 29, 32 |
|
| 468 | + ); |
|
| 469 | + |
|
| 470 | + // initial permutation (IP) |
|
| 471 | + self::$_ip = array( |
|
| 472 | + 58, 50, 42, 34, 26, 18, 10, 2, |
|
| 473 | + 60, 52, 44, 36, 28, 20, 12, 4, |
|
| 474 | + 62, 54, 46, 38, 30, 22, 14, 6, |
|
| 475 | + 64, 56, 48, 40, 32, 24, 16, 8, |
|
| 476 | + 57, 49, 41, 33, 25, 17, 9, 1, |
|
| 477 | + 59, 51, 43, 35, 27, 19, 11, 3, |
|
| 478 | + 61, 53, 45, 37, 29, 21, 13, 5, |
|
| 479 | + 63, 55, 47, 39, 31, 23, 15, 7 |
|
| 480 | + ); |
|
| 481 | + |
|
| 482 | + // expansion (E) |
|
| 483 | + self::$_e = array( |
|
| 484 | + 32, 1, 2, 3, 4, 5, |
|
| 485 | + 4, 5, 6, 7, 8, 9, |
|
| 486 | + 8, 9, 10, 11, 12, 13, |
|
| 487 | + 12, 13, 14, 15, 16, 17, |
|
| 488 | + 16, 17, 18, 19, 20, 21, |
|
| 489 | + 20, 21, 22, 23, 24, 25, |
|
| 490 | + 24, 25, 26, 27, 28, 29, |
|
| 491 | + 28, 29, 30, 31, 32, 1 |
|
| 492 | + ); |
|
| 493 | + |
|
| 494 | + // substition box (S) |
|
| 495 | + self::$_s = array( |
|
| 496 | + /* S1 */ |
|
| 497 | + array( |
|
| 498 | + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
| 499 | + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
| 500 | + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
| 501 | + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
| 502 | + ), |
|
| 503 | + |
|
| 504 | + /* S2 */ |
|
| 505 | + array( |
|
| 506 | + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
| 507 | + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
| 508 | + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
| 509 | + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
| 510 | + ), |
|
| 511 | + |
|
| 512 | + /* S3 */ |
|
| 513 | + array( |
|
| 514 | + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
| 515 | + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
| 516 | + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
| 517 | + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
| 518 | + ), |
|
| 519 | + |
|
| 520 | + /* S4 */ |
|
| 521 | + array( |
|
| 522 | + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
| 523 | + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
| 524 | + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
| 525 | + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
| 526 | + ), |
|
| 527 | + |
|
| 528 | + /* S5 */ |
|
| 529 | + array( |
|
| 530 | + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
| 531 | + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
| 532 | + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
| 533 | + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
| 534 | + ), |
|
| 535 | + |
|
| 536 | + /* S6 */ |
|
| 537 | + array( |
|
| 538 | + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
| 539 | + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
| 540 | + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
| 541 | + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
| 542 | + ), |
|
| 543 | + |
|
| 544 | + /* S7 */ |
|
| 545 | + array( |
|
| 546 | + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
| 547 | + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
| 548 | + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
| 549 | + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
| 550 | + ), |
|
| 551 | + |
|
| 552 | + /* S8 */ |
|
| 553 | + array( |
|
| 554 | + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
| 555 | + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
| 556 | + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
| 557 | + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
| 558 | + ) |
|
| 559 | + ); |
|
| 560 | + |
|
| 561 | + // permutation (P) |
|
| 562 | + self::$_p = array( |
|
| 563 | + 16, 7, 20, 21, |
|
| 564 | + 29, 12, 28, 17, |
|
| 565 | + 1, 15, 23, 26, |
|
| 566 | + 5, 18, 31, 10, |
|
| 567 | + 2, 8, 24, 14, |
|
| 568 | + 32, 27, 3, 9, |
|
| 569 | + 19, 13, 30, 6, |
|
| 570 | + 22, 11, 4, 25 |
|
| 571 | + ); |
|
| 572 | + |
|
| 573 | + // final permutation (FP) |
|
| 574 | + self::$_fp = array( |
|
| 575 | + 40, 8, 48, 16, 56, 24, 64, 32, |
|
| 576 | + 39, 7, 47, 15, 55, 23, 63, 31, |
|
| 577 | + 38, 6, 46, 14, 54, 22, 62, 30, |
|
| 578 | + 37, 5, 45, 13, 53, 21, 61, 29, |
|
| 579 | + 36, 4, 44, 12, 52, 20, 60, 28, |
|
| 580 | + 35, 3, 43, 11, 51, 19, 59, 27, |
|
| 581 | + 34, 2, 42, 10, 50, 18, 58, 26, |
|
| 582 | + 33, 1, 41, 9, 49, 17, 57, 25 |
|
| 583 | + ); |
|
| 584 | + |
|
| 585 | + // key schedule used in KeyPermutation() |
|
| 586 | + self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Indicates this is a block cipher |
|
| 592 | + * |
|
| 593 | + * @return integer Returns Cipher::BLOCK |
|
| 594 | + */ |
|
| 595 | + public function type() |
|
| 596 | + { |
|
| 597 | + return parent::BLOCK; |
|
| 598 | + } |
|
| 599 | 599 | } |
| 600 | 600 | ?> |
@@ -4,6 +4,6 @@ |
||
| 4 | 4 | $up = -100; |
| 5 | 5 | $ut = 50; |
| 6 | 6 | for ($i = 0; $i <= 255; $i++) { |
| 7 | - $cw[chr($i)] = 600; |
|
| 7 | + $cw[chr($i)] = 600; |
|
| 8 | 8 | } |
| 9 | 9 | ?> |