-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.rc4crypt.php
More file actions
117 lines (110 loc) · 3.02 KB
/
class.rc4crypt.php
File metadata and controls
117 lines (110 loc) · 3.02 KB
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
<?php
/* vim: set expandtab shiftwidth=4 softtabstop=4 tabstop=4: */
/**
* RC4Crypt 3.2
*
* RC4Crypt is a petite library that allows you to use RC4
* encryption easily in PHP. It's OO and can produce outputs
* in binary and hex.
*
* (C) Copyright 2006 Mukul Sabharwal [http://mjsabby.com]
* All Rights Reserved
*
* @link http://rc4crypt.devhome.org
* @author Mukul Sabharwal <mjsabby@gmail.com>
* @version $Id: class.rc4crypt.php,v 3.2 2006/03/10 05:47:24 mukul Exp $
* @copyright Copyright © 2006 Mukul Sabharwal
* @license http://www.gnu.org/copyleft/gpl.html
* @package RC4Crypt
*/
/**
* RC4 Class
* @package RC4Crypt
*/
class rc4crypt {
/**
* The symmetric encryption function
*
* @param string $pwd Key to encrypt with (can be binary of hex)
* @param string $data Content to be encrypted
* @param bool $ispwdHex Key passed is in hexadecimal or not
* @access public
* @return string
*/
function rc4 ($pwd, $data, $ispwdHex = 0)
{
if ($pwd=='') // if no password specified
$pwd = 'password'; //use this simple one, only for altering the data
if ($ispwdHex)
$pwd = @pack('H*', $pwd); // valid input, please!
$key[] = '';
$box[] = '';
$cipher = '';
$pwd_length = strlen($pwd);
$data_length = strlen($data);
for ($i = 0; $i < 256; $i++)
{
$key[$i] = ord($pwd[$i % $pwd_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i < 256; $i++)
{
$j = ($j + $box[$i] + $key[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i < $data_length; $i++)
{
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$k = $box[(($box[$a] + $box[$j]) % 256)];
$cipher .= chr(ord($data[$i]) ^ $k);
}
return $cipher;
}
/**
* Decryption, recall encryption
*
* @param string $pwd Key to decrypt with (can be binary of hex)
* @param string $data Content to be decrypted (alphabetically encoded)
* @param bool $ispwdHex Key passed is in hexadecimal or not
* @access public
* @return string
*/
function decrypt ($pwd, $data, $ispwdHex = 0)
{
//convert ascii to binary
$binary = ''; //binary string
$length = strlen($data);
for ($i=0; $i<$length; $i+=2)
{
$x1=ord($data[$i])-65;
$x2=ord($data[$i+1])-65;
$binary .= chr($x1 + ($x2 << 4));
}
//decrypt binary data
$data = rc4crypt::rc4($pwd, $binary, $ispwdHex);
return $data;
}
function encrypt ($pwd, $data, $ispwdHex = 0)
{
//encrypt binary data
$data = rc4crypt::rc4($pwd, $data, $ispwdHex);
//convert binary to ascii (chars 65-80)
$length = strlen($data);
$alpha = ''; //alpha string
for ($i = 0; $i < $length; $i++)
{
$x = ord($data[$i]);
$x1 = $x & 0x0f;
$x2 = ($x & 0xf0) >> 4;
$alpha .= chr($x1+65).chr($x2+65);
}
return $alpha;
}
}
?>