-
Notifications
You must be signed in to change notification settings - Fork 1
/
Opensslencryptdecrypt.php
65 lines (46 loc) · 1.68 KB
/
Opensslencryptdecrypt.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
61
62
63
64
65
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* simple method to encrypt or decrypt a plain text string
* initialization vector(IV) has to be the same when encrypting and decrypting
*
* @param string $string: string to encrypt or decrypt
*
* @return string
*
*/
class Opensslencryptdecrypt {
public $encrypt_method = "AES-256-CBC";
//these are test keys, you can be generate one at https://asecuritysite.com/encryption/keygen
public $secret_key = 'EF61FE21EB90047086DED9A5386A8808'; //This is my secret key
public $secret_iv = 'F148EB0B9C6A3E5B16A03FCBE949BAE8'; //This is my secret iv
public $output = false;
function encrypt($string) {
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
return $output;
}
function decrypt($string) {
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
return $output;
}
public function test() {
$plain_txt = "This is my plain text";
echo "Plain Text =" . $plain_txt . "\n";
$encrypted_txt = $this -> encrypt($plain_txt);
echo "Encrypted Text = " . $encrypted_txt . "\n";
$decrypted_txt = $this -> decrypt($encrypted_txt);
echo "Decrypted Text =" . $decrypted_txt . "\n";
echo $plain_txt === $decrypted_txt ? "SUCCESS" : "FAILED";
echo "\n";
}
}
?>