-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryption.php
More file actions
64 lines (59 loc) · 1.82 KB
/
Encryption.php
File metadata and controls
64 lines (59 loc) · 1.82 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
<?php
/**
* Class Encryption
*
* PHP Wrapper class for AES-256-CBC OpenSSL string encryption.
*
* Purpose of the class is to encrypt strings of potentially sensitive data
* for storage and provide a simple way to decrypt.
*
* Requires openssl_encrypt and openssl_decrypt libraries to be installed
* for PHP. Allows encryption and decryption for AES 256 CBC.
*
* @author krabello 4<kevin.rabello@gmail.com>
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version 1.0
* @copyright (c) 2017, Kevin Rabello
*/
class Encryption
{
/**
* @var string cipher
*/
private static $method = 'aes-256-cbc';
/**
* encrypt
*
* returns a base 64 encoded encrypted string
*
* @param string $data
* @param string $key
*
* @return string encrypted string
*/
public static function encrypt($data, $key)
{
// Generate an initialization vector
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::$method));
// Encrypt the data using AES 256 encryption in CBC mode using our
// encryption key and initialization vector.
$encrypted = openssl_encrypt($data, self::$method, base64_decode($key), 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
/**
* decrypt
*
* returns a decrypted string
*
* @param string $data
* @param string $key
*
* @return string decrypted string
*/
public static function decrypt($data, $key)
{
// To decrypt, split the encrypted data from our IV - our unique separator used was "::"
list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
return openssl_decrypt($encrypted_data, self::$method, base64_decode($key), 0, $iv);
}
}