包含phpcryptaes的词条

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

本篇文章给大家谈谈phpcryptaes,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

PHP的aes加解密算法

1. php的aes算法,加密时会存在空格,0,\0等方式进行补长,所以解密后需要进行trim操作,才能得到原数据串

2. aes加密后进行base64_encode,但是解密时,直接用aes进行解密,不需要先base64_decode.【这个操作很骚气】

function _decryptData($data,$password, $iv){

    $decryptData=openssl_decrypt($data, 'aes-128-cbc', $password, OPENSSL_ZERO_PADDING, $iv);

    $data =json_decode(trim($decryptData), true);

    return $data;

}

function encryptData($data, $password, $iv){

    $data = json_encode($data);//$data是一个数组,如果是字符串,请忽略此句.

    $result = base64_encode(openssl_encrypt($data, 'aes-128-cbc', $password, OPENSSL_RAW_DATA, $iv));

    return $result;

}

php aes加密~呢?

AES加密算法

密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。

?php

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, hex2bin($this-secret_key), $iv);

$cyper_text = mcrypt_generic($td, $str);

$rt = strtoupper(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 = '6D6A39C7078F6783E561B0D1A9EB2E68';

//加密的字符串

$plainText = 'test';

$aes = new CryptAES();

$aes-set_key($keyStr);

$aes-require_pkcs5();

$encText = $aes-encrypt($plainText);

echo $encText;

?

PHP如何实现AES加解密

AES(The Advanced Encryption Standard)是美国国家标准与技术研究所用于加密电子数据的规范。它被预期能成为人们公认的加密包括金融、电信和政府数字信息的方法。本文展示了AES的概貌并解析了它使用的算法。包括一个完整的C#实现和加密.NET数据的举例。在读完本文后你将能用AES加密、测试 基于AES的软件并能在你的系统中使用AES加密。

如何用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;*/

关于phpcryptaes和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

发布于 2023-04-11 12:04:44
收藏
分享
海报
32
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了