-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHPCrypt.class.php
More file actions
145 lines (125 loc) · 4.62 KB
/
PHPCrypt.class.php
File metadata and controls
145 lines (125 loc) · 4.62 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
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
<?PHP
class PHPCrypt {
private static $defaultKey = "e8938fbd4e202a5416bbae425eddb0ee56038327e2a5f919db5b2e6e4845c9e4";
private static $brokenAlgorithms = array('arcfour','wake','enigma');
private static $brokenModes = array('stream');
private static $defaultAlgorithm = "rijndael-256";
private static $defaultMode = "cbc";
private $algorithms = array();
private $modes = array();
private $cryptkey;
private $cryptAlgorithm;
private $cryptMode;
private $cryptIv;
public function __construct($options = NULL, $useKeyForIv = true) {
try {
if (function_exists("mcrypt_list_algorithms")) {
$this->algorithms = mcrypt_list_algorithms();
$this->modes = mcrypt_list_modes();
} else {
throw new Exception("Class Required function 'mcrypt_list_algorithms' does not exist. Please install the mcrypt library.");
}
if ($options != NULL) {
if (!is_array($options)) {
throw new Exception("Options passed must be an array.");
} else {
if (isset($options['algorithm'])) {
$algo = $options['algorithm'];
if (in_array($algo, $this->algorithms)) {
if (!in_array($algo, PHPCrypt::$brokenAlgorithms)) {
$this->cryptAlgorithm = $algo;
} else {
throw new Exception("Selected Crypt Algorithm $algo, currently not supported.");
}
} else {
throw new Exception("Invalid Crypt Algorithm $algo Specified.");
}
} else {
$this->cryptAlgorithm = PHPCrypt::$defaultAlgorithm;
}
if (isset($options['mode'])) {
$mode = $options['mode'];
if (in_array($mode, $this->modes)) {
if (!in_array($mode, PHPCrypt::$brokenModes)) {
$this->cryptMode = $mode;
} else {
throw new Exception("Selected Crypt Mode $mode, currently not supported");
}
} else {
throw new Exception("Invalid Crypt Mode $mode Specified.");
}
} else {
$this->cryptMode = PHPCrypt::$defaultMode;
}
if (isset($options['key'])) {
$this->cryptkey = $options['key'];
} else {
$this->cryptkey = PHPCrypt::$defaultKey;
}
}
} else {
$this->cryptkey = PHPCrypt::$defaultKey;
$this->cryptAlgorithm = PHPCrypt::$defaultAlgorithm;
$this->cryptMode = PHPCrypt::$defaultMode;
}
} catch (Exception $e) {
throw new Exception("Trouble instantiating PHPCrypt [" . $e->getMessage() . "]");
self::__destruct();
}
if ($useKeyForIv == true) {
$this->cryptIv = $this->generate_iv_from_key();
} else {
$this->cryptIv = mcrypt_create_iv(mcrypt_get_iv_size($this->cryptAlgorithm, $this->cryptMode), MCRYPT_DEV_URANDOM);
}
}
private function generate_iv_from_key() {
$ivSize = mcrypt_get_iv_size($this->cryptAlgorithm, $this->cryptMode);
$iv = '';
$cryptKeyHash = md5(md5($this->cryptkey));
for ($ivLoop = 0; $ivLoop < $ivSize; $ivLoop++) {
$iv .= $cryptKeyHash[$ivLoop];
}
return $iv;
}
public function get_iv() {
return $this->cryptIv;
}
public function set_iv($iv) {
$ivSize = mcrypt_get_iv_size($this->cryptAlgorithm, $this->cryptMode);
if (strlen($iv) <= $ivSize) {
$this->cryptIv = $iv;
return true;
}
return false;
}
public function show_algorithms() {
return $this->algorithms;
}
public function show_modes() {
return $this->modes;
}
public function encrypt($stringToBeCrypted) {
$maxCipherKeySize = mcrypt_get_key_size($this->cryptAlgorithm, $this->cryptMode);
if (strlen(md5($this->cryptkey)) > $maxCipherKeySize) {
$cipherKey = substr(md5($this->cryptkey),0,$maxCipherKeySize);
} else {
$cipherKey = md5($this->cryptkey);
}
$encryptedString = base64_encode(mcrypt_encrypt($this->cryptAlgorithm, $cipherKey, $stringToBeCrypted, $this->cryptMode, $this->cryptIv ));
return $encryptedString;
}
public function decrypt($encryptedString) {
$maxCipherKeySize = mcrypt_get_key_size($this->cryptAlgorithm, $this->cryptMode);
if (strlen(md5($this->cryptkey)) > $maxCipherKeySize) {
$cipherKey = substr(md5($this->cryptkey),0,$maxCipherKeySize);
} else {
$cipherKey = md5($this->cryptkey);
}
$decryptedString = rtrim(mcrypt_decrypt($this->cryptAlgorithm, $cipherKey, base64_decode($encryptedString), $this->cryptMode, $this->cryptIv), "\0");
return $decryptedString;
}
public function __destruct() {
unset($this);
}
}
?>