加密算法分析"/>
PHP加密算法分析
加密定义
在密码学中,加密(英语:Encryption)是将明文信息改变为难以读取的密文内容,使之不可读的过程。只有拥有解密方法的对象,经由解密过程,才能将密文还原为正常可读的内容。
加密技术的重点是加密算法,加密算法主要分为三类:
- 对称加密
- 非对称加密
- 不可逆加密
对称加密算法
1. 加密过程
- 将明文分成N个组,然后对各个组进行加密,形成各自的密文,最后把所有的分组密文进行合并,形成最终的密文。
2. 优点
- 算法公开、计算量小、加密速度快、加密效率高
3.缺点
- 交易双方都使用同样钥匙,安全性得不到保证
- 密钥管理困难,尤其是在分布式网络中
4.常用算法
- DES、3DES(TripleDES)、AES、RC2、RC4、RC5和Blowfish
PHP中对称加密算法
$mode_list = mcrypt_list_modes();//mcrypt支持的加密模式列表// print_r($cipher_list);
// print_r($mode_list);function encrypt($key,$data){$td = mcrypt_module_open("des", "", "ecb", "");//使用MCRYPT_DES算法,ecb模式$size = mcrypt_enc_get_iv_size($td); //设置初始向量的大小$iv = mcrypt_create_iv($size,MCRYPT_RAND); //创建初始向量$key_size = mcrypt_enc_get_key_size($td); //返回所支持的最大的密钥长度(以字节计算)$salt = '';$subkey = substr(md5(md5($key).$salt), 0,$key_size);//对key复杂处理,并设置长度mcrypt_generic_init($td, $subkey, $iv);$endata = mcrypt_generic($td, $data);mcrypt_generic_deinit($td);mcrypt_module_close($td);return $endata;
}function decrypt($key,$endata){$td = mcrypt_module_open("des", "", "ecb", "");//使用MCRYPT_DES算法,ecb模式$size = mcrypt_enc_get_iv_size($td); //设置初始向量的大小$iv = mcrypt_create_iv($size,MCRYPT_RAND); //创建初始向量$key_size = mcrypt_enc_get_key_size($td); //返回所支持的最大的密钥长度(以字节计算)$salt = '';$subkey = substr(md5(md5($key).$salt), 0,$key_size);//对key复杂处理,并设置长度mcrypt_generic_init($td, $subkey, $iv);$data = rtrim(mdecrypt_generic($td, $endata)).'\n';mcrypt_generic_deinit($td);mcrypt_module_close($td);return $data;
}$key = "www.tencent";
// $data = "返回所支持的最大的密钥长度(涉及到发件费啦";
$data = "dadfafdafd,我是一个好孩子";$endata = encrypt($key,$data);$data1 = decrypt($key,$endata);echo $endata; //直接输出,在网页上是乱码,用base64_encode处理,就变成由字符、数组、加号、斜杠等共64种字符注册
echo base64_encode($endata);
echo $data1;
非对称加密算法
1. 使用过程
- 乙方生成两把密钥(公钥和私钥)
- 甲方获取乙方的公钥,然后用它对信息加密。
- 乙方得到加密后的信息,用私钥解密,乙方也可用私钥加密字符串
- 甲方获取乙方私钥加密数据,用公钥解密
2. 优点
- 更安全,密钥越长,它就越难破解
3. 缺点
- 加密速度慢
4. 常用算法
- RSA、Elgamal、背包算法、Rabin、D-H、ECC(椭圆曲线加密算法) ####RSA算法
/*** 使用openssl实现非对称加密*/
class Rsa {/*** private key*/private $_privKey;/*** public key*/private $_pubKey;/*** the keys saving path*/private $_keyPath;/*** the construtor,the param $path is the keys saving path*/public function __construct($path) {if (empty($path) || !is_dir($path)) {throw new Exception('Must set the keys save path');}$this->_keyPath = $path;}/*** create the key pair,save the key to $this->_keyPath* 也可以使用openssl命令生成公钥私钥* openssl genrsa -out rsa_private_key.pem 1024* openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem*/public function createKey() {$r = openssl_pkey_new();openssl_pkey_export($r, $privKey);file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);$this->_privKey = openssl_pkey_get_public($privKey);$rp = openssl_pkey_get_details($r);$pubKey = $rp['key'];file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey);$this->_pubKey = openssl_pkey_get_public($pubKey);}/*** setup the private key*/public function setupPrivKey() {if (is_resource($this->_privKey)) {return true;}$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';$prk = file_get_contents($file);$this->_privKey = openssl_pkey_get_private($prk);return true;}/*** setup the public key*/public function setupPubKey() {if (is_resource($this->_pubKey)) {return true;}$file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key';$puk = file_get_contents($file);$this->_pubKey = openssl_pkey_get_public($puk);return true;}/*** encrypt with the private key*/public function privEncrypt($data) {if (!is_string($data)) {return null;}$this->setupPrivKey();$r = openssl_private_encrypt($data, $encrypted, $this->_privKey);if ($r) {return base64_encode($encrypted);}return null;}/*** decrypt with the private key*/public function privDecrypt($encrypted) {if (!is_string($encrypted)) {return null;}$this->setupPrivKey();$encrypted = base64_decode($encrypted);$r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);if ($r) {return $decrypted;}return null;}/*** encrypt with public key*/public function pubEncrypt($data) {if (!is_string($data)) {return null;}$this->setupPubKey();$r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);if ($r) {return base64_encode($encrypted);}return null;}/*** decrypt with the public key*/public function pubDecrypt($crypted) {if (!is_string($crypted)) {return null;}$this->setupPubKey();$crypted = base64_decode($crypted);$r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);if ($r) {return $decrypted;}return null;}public function __destruct() {@fclose($this->_privKey);@fclose($this->_pubKey);}
}
//以下是一个简单的测试demo,如果不需要请删除
$rsa = new Rsa('ssl-key');
//私钥加密,公钥解密
echo 'source:我是老鳖<br />';
$pre = $rsa->privEncrypt('我是老鳖');
echo 'private encrypted:<br />' . $pre . '<br />';
$pud = $rsa->pubDecrypt($pre);
echo 'public decrypted:' . $pud . '<br />';
//公钥加密,私钥解密
echo 'source:干IT的<br />';
$pue = $rsa->pubEncrypt('干IT的');
echo 'public encrypt:<br />' . $pue . '<br />';
$prd = $rsa->privDecrypt($pue);
echo 'private decrypt:' . $prd;
?>
不可逆加密算法
加密过程中不需要使用密钥,输入明文后由系统直接经过加密算法处理成密文,这种加密后的数据是无法被解密的,只有重新输入明文,并再次经过同样不可逆的加密算法处理,得到相同的加密密文并被系统重新识别后,才能真正解密。 常用算法有 md5, crypt,sha1
1. md5
$data = 'hello';
echo md5($data); //输出32位的16进制 5d41402abc4b2a76b9719d911017c592
2. crypt
<?php
if (CRYPT_STD_DES == 1) {echo 'Standard DES: ' . crypt('rasmuslerdorf', 'rl') . "\n";
}if (CRYPT_EXT_DES == 1) {echo 'Extended DES: ' . crypt('rasmuslerdorf', '_J9..rasm') . "\n";
}if (CRYPT_MD5 == 1) {echo 'MD5: ' . crypt('rasmuslerdorf', '$1$rasmusle$') . "\n";
}if (CRYPT_BLOWFISH == 1) {echo 'Blowfish: ' . crypt('rasmuslerdorf', '$2a$07$usesomesillystringforsalt$') . "\n";
}if (CRYPT_SHA256 == 1) {echo 'SHA-256: ' . crypt('rasmuslerdorf', '$5$rounds=5000$usesomesillystringforsalt$') . "\n";
}if (CRYPT_SHA512 == 1) {echo 'SHA-512: ' . crypt('rasmuslerdorf', '$6$rounds=5000$usesomesillystringforsalt$') . "\n";
}
?>以上输出
Standard DES: rl.3StKT.4T8M
Extended DES: _J9..rasmBYk8r9AiWNc
MD5: $1$rasmusle$rISCgZzpwk3UhDidwXvin0
Blowfish: $2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi
SHA-256: $5$rounds=5000$usesomesillystri$KqJWpanXZHKq2BOB43TSaYhEWsQ1Lr5QNyPCDH/Tp.6
SHA-512: $6$rounds=5000$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21
3. sha1
$data="hello";
echo sha1($data); // aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
//当然,可以将多种加密算法混合使用
echo md5(sha1($data));
//输出:e69d7e620e82be5eb414d1f8d1d4b9d9
//这种方式的双重加密也可以提高数据的安全性
更多推荐
PHP加密算法分析
发布评论