Php aes加密(php中aes加密和rsa加密的区别)

2024-05-27 06:50:04 :16

php aes加密(php中aes加密和rsa加密的区别)

各位老铁们好,相信很多人对php aes加密都不是特别的了解,因此呢,今天就来为大家分享下关于php aes加密以及php中aes加密和rsa加密的区别的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

本文目录

php中aes加密和rsa加密的区别

这个跟php没有关系,单纯的是两个密码学的算法。如果真想搞清楚区别,你需要有密码学的基础知识。

我简单说一下,这两个都是标准的密码学算法,应用广泛。AES是一个对称加密算法,常常用于对数据进行加密,RSA是一个非对称(公钥)加密算法,常常用于对AES加密用的密钥进行加密,或者进行数字签名等。

至于对称加密算法和非对称加密算法的区别说起来就越来越多了。你只要知道以下事实就好:

  1. 对称加密算法加解密密钥相同,而非对称加密算法加解密密钥不同

  2. 对称加密算法相对于非对称加密算法而言往往加解密速度很快

  3. 非对称加密算法具有任何有公钥的人都能加密数据,但是只有有私钥的人才能解密数据的特点

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加密解密,编码是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加密对不上java的加密,请问如何实现

要注意特定的Padding实现跟算法的blockSize有关,这里php的blocksize是在php的aes加密前先对源字符串进行Padding,问题得到解决。

求php aes加密代码,编码是UTF-8

    $key = pack(’H*’, "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");        // 显示 AES-128, 192, 256 对应的密钥长度:    // 16,24,32 字节。    $key_size =  strlen($key);    echo "Key size: " . $key_size . "\n";        $plaintext = "This string was AES-256 / CBC / ZeroBytePadding encrypted.";    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);        $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,                                 $plaintext, MCRYPT_MODE_CBC, $iv);

如何通过PHP 进行AES256加密算法

《?phpclass aes { // CRYPTO_CIPHER_BLOCK_SIZE 32 private $_secret_key = ’default_secret_key’; public function setKey($key) { $this-》_secret_key = $key; } public function encode($data) { $td = mcrypt_module_open(MCRYPT_RIJNDAEL_256,’’,MCRYPT_MODE_CBC,’’); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); mcrypt_generic_init($td,$this-》_secret_key,$iv); $encrypted = mcrypt_generic($td,$data); mcrypt_generic_deinit($td); return $iv . $encrypted; } public function decode($data) { $td = mcrypt_module_open(MCRYPT_RIJNDAEL_256,’’,MCRYPT_MODE_CBC,’’); $iv = mb_substr($data,0,32,’latin1’); mcrypt_generic_init($td,$this-》_secret_key,$iv); $data = mb_substr($data,32,mb_strlen($data,’latin1’),’latin1’); $data = mdecrypt_generic($td,$data); mcrypt_generic_deinit($td); mcrypt_module_close($td); return trim($data); }} $aes = new aes();$aes-》setKey(’key’); // 加密$string = $aes-》encode(’string’);// 解密$aes-》decode($string);?》

OK,关于php aes加密和php中aes加密和rsa加密的区别的内容到此结束了,希望对大家有所帮助。

php aes加密(php中aes加密和rsa加密的区别)

本文编辑:admin
Copyright © 2022 All Rights Reserved 威海上格软件有限公司 版权所有

鲁ICP备20007704号

Thanks for visiting my site.