當前位置:首頁 » 編程語言 » php加密aes

php加密aes

發布時間: 2022-06-10 05:51:06

『壹』 如何通過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_mole_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_mole_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_mole_close($td);
return trim($data);
}
}
$aes = new aes();$aes->setKey('key');
// 加密$string = $aes->encode('string');// 解密$aes->decode($string);?>

『貳』 如何在php中實現AES加密

文件加密建議:
1.下載 Abel文件加密器 1.1(軟體大小39KB,簡體中文,免費版,綠色軟體,無須安裝。)
iask.tech.sina/ishare/browse_file.php?fileid=87135
2.在非系統盤里建一文件夾,把壓縮文件解壓其中.
點擊文件加密器(AbelLock)圖標---出現「文件列表」,點擊「添加」---出現打開,在「查找范圍」,點&quot;Abel文件加密器 1.1&quot;右面的▲下拉框符號,選擇你需加密文件的路經及文件,單擊後會出現在「文件名」顯示框中,點擊「打開」---輸入密碼.確認密碼後點擊「加密」,點擊退出.

『叄』 php AES加密,怎樣用CTR加密模式

分組密碼有五種工作體制:1.電碼本模式(Electronic Codebook Book (ECB));2.密碼分組鏈接模式(Cipher Block Chaining (CBC));3.計算器模式(Counter (CTR));4.密碼反饋模式(Cipher FeedBack (CFB));5.輸出反饋模式(Output FeedBack (OFB))。
以下逐一介紹一下:
1.電碼本模式(Electronic Codebook Book (ECB)
這種模式是將整個明文分成若干段相同的小段,然後對每一小段進行加密。
2.密碼分組鏈接模式(Cipher Block Chaining (CBC))
這種模式是先將明文切分成若干小段,然後每一小段與初始塊或者上一段的密文段進行異或運算後,再與密鑰進行加密。
3.計算器模式(Counter (CTR))
計算器模式不常見,在CTR模式中, 有一個自增的運算元,這個運算元用密鑰加密之後的輸出和明文異或的結果得到密文,相當於一次一密。這種加密方式簡單快速,安全可靠,而且可以並行加密,但是在計算器不能維持很長的情況下,密鑰只能使用一次。
4.密碼反饋模式(Cipher FeedBack (CFB))
這種模式較復雜。
5.輸出反饋模式(Output FeedBack (OFB))
這種模式較復雜。

『肆』 如何用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_mole_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_mole_close($td);

return $rt;
}

public function decrypt($str){
$td = mcrypt_mole_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_mole_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加密和rsa加密的區別

這個跟php沒有關系,單純的是兩個密碼學的演算法。如果真想搞清楚區別,你需要有密碼學的基礎知識。

我簡單說一下,這兩個都是標準的密碼學演算法,應用廣泛。AES是一個對稱加密演算法,常常用於對數據進行加密,RSA是一個非對稱(公鑰)加密演算法,常常用於對AES加密用的密鑰進行加密,或者進行數字簽名等。

至於對稱加密演算法和非對稱加密演算法的區別說起來就越來越多了。你只要知道以下事實就好:

  1. 對稱加密演算法加解密密鑰相同,而非對稱加密演算法加解密密鑰不同

  2. 對稱加密演算法相對於非對稱加密演算法而言往往加解密速度很快

  3. 非對稱加密演算法具有任何有公鑰的人都能加密數據,但是只有有私鑰的人才能解密數據的特點

『陸』 php 如何進行aes加密

AES只是一種加密演算法,只是一種工具,怎麼用看你自己了~
一般用來加密數據、驗證數據完整性等等~

『柒』 php aes加密~呢

AES加密演算法

密碼學中的高級加密標准(AdvancedEncryptionStandard,AES),又稱Rijndael加密法,是美國聯邦政府採用的一種區塊加密標准。這個標准用來替代原先的DES,已經被多方分析且廣為全世界所使用。

<?php

classCryptAES

{

protected$cipher=MCRYPT_RIJNDAEL_128;

protected$mode=MCRYPT_MODE_ECB;

protected$pad_method=NULL;

protected$secret_key='';

protected$iv='';

publicfunctionset_cipher($cipher)

{

$this->cipher=$cipher;

}

publicfunctionset_mode($mode)

{

$this->mode=$mode;

}

publicfunctionset_iv($iv)

{

$this->iv=$iv;

}

publicfunctionset_key($key)

{

$this->secret_key=$key;

}

publicfunctionrequire_pkcs5()

{

$this->pad_method='pkcs5';

}

protectedfunctionpad_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);

returncall_user_func($func_name,$str,$size);

}

}

