Aes.php
class Aes{
const M_CBC = 'cbc';
const M_CFB = 'cfb';
const M_ECB = 'ecb';
const M_NOFB = 'nofb';
const M_OFB = 'ofb';
const M_STREAM = 'stream';
protected $key;
protected $cipher;
protected $data;
protected $mode;
protected $IV;
/**
*
* @param type $data
* @param type $key
* @param type $blockSize
* @param type $mode
*/
function __construct($data = null, $key = null, $blockSize = null, $mode = null)
{
$this->setData($data);
$this->setKey($key);
$this->setBlockSize($blockSize);
$this->setMode($mode);
$this->setIV('');
}
/**
*
* @param type $data
*/
public function setData($data)
{
$this->data = $data;
}
/**
*
* @param type $key
*/
public function setKey($key)
{
$this->key = $this->hex2bin($key);
}
/**
*
* @param type $blockSize
*/
public function setBlockSize($blockSize)
{
switch ($blockSize)
{
case 128:
$this->cipher = MCRYPT_RIJNDAEL_128;
break;
case 192:
$this->cipher = MCRYPT_RIJNDAEL_192;
break;
case 256:
$this->cipher = MCRYPT_RIJNDAEL_256;
break;
}
}
public function hex2bin($hexdata)
{
$bindata="";
for ($i=0;$i<strlen($hexdata);$i+=2)
{
$bindata.=chr(hexdec(substr($hexdata,$i,2)));
}
return $bindata;
}
/**
*
* @param type $mode
*/
public function setMode($mode)
{
switch ($mode)
{
case Aes::M_CBC:
$this->mode = MCRYPT_MODE_CBC;
break;
case Aes::M_CFB:
$this->mode = MCRYPT_MODE_CFB;
break;
case Aes::M_ECB:
$this->mode = MCRYPT_MODE_ECB;
break;
case Aes::M_NOFB:
$this->mode = MCRYPT_MODE_NOFB;
break;
case Aes::M_OFB:
$this->mode = MCRYPT_MODE_OFB;
break;
case Aes::M_STREAM:
$this->mode = MCRYPT_MODE_STREAM;
break;
default:
$this->mode = MCRYPT_MODE_ECB;
break;
}
}
/**
*
* @return boolean
*/
public function validateParams()
{
if ($this->data != null && $this->key != null && $this->cipher != null)
{
return true;
}
else
{
return FALSE;
}
}
public function setIV($IV)
{
$this->IV = $IV;
}
protected function getIV() {
if ($this->IV == "") {
$this->IV = mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->mode), MCRYPT_RAND);
}
return $this->hex2bin($this->IV);
}
/**
* @return type
* @throws Exception
*/
public function encrypt() {
if ($this->validateParams())
{
return trim(base64_encode(mcrypt_encrypt($this->cipher, $this->key, $this->data, $this->mode, $this->getIV())));
}
else
{
throw new Exception('Invlid params!');
}
}
/**
*
* @return type
* @throws Exception
*/
public function decrypt()
{
if ($this->validateParams())
{
return trim(mcrypt_decrypt($this->cipher, $this->key, base64_decode($this->data), $this->mode, $this->getIV()));
}
else
{
throw new Exception('Invlid params!');
}
}
}
//rfc1423
사용할 페이지
include "class/Aes.php"; //Encoding, Decoding Class
$key = "431007e1a4b4690ad0bbd65a37230c91";
//Encoding
function get_aes_encoding($text)
{
global $key;
$rtn="";
if($text)
{
$aes = new Aes();
$aes->setIV("9cdcd84b33bdcae8ee8cb05798cc19be");
$aes->setKey($key);
$aes->setBlockSize('128');
$aes->setMode('cbc');
$aes->setData($text);
$rtn=$aes->encrypt();
}
return $rtn;
}
//Decoding
function get_aes_decoding($text)
{
global $key;
$rtn="";
if($text)
{
$aes = new Aes();
$aes->setIV("9cdcd84b33bdcae8ee8cb05798cc19be");
$aes->setKey($key);
$aes->setBlockSize('128');
$aes->setMode('cbc');
$aes->setData($text);
$rtn=$aes->decrypt();
}
return $rtn;
}
$str = "123";
$en_str = get_aes_encoding($str);
echo $en_str;
echo "<br><br>";
echo get_aes_decoding($en_str);
'[ Web 관련 ] > php' 카테고리의 다른 글
파일 경로에서 폴더 출력 (0) | 2021.02.05 |
---|---|
php 폴더 생성, 파일생성 file_exists(), is_dir(), mkdir() (0) | 2021.02.05 |
$_SERVER 전역 변수 (0) | 2021.01.29 |
php 두 날짜의 개월수 차이 구하기 (예. 호봉 구하기) (0) | 2021.01.07 |
php 폴더 용량 조회 + DB 용량 조회 (0) | 2020.12.02 |