| 제목 | AES 암호화 라이브러리는 없나요??? | ||
|---|---|---|---|
| 글쓴이 | 이그제트 | 작성시각 | 2016/03/10 17:08:43 | 
|  | |||
| 데이터 암호화를 AES로 처리를 해야하는데 
 코드이그나이터용 라이브러리가 혹시 있을까요??? 답변 부탁드립니다. | |||
| 다음글 | 중복로그인 식별방법 및 도메인문의 | ||
| 이전글 | $this->db->where() 공백이 왜... (3) | ||
| 
                                Luj
                                /
                                2016/03/10 17:25:44 /
                                추천
                                0
                             | 
| 
                                kaido
                                /
                                2016/03/11 09:00:47 /
                                추천
                                0
                             제거 라이브러리 하나 올려드릴게요 ㅎㅎ 
<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
class Secretlib {
	protected static $key_size = 128;
	// The default key size in bits
	protected static $valid_key_sizes = array(128, 192, 256);
	// Sizes in bits
	protected static $key = "your key";
	protected static $iv = "1234567890123"; // sample iv
	public static function pkcs5Pad2($data, $blocksize) {
		$pad = $blocksize - (strlen($data) % $blocksize);
		$returnValue = $data . str_repeat(chr($pad), $pad);
		return $returnValue;
	}
	public static function enc_aes128_ecb($data, $key = null, $iv = null, $dataAs = 0) {
		if (!$key)	$key = Secretlib::$key;
		if (!$iv)	$iv = Secretlib::$iv;
		$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'ecb');
		$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'ecb', '');
		// Add padding to String
		$data = self::pkcs5Pad2($data, $size);
		$length = strlen($data);
		mcrypt_generic_init($cipher, $key, $iv);
		$data = mcrypt_generic($cipher, $data);
		$data = bin2hex($data);
		mcrypt_generic_deinit($cipher);
		return $data;
	}
	public static function dec_aes128_ecb($data, $key = null, $iv = null, $dataAs = 0) {
		if (!$key)	$key = Secretlib::$key;
		if (!$iv)	$iv = Secretlib::$iv;
		$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'ecb');
		$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'ecb', '');
		mcrypt_generic_init($cipher, $key, $iv);
		$data = pack('H*', $data);
		
		$data = mdecrypt_generic($cipher, $data);
		mcrypt_generic_deinit($cipher);
		return Secretlib::pkcs5Unpad($data);
	}
	private static function pkcs5Unpad($data) {
		$pad = ord($data{strlen($data) - 1});
		if ($pad > strlen($data))
			return false;
		if (strspn($data, chr($pad), strlen($data) - $pad) != $pad)
			return false;
		return substr($data, 0, -1 * $pad);
	}
}
 ecb 말고도 다른걸로도 사용 가능 합니다. mcrypt 로 검색하셔서 원하는 대로 수정해서 사용하세요.^^ | 
$this->load->model('aes_m');
$this->aes_m->setKey($licenseKey); // Key Set 하는 부분을 깜빡했었네요...ㅎㅎ;
base64_encode($this->aes_m->encrypt($var));
$this->aes_m->decrypt(base64_decode($var));
이렇게 사용하고 있어요ㅎㅎ