-
Notifications
You must be signed in to change notification settings - Fork 0
/
AES.php
39 lines (38 loc) · 1.33 KB
/
AES.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
<?php
//AES 加密算法
function encrypt($input, $key) {
$key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); //获得加密算法的分组大小
//var_dump($size);exit;
$input = pkcs5_pad($input, $size);
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); //打开算法和模式对应的模块
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); //初始化向量
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function decrypt($sStr, $sKey) {
$key = substr(openssl_digest(openssl_digest($sKey, 'sha1', true), 'sha1', true), 0, 16);
$decrypted= mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$key,
base64_decode($sStr),
MCRYPT_MODE_ECB
);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s-1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
$str="xxxxxxxxxxxx";
$key="gTrUYb11111";
$en=encrypt($str,$key);
$de=decrypt('e99T663RP77lCYhAQKrwD9mjXxYj4J+ZPSANfKBnL6M=',$key);
//var_dump($en,$de);exit;