php에서 mcrypt_encrypt 함수를 이용할 수 있다면 손쉽게 적용할 수 있겠지만 함수를 이용할 수 없다면 mcrypt 관련 소스를 설치를 해야하며 php.ini 적용도 해야하는 번거러움이 있다.
그래서 php 스크립트를 이용해 함수를 이용해서 바로 이용가능 하도록 합니다.
먼저 암호화 encrypt 함수 :
### PHP암호화 함수
function encrypt($data,$k) {
$encrypt_these_chars = "1234567890ABCDEFGHIJKLMNOPQRTSUVWXYZabcdefghijklmnopqrstuvwxyz.,/?!$@^*()_+-=:;~{}";
$t = $data;
$result2;
$ki;
$ti;
$keylength = strlen($k);
$textlength = strlen($t);
$modulo = strlen($encrypt_these_chars);
$dbg_key;
$dbg_inp;
$dbg_sum;
for ($result2 = "", $ki = $ti = 0; $ti < $textlength; $ti++, $ki++) {
if ($ki >= $keylength) {
$ki = 0;
}
$dbg_inp += "["+$ti+"]="+strpos($encrypt_these_chars, substr($t, $ti,1))+" ";
$dbg_key += "["+$ki+"]="+strpos($encrypt_these_chars, substr($k, $ki,1))+" ";
$dbg_sum += "["+$ti+"]="+strpos($encrypt_these_chars, substr($k, $ki,1))+ strpos($encrypt_these_chars, substr($t, $ti,1)) % $modulo +" ";
$c = strpos($encrypt_these_chars, substr($t, $ti,1));
$d;
$e;
if ($c >= 0) {
$c = ($c + strpos($encrypt_these_chars, substr($k, $ki,1))) % $modulo;
$d = substr($encrypt_these_chars, $c,1);
$e .= $d;
} else {
$e += $t.substr($ti,1);
}
}
$key2 = $result2;
$debug = "Key : "+$k+"\n"+"Input: "+$t+"\n"+"Key : "+$dbg_key+"\n"+"Input: "+$dbg_inp+"\n"+"Enc : "+$dbg_sum;
return $e . "";
}
암호화 사용 예:
$data = "iloveyou!good"; //암호화 해서 넘길 값
$key = "123456"; //암호화에 이용될 키 값
$edata = encrypt($data,$key); //key 값을 이용해 data 값을 암호화해서 edata에 담았습니다.
$getdata = urlencode($edata); //이 값을 post가 아닌 get으로 넘긴다면 urlencode를 해주시는게 좋겠죠!
echo $getdata; //최종 암호화 및 url 엔코드까지 한 값 입니다.
출력 : imqyi%3Fov%40jstd
출력된 값만 봐서는 도저히 어떤 값인지 모르겠죠?
이제 위 값으로 복호화 해 보겠습니다.
복호화 decrypt 함수 :
function decrypt($key2,$secret) {
$encrypt_these_chars = "1234567890ABCDEFGHIJKLMNOPQRTSUVWXYZabcdefghijklmnopqrstuvwxyz.,/?!$@^*()_+-=:;~{}";
$input = $key2;
$output = "";
$debug = "";
$k = $secret;
$t = $input;
$result;
$ki;
$ti;
$keylength = strlen($k);
$textlength = strlen($t);
$modulo = strlen($encrypt_these_chars);
$dbg_key;
$dbg_inp;
$dbg_sum;
for ($result = "", $ki = $ti = 0; $ti < $textlength; $ti++, $ki++) {
if ($ki >= $keylength){
$ki = 0;
}
$c = strpos($encrypt_these_chars, substr($t, $ti,1));
if ($c >= 0) {
$c = ($c - strpos($encrypt_these_chars , substr($k, $ki,1)) + $modulo) % $modulo;
$result .= substr($encrypt_these_chars , $c, 1);
} else {
$result += substr($t, $ti,1);
}
}
return $result;
}
복호화 사용 예 :
$data = urldecode($getdata); //urlencode로 받은 값을 먼저 urldecode 처리해야함
$key = "123456"; //암호화 할 때 이용한 키값과 동일하게 사용
$ddata = decrypt($data,$key); //복호화 처리
echo $ddata; //최종 복호화 값 전달하고자 하는 값이 제대로 전달 되었군요!
출력결과 : iloveyou!good
위 암호화 복호화 방식의 단점은
$encrypt_these_chars = "1234567890ABCDEFGHIJKLMNOPQRTSUVWXYZabcdefghijklmnopqrstuvwxyz.,/?!$@^*()_+-=:;~{}";
에 지정된 값만 암호화 된다는 것 입니다.
즉, 암호화 비교를 할 값이 없는건에 대해서는 추가 해야 합니다.
참고로, 한글은 안됩니다. ^^
'PHP∵SCRIPT' 카테고리의 다른 글
PHP 코드를 최적화하는 40가지 팁 (번역) (0) | 2014.04.11 |
---|---|
이벤트 핸들러 (Event Handler) 모음 (0) | 2014.04.11 |
잘못된 PHP 코딩 스타일 (0) | 2014.04.08 |
PHP의 소켓 함수 (0) | 2014.04.08 |
php 환경변수정리 (0) | 2014.04.07 |
mySQL테이블의 설계 (0) | 2014.04.06 |
mySQL접속하기 (0) | 2014.04.06 |
mySQL사용하기 (0) | 2014.04.06 |
댓글