-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes128encrypter.php
82 lines (72 loc) · 2.6 KB
/
aes128encrypter.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
Requirements:
- a initial string from which I create a key ($string)
- for decryption:
- given a base64 encoded string to be decrypted -> return clear text
- for encryption:
- given a clear Text -> return a base64 encoded string
- every object is instanziated by the initial string:
var encObj = new sswEncrypter($string);
var aClearText = encObj.decrypt(base64encodedString);
var aBase64EncodedString = encObj.encrypt(aClearText);
*/
class AES128encrypter
{
public $key;
function __construct($string)
{
$this->key = pack('H*',md5($string));
}
private function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
if($pad == 0) {
$pad = $blocksize;
}
return $text . str_repeat(chr($pad), $pad);
}
function encrypt($clearText,$ivStr = NULL)
{
$key = $this->key;
$key_size = strlen($key);
// text has to be divisible by block size, thus a possibile padding is added
$plaintext = $this->pkcs5_pad($clearText,mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
if($ivStr != NULL) {
for($i = 0; $i<$iv_size && $i<strlen($ivStr); $i++) {
$iv[$i] = $ivStr[$i];
}
}
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
$plaintext, MCRYPT_MODE_CBC, $iv);
# prepend the IV for it to be available for decryption
$ciphertext = $iv . $ciphertext;
$ciphertext_base64 = base64_encode($ciphertext);
return $ciphertext_base64;
}
function decrypt($base64EncodedStr)
{
$sKey = $this->key;
$ciphertext_dec = base64_decode($base64EncodedStr);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
# retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
$iv_dec = substr($ciphertext_dec, 0, $iv_size);
# retrieves the cipher text (everything except the $iv_size in the front)
$ciphertext_dec = substr($ciphertext_dec, $iv_size);
$plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $sKey,
$ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);
// and now remove the padding (possible)
$dec_s = strlen($plaintext_dec);
$padding = ord($plaintext_dec[$dec_s-1]);
$plaintext_dec = substr($plaintext_dec, 0, -$padding);
return $plaintext_dec;
}
function getIV($base64EncodedStr) {
$ciphertext_dec = base64_decode($base64EncodedStr);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
# retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
$iv_dec = substr($ciphertext_dec, 0, $iv_size);
return $iv_dec;
}
}