-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypt.php
More file actions
80 lines (75 loc) · 1.61 KB
/
Crypt.php
File metadata and controls
80 lines (75 loc) · 1.61 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
<?php
/**
* M PHP Framework
* @package M
* @subpackage Crypt
*/
/**
* M PHP Framework
*
*
* @package M
* @subpackage Crypt
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
* Strong encryption/decryption static methods
* Dependency : PEAR_Crypt_Blowfish
*/
class M_Crypt
{
/**
* Encrypt data
*
* @access public
* @static
* @param string $val Data to encrypt
* @param string $ky Key
* @return string Encrypted data
*/
public static function encrypt($val,$ky = null,$meth='cbc' )
{
if(is_null($ky)) {
$ky = ENCSALT;
}
if(empty($val)) return '';
$bf =& Crypt_Blowfish::factory($meth);
if (PEAR::isError($bf)) {
throw new Exception($bf->getMessage());
}
$iv = 'abc123+=';
$bf->setKey($ky, $iv);
$encrypted = $bf->encrypt($val);
return base64_encode($encrypted);
}
/**
* Encrypt data
*
* @access public
* @static
* @param string $val Data to encrypt
* @param string $ky Key
* @return string Encrypted data
*/
public static function decrypt( $val, $ky = null,$meth='cbc' )
{
if(is_null($ky)) {
$ky = ENCSALT;
}
if(empty($val)) return '';
$val = base64_decode($val);
$bf =& Crypt_Blowfish::factory($meth);
if (PEAR::isError($bf)) {
throw new Exception($bf->getMessage());
}
$iv = 'abc123+=';
$bf->setKey($ky, $iv);
$plaintext = $bf->decrypt($val);
if (PEAR::isError($plaintext)) {
throw new Exception('decoding error : '.$plaintext->getMessage());
}
return trim($plaintext);
}
}