PHP实现AES256加密算法实例(3)

<?php require 'aes.class.php'; // AES PHP implementation require 'aesctr.class.php'; // AES Counter Mode implementation echo 'each change<br>'; $mstr = AesCtr::encrypt('Hello World', 'key', 256); echo "Encrypt String : $mstr<br />"; $dstr = AesCtr::decrypt($mstr, 'key', 256); echo "Decrypt String : $dstr<br />"; echo 'each not change<br>'; $mstr = AesCtr::encrypt('Hello World', 'key', 256, 1); // keep=1 echo "Encrypt String : $mstr<br />"; $dstr = AesCtr::decrypt($mstr, 'key', 256); echo "Decrypt String : $dstr<br />"; ?>

这里再介绍另一使用 PHP mcrypt 加解密方法:

/* aes 256 encrypt * @param String $ostr * @param String $securekey * @param String $type encrypt, decrypt */ function aes($ostr, $securekey, $type='encrypt'){ if($ostr==''){ return ''; } $key = $securekey; $iv = strrev($securekey); $td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); mcrypt_generic_init($td, $key, $iv); $str = ''; switch($type){ case 'encrypt': $str = base64_encode(mcrypt_generic($td, $ostr)); break; case 'decrypt': $str = mdecrypt_generic($td, base64_decode($ostr)); break; } mcrypt_generic_deinit($td); return $str; } // Demo $key = "fdipzone201314showmethemoney!@#$"; $str = "show me the money"; $ostr = aes($str, $key); echo "String 1: $ostr<br />"; $dstr = aes($ostr, $key, 'decrypt'); echo "String 2: $dstr<br />";

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6b96e3dcf1b9ca6a7e8f4c0ff44739cc.html