PHP¿¡¼ ¾ÏÈ£È encrypt º¹È£È decrypt Çؼ °ªÀ» ³Ñ±â±â |
|
|
 |
13³â Àü |
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.,/?!$@^*()_+-=:;~{}";
¿¡ ÁöÁ¤µÈ °ª¸¸ ¾ÏÈ£È µÈ´Ù´Â °Í ÀÔ´Ï´Ù.
Áï, ¾ÏÈ£È ºñ±³¸¦ ÇÒ °ªÀÌ ¾ø´Â°Ç¿¡ ´ëÇؼ´Â Ãß°¡ ÇØ¾ß ÇÕ´Ï´Ù.
Âü°í·Î, ÇѱÛÀº ¾ÈµË´Ï´Ù. ^^
|
|
̵̧ : 541 |
̵̧
¸ñ·Ï
|
|