php_PHP チュートリアルでの AES 暗号化と復号化の例のまとめ

WBOY
リリース: 2016-07-13 10:24:50
オリジナル
988 人が閲覧しました

aesDemo.php:

例、

コードをコピーします コードは次のとおりです:

require_once('./AES.php');
//$aes = new AES();
$aes = new AES(true) ;// 暗号化された文字列を 16 進数形式で保存します
//$aes = new AES(true,true);// デバッグ情報を含む暗号化された文字列を 16 進数形式で保存します
$key = "これは 32 バイトですkey";// キー
$keys = $aes->makeKey($key);
$encode = "123456";// 暗号化された文字列
$ct = $aes->encryptString($encode, $keys) ;
echo "encode = ".$ct."
";
$cpt = $aes->decryptString($ct, $keys);
echo "decode = ".$cpt;
?>

例、AES暗号化クラス

コードをコピーします コードは次のとおりです:

//php aes暗号化クラス
class AESMcrypt {

パブリック $iv = null;
パブリック $key = null;
パブリック $bit = 128;
プライベート $cipher;

パブリック関数 __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; ブレーク;
case 256:$this->cipher = MCRYPT_RIJNDAEL_256;
デフォルト: $this->cipher = MCRYPT_RIJNDAEL_128;
}

switch($this->mode) {
case 'ecb':$this->mode = MCRYPT_MODE_ECB; ブレーク;
case 'cfb':$this->mode = MCRYPT_MODE_CFB; :$this->mode = MCRYPT_MODE_OFB; ブレーク;
case 'nofb':$this->mode = MCRYPT_MODE_NOFB; ブレーク;
デフォルト: $this->mode = MCRYPT_MODE_CBC;
}
}
public function encrypt($data) {

$data =base64_encode(mcrypt_encrypt( $this->cipher, $this->key, $data, $this->mode, $this->iv));
$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));

暗号化可能および復号化可能なクラスをアタッチした例

コードをコピーします コードは次のとおりです:

/**
* AES 暗号化および復号化クラス
* @author hushangming
*
* 使用法:
*
<br> * // インスタンス化クラス <br> * // パラメータ $_bit: 形式、256、192、128 をサポート、デフォルトは 128 bytes <br> * // パラメータ $_type: 暗号化/復号化方式、cfb、cbc、nofb、ofb、stream、ecb をサポート、デフォルトは ecb<br> * // パラメータ $_key: キー、デフォルトは abcdefghijuklmno<br> * $ tcaes = new TCAES(); <br> * $string = 'laohu';<br> * // 暗号化 <br> * $encodeString = $tcaes->encode($string);<br> * // 復号 <br> * $decodeString = $tcaes- >デコード($encodeString);<br> * 

*/
class TCAES{
private $_bit = MCRYPT_RIJNDAEL_256;
private $_type = MCRYPT_MODE_CBC;
//private $_key = 'abcdefghijuklmno0123456789012345';
プライベート $_key = ' abcdefghijuklmno'; // 密钥
private $_use_base64 = true;
private $_iv_size = null;
private $_iv = null;

/**
* @param string $_key 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->gt;_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->gt;_type = MCRYPT_MODE_OFB;
}elseif('stream' === $_type){
$this->gt;_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 を 16 進数に変換します
* @param string $string
* @return stream
*/
private function toHexString ($string){
$buf = "";
for ($i = 0; $i $val = dechex(ord($string{$i}));
if(strlen($val) $val = "0".$val;
$buf .= $val;
}
return $buf;
}

/**
* 16 進数ストリーム $string を string に変換します
* @param stream $string
* @return string
*/
private function fromHexString($string){
$buf = "";
for($i = 0; $i $val = chr(hexdec(substr($string, $i, 2)));
$buf .= $val;
}
return $buf;
}
}

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/825299.html技術記事 aesDemo.php: 例として、次のようにコードをコピーします: ?php require_once('./AES.php'); //$aes = new AES(); // 暗号化された文字を配置します。文字列は 16 進形式で保存されます...
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!