DES是一种标准的数据加密算法,关于这个算法的详细介绍可以参考wiki和百度百科:
php中有一个扩展可以支持DES的加密算法,是:extension=php_mcrypt.dll
在配置文件中将这个扩展打开还不能够在windows环境下使用
需要将PHP文件夹下的 libmcrypt.dll 拷贝到系统的 system32 目录下,这是通过phpinfo可以查看到mcrypt表示这个模块可以正常试用了。
下面是PHP中使用DES加密解密的一个例子:
1 //$input - stuff to decrypt
2
3 //$key - the secret key to use
4
5
6
7 function do_mencrypt($input, $key)
8
9 {
10
11 $input = str_replace(""n", "", $input);
12
13 $input = str_replace(""t", "", $input);
14
15 $input = str_replace(""r", "", $input);
16
17 $key = substr(md5($key), 0, 24);
18
19 $td = mcrypt_module_open('tripledes', '', 'ecb', '');
20
21 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
22
23 mcrypt_generic_init($td, $key, $iv);
24
25 $encrypted_data = mcrypt_generic($td, $input);
26
27 mcrypt_generic_deinit($td);
28
29 mcrypt_module_close($td);
30
31 return trim(chop(base64_encode($encrypted_data)));
32
33 }
34
35
36
37 //$input - stuff to decrypt
38
39 //$key - the secret key to use
40
41
42
43 function do_mdecrypt($input, $key)
44
45 {
46
47 $input = str_replace(""n", "", $input);
48
49 $input = str_replace(""t", "", $input);
50
51 $input = str_replace(""r", "", $input);
52
53 $input = trim(chop(base64_decode($input)));
54
55 $td = mcrypt_module_open('tripledes', '', 'ecb', '');
56
57 $key = substr(md5($key), 0, 24);
58
59 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
60
61 mcrypt_generic_init($td, $key, $iv);
62
63 $decrypted_data = mdecrypt_generic($td, $input);
64
65 mcrypt_generic_deinit($td);
66
67 mcrypt_module_close($td);
68
69 return trim(chop($decrypted_data));
70
71
72 }