-
Notifications
You must be signed in to change notification settings - Fork 4
/
Encryption.php
47 lines (40 loc) · 1.26 KB
/
Encryption.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
<?php
/**
Name: PHP Encryption:
Desc: Encrypt and Decrypt data.
Version: V1.0
Date: 3/11/2017
Author: Prince Adeyemi
Contact: [email protected]
Facebook: fb.com/YourVegasPrince
*/
// Encryption setup:
$dataToEncrypt = "My Secret Telegram: Hakuna matata!";
$encryptMethod = "AES-256-CBC";
$mySecretHash = "somethingRandom12!)!)I!@1U2 goes gere";
//$iv = mcrypt_create_iv(16, MCRYPT_RAND);
// php versopm 5.6.0- 7.2.x compability;
If( phpversion() >= 7)
{
$bytes = random_bytes(8);
$iv = (bin2hex($bytes));
}
elseif( phpversion() < 7 )
{
for ($i = -1; $i <= 16; $i++) {
$iv = openssl_random_pseudo_bytes($i, $cstrong);
}
}
else {
$iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM);
}
//Encrypt data:
$encryptedData = openssl_encrypt($dataToEncrypt,$encryptMethod,$mySecretHash, 0, $iv);
//Decrypt data:
$decryptedData = openssl_decrypt($encryptedData, $encryptMethod, $mySecretHash, 0, $iv);
//OUTPUT:
echo ("RandomIV : " . $iv . "\n");
echo ( "Secret message : " . $dataToEncrypt ."\n" );
echo ( "Encrypted message : ". $encryptedData ."\n" );
echo ( "Decrypted message: ". $decryptedData ."\ln");
?>