-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryDecryString.php
60 lines (54 loc) · 1.49 KB
/
EncryDecryString.php
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
<?php
/**
* @author Sachin kumar
*/
// Check error if exists
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class EncryDecryString {
public function __construct()
{
$this->key = '124645';
}
public function encrypt($plain_string)
{
$iv = mcrypt_create_iv(
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC),
MCRYPT_DEV_URANDOM
);
$encrypted = base64_encode(
$iv .
mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
hash('sha256', $this->key, true),
$plain_string,
MCRYPT_MODE_CBC,
$iv
)
);
return strtr($encrypted, '+/', '-_');
}
public function decrypt($encrypted_string)
{
$data = str_pad(strtr($encrypted_string, '-_', '+/'), strlen($encrypted_string) % 4, '=', STR_PAD_RIGHT);
$data = base64_decode($data);
$iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
$decrypted = rtrim(
mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
hash('sha256', $this->key, true),
substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)),
MCRYPT_MODE_CBC,
$iv
),
"\0"
);
// return
return $decrypted;
}
}
// Use the above library.
$new = new EncryDecryString();
echo $encrypted_string = $new->encrypt("sachin");
echo $new->decrypt($encrypted_string);