网站首页php
PHP程序中常用到的对称加密算法
发布时间:2015-11-04 00:46:55编辑:阅读(4502)
对称加密算法,可以使用同一个函数加解密。
一、discuz经典算法
<?php /** * $string:需要加解密的字符串 * $operation: 执行的操作,DECODE解密,ENCODE加密 * $key: 密钥 * $expiry: 设置过期时间戳 * */ define('AUTH_KEY', '33ssa6d5f4Xzhf&797d4f.g$t9sd6f5e'); function authcode($string, $operation = 'DECODE', $key = '', $设置 = 0) { // 动态密匙长度,相同的明文会生成不同密文就是依靠动态密匙 $ckey_length = 4; // 密匙 $key = md5($key ? $key : AUTH_KEY); // 密匙a会参与加解密 $keya = md5(substr($key, 0, 16)); // 密匙b会用来做数据完整性验证 $keyb = md5(substr($key, 16, 16)); // 密匙c用于变化生成的密文 $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; // 参与运算的密匙 $cryptkey = $keya.md5($keya.$keyc); $key_length = strlen($cryptkey); // 明文,前10位用来保存时间戳,解密时验证数据有效性,10到26位用来保存$keyb(密匙b), //解密时会通过这个密匙验证数据完整性 // 如果是解码的话,会从第$ckey_length位开始,因为密文前$ckey_length位保存 动态密匙,以保证解密正确 $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string; $string_length = strlen($string); $result = ''; $box = range(0, 255); $rndkey = array(); // 产生密匙簿 for($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } // 用固定的算法,打乱密匙簿,增加随机性,好像很复杂,实际上对并不会增加密文的强度 for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } // 核心加解密部分 for($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; // 从密匙簿得出密匙进行异或,再转成字符 $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation == 'DECODE') { // 验证数据有效性,请看未加密明文的格式 if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { return substr($result, 26); } else { return ''; } } else { // 把动态密匙保存在密文里,这也是为什么同样的明文,生产不同密文后能解密的原因 // 因为加密后的密文可能是一些特殊字符,复制过程可能会丢失,所以用base64编码 return $keyc.str_replace('=', '', base64_encode($result)); } }
二、常用encrypt加解密函数
<?php /** * 加密/解密字符串 * * @param string $string 原始字符串 * @param string $operation 操作选项: DECODE:解密;其它为加密 * @param string $key 密钥 * * @return string $result 处理加/解密后的字符串 */ define('AUTH_KEY', '33ssa6d5f4Xzhf&797d4f.g$t9sd6f5e'); function authcode($string, $operation, $key = '') { $key = md5($key ? $key : AUTH_KEY); $key_length = strlen($key); $string = $operation == 'DECODE' ? base64decode($string) : substr(md5($string.$key), 0, 8).$string; $string_length = strlen($string); $rndkey = $box = array(); $result = ''; for($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($key[$i % $key_length]); $box[$i] = $i; } for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for($a = $j = $i = 0; $i < $string_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation == 'DECODE') { if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) { return substr($result, 8); } else { return ''; } } else { return str_replace('=', '', base64_encode($result)); } }
------------------------------我是分割线-------------------------------
三、使用openssl的AES256方式加密解密, $method可以通过openssl_get_cipher_methods方法获取有146种之多.
<?php define('ENCRYPT_KEY', 'as6d5f1we6t87we'); define('ENCRYPT_IV', '97d4f.g$t9sd6f5e'); function encode($data){ $method = "AES-256-CBC"; return base64_encode(openssl_encrypt($data, $method, ENCRYPT_KEY, OPENSSL_RAW_DATA , ENCRYPT_IV)); } function decode($data){ $method = "AES-256-CBC"; return openssl_decrypt(base64_decode($data), $method, ENCRYPT_KEY, OPENSSL_RAW_DATA, ENCRYPT_IV); }
使用:
#> echo encode('hello, world'); #> 7WlPEBezVLy9tveHTIkPfQ== #> #> echo decode('7WlPEBezVLy9tveHTIkPfQ=='); #> hello, world
若想ID更加美观, 不想出现乱七八糟的base64符号, 可使用自定义的base32函数
function base32_encode(string $input) :string { $BASE32_ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567'; $output = ''; $v = 0; $vbits = 0; for ($i = 0, $j = strlen($input); $i < $j; $i++) { $v <<= 8; $v += ord($input[$i]); $vbits += 8; while ($vbits >= 5) { $vbits -= 5; $output .= $BASE32_ALPHABET[$v >> $vbits]; $v &= ((1 << $vbits) - 1); } } if ($vbits > 0) { $v <<= (5 - $vbits); $output .= $BASE32_ALPHABET[$v]; } return $output; } function base32_decode(string $input) :string { $output = ''; $v = 0; $vbits = 0; for ($i = 0, $j = strlen($input); $i < $j; $i++) { $v <<= 5; if ($input[$i] >= 'a' && $input[$i] <= 'z') { $v += (ord($input[$i]) - 97); } elseif ($input[$i] >= '2' && $input[$i] <= '7') { $v += (24 + $input[$i]); } else { return ''; } $vbits += 5; while ($vbits >= 8) { $vbits -= 8; $output .= chr($v >> $vbits); $v &= ((1 << $vbits) - 1); } } return $output; } function id_encode($data){ $method = "AES-256-CBC"; return base32_encode(openssl_encrypt($data, $method, ENCRYPT_KEY, OPENSSL_RAW_DATA , ENCRYPT_IV)); } function id_decode($data){ $method = "AES-256-CBC"; return openssl_decrypt(base32_decode($data), $method, ENCRYPT_KEY, OPENSSL_RAW_DATA, ENCRYPT_IV); }
使用:
#> echo id_encode('A-1') . PHP_EOL; #> jfeww5evay47z6b4aluiarjxwi #> #> echo id_decode('jfeww5evay47z6b4aluiarjxwi'); #> A-1
------------------------------可自定义加密串格式的hashid-------------------------------
四、使用hashids composer包, 此方法只可加密数字id.
引入composer包:
composer require hashids/hashids
require('./vendor/autoload.php'); use Hashids\Hashids; $hashids = new Hashids('1233211234567', 18, 'abcdefghijklmnopqrstuvwxyz1234567890'); echo $hashids->encode(2), PHP_EOL; print_r($hashids->decode('ojrq8xgvymvkp0423d'));
评论