forked from DrHazemAli/PHP-Class-Encryption-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.php
More file actions
121 lines (102 loc) · 3.71 KB
/
Encryption.php
File metadata and controls
121 lines (102 loc) · 3.71 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
<?php
/**
* Encryption Algorithms Class
* Written By : Dr. Hazem Ali
* https://www.fb.com/Haz4m
* Date: 01/12/2016
* Time: 5:00 AM
* License : GNU GENERAL PUBLIC LICENSE
*/
Class Encryption
{
// DECLARE THE REQUIRED VARIABLES
public $ENC_METHOD = "AES-256-CBC"; // THE ENCRYPTION METHOD.
public $ENC_KEY = "SOME_RANDOM_KEY"; // ENCRYPTION KEY
public $ENC_IV = "SOME_RANDOM_IV"; // ENCRYPTION IV.
public $ENC_SALT = "xS$"; // THE SALT FOR PASSWORD ENCRYPTION ONLY.
// DECLARE REQUIRED VARIABLES TO CLASS CONSTRUCTOR
function __construct($METHOD = NULL, $KEY = NULL, $IV = NULL, $SALT = NULL)
{
try
{
// Setting up the Encryption Method when needed.
$this->ENC_METHOD = (isset($METHOD) && !empty($METHOD) && $METHOD != NULL) ?
$METHOD : $this->ENC_METHOD;
// Setting up the Encryption Key when needed.
$this->ENC_KEY = (isset($KEY) && !empty($KEY) && $KEY != NULL) ?
$KEY : $this->ENC_KEY;
// Setting up the Encryption IV when needed.
$this->ENC_IV = (isset($IV) && !empty($IV) && $IV != NULL) ?
$IV : $this->ENC_IV;
// Setting up the Encryption IV when needed.
$this->ENC_SALT = (isset($SALT) && !empty($SALT) && $SALT != NULL) ?
$SALT : $this->ENC_SALT;
}
catch (Exception $e)
{
return "Caught exception: ".$e->getMessage();
}
}
// THIS FUNCTION WILL ENCRYPT THE PASSED STRING
public function Encrypt($string)
{
try
{
$output = false;
$key = hash('sha256', $this->ENC_KEY);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $this->ENC_IV), 0, 16);
$output = openssl_encrypt($string, $this->ENC_METHOD, $key, 0, $iv);
$output = base64_encode($output);
return $output;
}
catch (Exception $e)
{
return "Caught exception: ".$e->getMessage();
}
}
// THIS FUNCTION WILL DECRYPT THE ENCRYPTED STRING.
public function Decrypt($string)
{
try
{
$output = false;
// hash
$key = hash('sha256', $this->ENC_KEY);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $this->ENC_IV), 0, 16);
$output = openssl_decrypt(base64_decode($string), $this->ENC_METHOD, $key, 0, $iv);
return $output;
}
catch (Exception $e)
{
return "Caught exception: ".$e->getMessage();
}
}
// THIS FUNCTION FOR PASSWORDS ONLY, BECAUSE IT CANNOT BE DECRYPTED IN FUTURE.
public function EncryptPassword($Input)
{
try
{
if (!isset($Input) || $Input == null || empty($Input)) { return false;}
// GENERATE AN ENCRYPTED PASSWORD SALT
$SALT = $this->Encrypt($this->ENC_SALT);
$SALT = md5($SALT);
// PERFORM MD5 ENCRYPTION ON PASSWORD SALT.
// ENCRYPT PASSWORD
$Input = md5($this->Encrypt(md5($Input)));
$Input = $this->Encrypt($Input);
$Input = md5($Input);
// PERFORM ANOTHER ENCRYPTION FOR THE ENCRYPTED PASSWORD + SALT.
$Encrypted = $this->Encrypt($SALT).$this->Encrypt($Input);
$Encrypted = sha1($Encrypted.$SALT);
// RETURN THE ENCRYPTED PASSWORD AS MD5
return md5($Encrypted);
}
catch (Exception $e)
{
return "Caught exception: ".$e->getMessage();
}
}
}
?>