-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption-example.php
More file actions
40 lines (25 loc) · 903 Bytes
/
encryption-example.php
File metadata and controls
40 lines (25 loc) · 903 Bytes
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
<?php
namespace Schluss\Tools;
include ('tools/encryption.php');
$password = 'aP@ssw@rd';
$data = 'encrypt me';
// ENCRYPTION:
// generate (iv/)salt for encrypting the url and additional data
$key_salt = Encryption::iv(16); // note this needs to be stored next to the encrypted data because it's needed for decryption
// generate derived key from given password
$key = Encryption::pbkdf2($password, $key_salt);
// generate salt for the encrypted data
$data_salt = Encryption::iv(16);
// encrypt the data
$encrypted_data = Encryption::encrypt($data, $key, $data_salt);
//echo $encrypted_data;
// what to store in db:
// $encrypted_data
// $key_salt
// DECRYPTION:
// (re)generate derived key from given password
$key = Encryption::pbkdf2($password, $key_salt);
// decrypt the data
$decrypted_data = Encryption::decrypt($encrypted_data, $key);
// tada, it's back:)
echo $decrypted_data;