return$str;

}

protectedfunctionpad($str)

{

return$this->pad_or_unpad($str,'');

}

protectedfunctionunpad($str)

{

return$this->pad_or_unpad($str,'un');

}

publicfunctionencrypt($str)

{

$str=$this->pad($str);

$td=mcrypt_mole_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_mole_close($td);

return$rt;

}

publicfunctiondecrypt($str){

$td=mcrypt_mole_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_mole_close($td);

return$this->unpad($rt);

}

publicstaticfunctionhex2bin($hexdata){

$bindata='';

$length=strlen($hexdata);

for($i=0;$i<$length;$i+=2)

{

$bindata.=chr(hexdec(substr($hexdata,$i,2)));

}

return$bindata;

}

publicstaticfunctionpkcs5_pad($text,$blocksize)

{

$pad=$blocksize-(strlen($text)%$blocksize);

return$text.str_repeat(chr($pad),$pad);

}

publicstaticfunctionpkcs5_unpad($text)

{

$pad=ord($text{strlen($text)-1});

if($pad>strlen($text))returnfalse;

if(strspn($text,chr($pad),strlen($text)-$pad)!=$pad)returnfalse;

returnsubstr($text,0,-1*$pad);

}

}

//密鑰

$keyStr='';

//加密的字元串

$plainText='test';

$aes=newCryptAES();

$aes->set_key($keyStr);

$aes->require_pkcs5();

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

echo$encText;

?>

『捌』 PHP如何實現AES加解密

php載入Mcrypt組件php_mycrypt.dll/.so,支持AES和3DES編碼,
只是該模塊沒有提供補齊padding方法,要自己用PHP代碼寫PKCS7之類的補齊方法

『玖』 求php aes加密代碼,編碼是UTF-8


$key=pack('H*',"");

//顯示AES-128,192,256對應的密鑰長度:
//16,24,32位元組。
$key_size=strlen($key);
echo"Keysize:".$key_size." ";

$plaintext="ThisstringwasAES-256/CBC/ZeroBytePaddingencrypted.";


$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 AES加密對不上java的加密,請問如何實現

要注意特定的Padding實現跟演算法的blockSize有關,這里php的blocksize是在php的aes加密前先對源字元串進行Padding,問題得到解決。

熱點內容
伺服器機櫃屬於什麼輻射 發布:2024-05-05 18:02:10 瀏覽:335
存儲成本計算 發布:2024-05-05 18:02:10 瀏覽:583
如何把手機改安卓10 發布:2024-05-05 17:39:07 瀏覽:497
我的世界怎麼擴容伺服器內存 發布:2024-05-05 17:19:54 瀏覽:48
java讀取文件字元 發布:2024-05-05 17:15:18 瀏覽:11
三星怎麼應用加密 發布:2024-05-05 17:13:18 瀏覽:152
cad字體在那個文件夾 發布:2024-05-05 17:08:20 瀏覽:331
什麼時候用編譯器 發布:2024-05-05 17:08:20 瀏覽:766
應急救援腳本 發布:2024-05-05 17:08:17 瀏覽:338
我的世界搭建無正版驗證伺服器 發布:2024-05-05 17:03:48 瀏覽:819