aesDemo.php:
例子,
<?php require_once('./AES.php'); //$aes = new AES(); $aes = new AES(true);// 把加密后的字符串按十六进制进行存储 //$aes = new AES(true,true);// 带有调试信息且加密字符串按十六进制存储 $key = "this is a 32 byte key";// 密钥 $keys = $aes->makeKey($key); $encode = "123456";// 被加密的字符串 $ct = $aes->encryptString($encode, $keys); echo "encode = ".$ct."<br>"; $cpt = $aes->decryptString($ct, $keys); echo "decode = ".$cpt; ?>
例子、AES加密類
<?php //php aes加密类 class AESMcrypt { public $iv = null; public $key = null; public $bit = 128; private $cipher; public function __construct($bit, $key, $iv, $mode) { if(empty($bit) || empty($key) || empty($iv) || empty($mode)) return NULL; $this->bit = $bit; $this->key = $key; $this->iv = $iv; $this->mode = $mode; switch($this->bit) { case 192:$this->cipher = MCRYPT_RIJNDAEL_192; break; case 256:$this->cipher = MCRYPT_RIJNDAEL_256; break; default: $this->cipher = MCRYPT_RIJNDAEL_128; } switch($this->mode) { case 'ecb':$this->mode = MCRYPT_MODE_ECB; break; case 'cfb':$this->mode = MCRYPT_MODE_CFB; break; case 'ofb':$this->mode = MCRYPT_MODE_OFB; break; case 'nofb':$this->mode = MCRYPT_MODE_NOFB; break; default: $this->mode = MCRYPT_MODE_CBC; } } public function encrypt($data) { $data = base64_encode(mcrypt_encrypt( $this->cipher, $this->key, $data, $this->mode, $this->iv)); return $data; } public function decrypt($data) { $data = mcrypt_decrypt( $this->cipher, $this->key, base64_decode($data), $this->mode, $this->iv); $data = rtrim(rtrim($data), ".."); return $data; } } //使用方法 $aes = new AESMcrypt($bit = 128, $key = 'abcdef1234567890', $iv = '0987654321fedcba', $mode = 'cbc'); $c = $aes->encrypt('haowei.me'); var_dump($aes->decrypt($c));
例子、附一個可加密可解密類
<?PHP /** * AES加密、解密类 * @author hushangming * * 用法: * <pre class="brush:php;toolbar:false"> * // 实例化类 * // 参数$_bit:格式,支持256、192、128,默认为128字节的 * // 参数$_type:加密/解密方式,支持cfb、cbc、nofb、ofb、stream、ecb,默认为ecb * // 参数$_key:密钥,默认为abcdefghijuklmno * $tcaes = new TCAES(); * $string = 'laohu'; * // 加密 * $encodeString = $tcaes->encode($string); * // 解密 * $decodeString = $tcaes->decode($encodeString); **/ class TCAES{ private $_bit = MCRYPT_RIJNDAEL_256; private $_type = MCRYPT_MODE_CBC; //private $_key = 'abcdefghijuklmno0123456789012345'; private $_key = 'abcdefghijuklmno'; // 密钥 private $_use_base64 = true; private $_iv_size = null; private $_iv = null; /** * @param string $_key 密钥 * @param int $_bit 默认使用128字节 * @param string $_type 加密解密方式 * @param boolean $_use_base64 默认使用base64二次加密 */ public function __construct($_key = '', $_bit = 128, $_type = 'ecb', $_use_base64 = true){ // 加密字节 if(192 === $_bit){ $this->_bit = MCRYPT_RIJNDAEL_192; }elseif(128 === $_bit){ $this->_bit = MCRYPT_RIJNDAEL_128; }else{ $this->_bit = MCRYPT_RIJNDAEL_256; } // 加密方法 if('cfb' === $_type){ $this->_type = MCRYPT_MODE_CFB; }elseif('cbc' === $_type){ $this->_type = MCRYPT_MODE_CBC; }elseif('nofb' === $_type){ $this->_type = MCRYPT_MODE_NOFB; }elseif('ofb' === $_type){ $this->_type = MCRYPT_MODE_OFB; }elseif('stream' === $_type){ $this->_type = MCRYPT_MODE_STREAM; }else{ $this->_type = MCRYPT_MODE_ECB; } // 密钥 if(!empty($_key)){ $this->_key = $_key; } // 是否使用base64 $this->_use_base64 = $_use_base64; $this->_iv_size = mcrypt_get_iv_size($this->_bit, $this->_type); $this->_iv = mcrypt_create_iv($this->_iv_size, MCRYPT_RAND); } /** * 加密 * @param string $string 待加密字符串 * @return string */ public function encode($string){ if(MCRYPT_MODE_ECB === $this->_type){ $encodeString = mcrypt_encrypt($this->_bit, $this->_key, $string, $this->_type); }else{ $encodeString = mcrypt_encrypt($this->_bit, $this->_key, $string, $this->_type, $this->_iv); } if($this->_use_base64) $encodeString = base64_encode($encodeString); return $encodeString; } /** * 解密 * @param string $string 待解密字符串 * @return string */ public function decode($string){ if($this->_use_base64) $string = base64_decode($string); $string = $this->toHexString($string); if(MCRYPT_MODE_ECB === $this->_type){ $decodeString = mcrypt_decrypt($this->_bit, $this->_key, $string, $this->_type); }else{ $decodeString = mcrypt_decrypt($this->_bit, $this->_key, $string, $this->_type, $this->_iv); } return $decodeString; } /** * 将$string转换成十六进制 * @param string $string * @return stream */ private function toHexString ($string){ $buf = ""; for ($i = 0; $i
更多php中AES加密解密請注意小結網相關文章的例子小結網相關文章!

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1
好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版