-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEncryption.php
48 lines (41 loc) · 1.24 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
48
<?php
class Encryption {
private $key;
private $blocksize;
public function __construct($key) {
$this->key = $key;
$this->blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
}
public function encrypt_aes_ecb_pkcs5($payload) {
$payload = self::pkcs5_pad($payload, $this->blocksize);
$encrypted = mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$this->key,
$payload,
MCRYPT_MODE_ECB
);
return self::base64url_encode($encrypted);
}
// From http://php.net/manual/en/ref.mcrypt.php#69782
public static function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
// From http://php.net/manual/en/ref.mcrypt.php#69782
public static function pkcs5_unpad($text) {
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) {
return false;
}
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
return false;
}
return substr($text, 0, -1 * $pad);
}
public static function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
public static function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
}