从mcrypt到OpenSSL

mcrypt扩展已经大约10年之久了,从php7.1+以后它将被从核心代码中移除并且移到PECL中。

直接会被OpenSSL所取代,程序宝宝再也不用担心AES、DES等加密,具体请参考php手册OpenSSL

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
/**
* @desc OpenSSL加密算法
*/
class OpenSSL {
private static $key = "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3";
/**
* @desc 验证获取加密算法方式
*/
private static function getMethods($cipher=null){
if ($cipher && in_array($cipher, openssl_get_cipher_methods())) {
return $cipher;
} else {
/**
* 为空返回 AES-128-ECB 加密算法方式
*/
$cipher = openssl_get_cipher_methods();
return $cipher[5];
}
}
/**
* @desc 数据加密
*/
public static function encrypted($data, $key=null, $cipher=null){
if (empty($data)) {
return false;
}
if (empty($cipher)) {
$cipher = self::getMethods();
}
//初始化向量
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
//加密 - 版本判断
if(phpversion() >= '7.1.0'){
$ciphertext = openssl_encrypt($data, $cipher, $key, $options=0, $iv);
} else {
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
}
return $ciphertext;
}
/**
* @desc 数据解密
*/
public static function decrypted($data, $key=null, $cipher=null){
if (empty($data)) {
return false;
}
if (empty($cipher)) {
$cipher = self::getMethods();
}
//初始化向量
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
//解密 - 版本判断
if(phpversion() >= '7.1.0'){
$plaintext = openssl_decrypt($data, $cipher, $key, $options=0, $iv);
} else {
$plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
}
return $plaintext;
}
}