基於openssl的AES加解密

2021-09-19 10:41:48 字數 4515 閱讀 7951

對於加解密,企業常用的有aes和rsa,它們分別是對稱加解密演算法和非對稱加解密演算法的代表,這裡主要封裝了php的一套基於openssl的aes加解密實現類

<?php

/** * created by phpstorm.

* user: 小小徐

* date: 2019/4/21

* time: 18:11

*/class baseaes

/*** 校驗金鑰長度

* @param string $secretkey 16/24/32 -characters secret password

* @return bool

*/protected static function iskeylengthvalid($secretkey)

/*** 獲取加解密的字串

* @return string|null

*/public function getdata()

/*** 獲取隨機偏移量

* @return string|null

*/public function getinitvector()

/*** 獲取錯誤資訊

* @return string|null

*/public function geterrormessage()

/*** 判斷是否有錯誤

* @return bool

*/public function haserror()

/*** @return null|string

*/public function __tostring()

/*** 基於openssl的解密方法

* @param string $plaintext 需要解密的字串

* @param string $secretkey 金鑰長度16|24|32

* @param string $initvector 偏移量

* @param string $method 解密方式

* @return static

*/public static function decrypt($ciphertext, $secretkey,$initvector=null, $method= null)

// 獲取原加密後的字串

$encoded = base64_decode($ciphertext);

//獲取解密方式

if(null === $method)

// 獲取真正加密後的原始串

$data = static ::decryptcallback($initvector,$encoded);

// 解密

$decoded = openssl_decrypt(

$data,

$method,

$secretkey,

openssl_raw_data,

$initvector

);if ($decoded === false)

// 返回乙個自己的物件 可以再進行進一步操作

return new static($initvector, $decoded);

} catch (\exception $e)

}/**

* 基於openssl的加密方法

* @param string $plaintext 需要加密的字串

* @param string $secretkey 金鑰長度16|24|32

* @param string $initvector 偏移量

* @param string $method 加密方式

* @return static

*/protected static function encrypt($plaintext, $secretkey,$initvector=null, $method= null)

//設定加密方式

if(null === $method)

// 生成隨機向量 即偏移量

if(null === $initvector)

//加密

$raw = openssl_encrypt(

$plaintext,

$method,

$secretkey,

openssl_raw_data,

$initvector

);//對加密後的原始資料和偏移量進行操作

$callbackresult = static ::encryptcallback($initvector,$raw);

// return base64-encoded string: $callbackresult result |將資料進行base64_encode

$result = base64_encode($callbackresult);

if ($result === false)

// 返回自己的物件方便進一步操作

return new static($initvector, $result);

} catch (\exception $e)

}/**

* 與decryptcallback相對應

* 在加密後和base64_encode之前 對 加密後的原始資料與偏移量進行操作

* @param $initvector string 偏移量

* @param $raw string 加密後的原始串[非utf8]

* @return string

*/protected static function encryptcallback($initvector,$raw)

/*** 與encryptcallback相對應

* 在base64_decode之後和解密之前 對 需要解密的字串進行base64_decode之後的原始資料與偏移量進行操作

* @param $initvector string 偏移量

* @param $encoded string 解密後的原始串[非utf8]

* @return bool|string

*/protected static function decryptcallback(&$initvector,$encoded)

return substr($encoded, static::init_vector_length);

}/**

* aes128加密

* @param $plaintext string 需要加密的字串

* @param $secretkey string 金鑰

* @param $initvector string 偏移量

* @return baseaes

*/public static function encryptwith128cbc($plaintext, $secretkey,$initvector=null)

/*** aes128解密

* @param $ciphertext string 需要解密的字串

* @param $secretkey string 金鑰

* @param $initvector string 偏移量

*/public static function decryptwith128cbc($ciphertext, $secretkey,$initvector=null)

/*** aes256加密

* @param $plaintext string 需要加密的字串

* @param $secretkey string 金鑰

* @param $initvector string 偏移量

* @return baseaes

*/public static function encryptwith256cbc($plaintext, $secretkey,$initvector=null)

/*** aes256解密

* @param $ciphertext string 需要解密的字串

* @param $secretkey string 金鑰

* @param $initvector string 偏移量

*/public static function decryptwith256cbc($ciphertext, $secretkey,$initvector=null)

}

使用方式

$key = 'zxcvbnmasdfghjkl';

$data = "who are you!";

$res1 = baseaes::encryptwith256cbc($data,$key);

$res2 = baseaes::decryptwith256cbc($res1,$key);

var_dump($res1);

var_dump($res2);

為了滿足更特殊的需求,返回的是乙個物件,包含加密|解密的資料及隨機生成的偏移量以滿足特殊的使用場景

OpenSSL中AES加密的用法

使用api的時候,需要特別小心資料長度,我在初次使用的時候簡直被弄的暈頭轉向,遂作此文留個備忘。一般沒有指定長度的引數,預設都是16 aes block size 個位元組。輸出資料的長度一般都是16位元組的倍數,否則會出現陣列越界訪問。以下api中,encrypt表示加密,decrypt表示解密。...

PHP使用OpenSSL實現AES加密的筆記

記錄這兩天遇到的一些問題,一開始主要是對一些概念理解的不清楚 比如aes是一種演算法,而openssl是乙個擴充套件庫,其中包含對aes演算法的實現 當然在完全理解演算法後可以自己實現,但可能會遇到很多問題,我會在以後進行嘗試 目前在專案的使用中,還是使用擴充套件庫的方法最快最簡單 1,加密方法 通...

openssl裡面AES演算法主要函式的引數的介紹

int aes set encrypt key const unsigned char userkey,const int bits,aes key key int aes set decrypt key const unsigned char userkey,const int bits,aes ...