今天我爱编程网小编为大家带来了如何用php做AES加密解密,编码是UTF-8,跪谢求代码,希望能帮助到大家,一起来看看吧!
本文目录一览:
php SHA256WithRSA签名验签&加密解密
使用:
$pri_key ="";
$pub_key = "";
$char = '方方块儿';//要加密的字符
$sign = $this->RsaEncrypt($char,$pri_key);//加密结果
$result = $this->RsaDecrypt($sign,$pub_key);//对加密结果进行解密
加密:
public function RsaEncrypt($str,$pri_key){
$pi_key =openssl_pkey_get_private($pri_key);
if(!$pi_key)return false;//秘钥不可用
openssl_private_encrypt($str,$encrypted,$pi_key);
$encrypted =base64_encode($encrypted);
return $encrypted;
}
解密:
public function RsaDecrypt($str,$pub_key){
$pu_key =openssl_pkey_get_public($pub_key);
if(!$pu_key)return false;//秘钥不可用
openssl_public_decrypt(base64_decode($str),$decrypted,$pu_key);
return $decrypted;
}
注:开启PHP的php_openssl扩展
我爱编程网
php加密和解密的代码
php提供的加密函数不只是有MD5,虽然MD5是用得最多的,因为其安全性相对较好。但是现在也有网站通过提供庞大的数据库来对其进行破解(比如
www.cmd5.com
)。
除了MD5外,php还支持base64加密,这个函数可以进行解密,但是这种方式加密的结果并不是很安全,当然,如果要求不是很高的话,可以使用。进行base64加密的函数是:base64_encode($str);相反,对加密后的结果进行解密的函数是:base64_decode($str)。如:
<?php
$a="admin";
$b=base64_encode($a);
$c=base64_decode($b);
echo "加密前:\$a=",$a,"<br />";
echo "加密后:\$b=",$b,"<br />";
echo "解密后:\$c=",$c,"<br />";
?>
如果对这种方式得到的加密结果不满意,你可以对加密后或者加密前的字符串进行可逆处理(可逆是为了方便解密),然后在进行加密或者相关处理就可以了。
余下的自己搞定吧
如何用php做AES加密解密,编码是UTF-8,跪谢求代码
我爱编程网(https://www.52biancheng.com)小编还为大家带来如何用php做AES加密解密,编码是UTF-8,跪谢求代码的相关内容。
class CryptAES
{
protected $cipher = MCRYPT_RIJNDAEL_128;
protected $mode = MCRYPT_MODE_ECB;
protected $pad_method = NULL;
protected $secret_key = '';
protected $iv = '';
public function set_cipher($cipher)
{
$this->cipher = $cipher;
}
public function set_mode($mode)
{
$this->mode = $mode;
}
public function set_iv($iv)
{
$this->iv = $iv;
}
public function set_key($key)
{
$this->secret_key = $key;
}
public function require_pkcs5()
{
$this->pad_method = 'pkcs5';
}
protected function pad_or_unpad($str, $ext)
{
if ( is_null($this->pad_method) )
{
return $str;
}
else
{
$func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
if ( is_callable($func_name) )
{
$size = mcrypt_get_block_size($this->cipher, $this->mode);
return call_user_func($func_name, $str, $size);
}
}
return $str;
}
protected function pad($str)
{
return $this->pad_or_unpad($str, '');
}
protected function unpad($str)
{
return $this->pad_or_unpad($str, 'un');
}
public function encrypt($str)
{
$str = $this->pad($str);
$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
}
mcrypt_generic_init($td, $this->secret_key, $iv);
$cyper_text = mcrypt_generic($td, $str);
$rt=base64_encode($cyper_text);
//$rt = bin2hex($cyper_text);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $rt;
}
public function decrypt($str){
$td = mcrypt_module_open($this->cipher, '', $this->mode, '');
if ( empty($this->iv) )
{
$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
}
else
{
$iv = $this->iv;
}
mcrypt_generic_init($td, $this->secret_key, $iv);
//$decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
$decrypted_text = mdecrypt_generic($td, base64_decode($str));
$rt = $decrypted_text;
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $this->unpad($rt);
}
public static function hex2bin($hexdata) {
$bindata = '';
$length = strlen($hexdata);
for ($i=0; $i< $length; $i += 2)
{
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
public static function pkcs5_pad($text, $blocksize)
{
$pad = $blocksize - (@strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public static function pkcs5_unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}
/*$keyStr = 'UITN25LMUQC436IM';
$plainText = 'this is a string will be AES_Encrypt';
$aes = new CryptAES();
$aes->set_key($keyStr);
$aes->require_pkcs5();
$encText = $aes->encrypt($plainText);
$decString = $aes->decrypt($encText);
echo $encText,"n",$decString;*/
以上就是我爱编程网整理的如何用php做AES加密解密,编码是UTF-8,跪谢求代码相关内容,想要了解更多信息,敬请查阅我爱编程网。更多相关文章关注我爱编程网:
www.52biancheng.com免责声明:文章内容来自网络,如有侵权请及时联系删除。