Advertisement
Guest User

EKI

a guest
Jul 19th, 2010
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.34 KB | None | 0 0
  1. <?
  2.  
  3.     /*
  4.         Titkosítás a saki protokoll alapján
  5.  
  6.         Paraméterek:
  7.  
  8.             $plaintext: a titkosítandó string
  9.             $keyfile: a titkosításhoz szükséges kulcsfile neve
  10.  
  11.         Visszatérő érték:
  12.  
  13.             A titkosított üzenet
  14.  
  15.     */
  16.  
  17.     function ekiEncodeUrl($plaintext,$keyfile)
  18.     {
  19.         $f=fopen($keyfile,"r");
  20.         $keyinfo=fread($f,38);
  21.         fclose($f);
  22.         $k1=substr($keyinfo,14,8);
  23.         $k2=substr($keyinfo,22,8);
  24.         $iv=substr($keyinfo,30,8);
  25.         $key=$k1.$k2.$k1;
  26.         $arr=split("&",$plaintext);
  27.         $outs="";
  28.         $pid="";
  29.         for ($i=0;$i<count($arr);$i++)
  30.         {
  31.             if (strtoupper($arr[$i])!="CRYPTO=1")
  32.                 $outs.="&".$arr[$i];
  33.             if (substr(strtoupper($arr[$i]),0,4)=="PID=")
  34.                 $pid=substr(strtoupper($arr[$i]),4,7);
  35.         }
  36.         $outs=substr($outs,1);
  37.         $outs=rawurlencode($outs);
  38.         $outs=str_replace("%3D","=",$outs);
  39.         $outs=str_replace("%26","&",$outs);
  40.         $crc=str_pad(dechex(crc32($outs)),8,"0",STR_PAD_LEFT);
  41.         for ($i=0;$i<4;$i++)
  42.             $outs.=chr(base_convert(substr($crc,$i*2,2),16,10));
  43.         $pad=8-(strlen($outs) % 8);
  44.         for ($i=0;$i<$pad;$i++)
  45.             $outs.=chr($pad);
  46.         $td=mcrypt_module_open("tripledes","","cbc","");
  47.         mcrypt_generic_init($td,$key,$iv);
  48.         $outs=mcrypt_generic($td,$outs);
  49.         mcrypt_module_close($td);
  50.         $pad=3-(strlen($outs) % 3);
  51.         for ($i=0;$i<$pad;$i++)
  52.             $outs.=chr($pad);
  53.         $outs=base64_encode($outs);
  54.         $outs=rawurlencode($outs);
  55.         $outs="PID=".$pid."&CRYPTO=1&DATA=".$outs;
  56.         return $outs;
  57.     }
  58.  
  59.     /*
  60.         Kititkosítás a saki protokoll alapján
  61.  
  62.         Paraméterek:
  63.  
  64.             $crypto: a kititkosítandó string
  65.             $keyfile: a titkosításhoz szükséges kulcsfile neve
  66.  
  67.         Visszatérő érték:
  68.  
  69.             A kititkosított üzenet, vagy crc hiba esetén üres string
  70.  
  71.     */
  72.  
  73.         function ekiDecodeUrl($crypto,$keyfile)
  74.     {
  75.                 $f=fopen($keyfile,"r");
  76.                 $keyinfo=fread($f,38);
  77.         fclose($f);
  78.         $k1=substr($keyinfo,14,8);
  79.         $k2=substr($keyinfo,22,8);
  80.         $iv=substr($keyinfo,30,8);
  81.         $key=$k1.$k2.$k1;
  82.         $arr=split("&",$crypto);
  83.         $outs="";
  84.         $pid="";
  85.         for ($i=0;$i<count($arr);$i++)
  86.         {
  87.             if (substr(strtoupper($arr[$i]),0,5)=="DATA=")
  88.                 $outs=substr($arr[$i],5);
  89.             if (substr(strtoupper($arr[$i]),0,4)=="PID=")
  90.                 $pid=substr(strtoupper($arr[$i]),4,7);
  91.         }
  92.         $outs=rawurldecode($outs);
  93.         $outs=base64_decode($outs);
  94.         $lastc=ord($outs[strlen($outs)-1]);
  95.         $validpad=1;
  96.         for ($i=0;$i<$lastc;$i++)
  97.             if (ord(substr($outs,strlen($outs)-1-$i,1))!=$lastc)
  98.                 $validpad=0;
  99.         if ($validpad==1)
  100.             $outs=substr($outs,0,strlen($outs)-$lastc);
  101.         $td=mcrypt_module_open("tripledes","","cbc","");
  102.         mcrypt_generic_init($td,$key,$iv);
  103.         $outs=mdecrypt_generic($td,$outs);
  104.         mcrypt_module_close($td);
  105.         $lastc=ord($outs[strlen($outs)-1]);
  106.         $validpad=1;
  107.         for ($i=0;$i<$lastc;$i++)
  108.             if (ord(substr($outs,strlen($outs)-1-$i,1))!=$lastc)
  109.                 $validpad=0;
  110.         if ($validpad==1)
  111.             $outs=substr($outs,0,strlen($outs)-$lastc);
  112.         $crc=substr($outs,strlen($outs)-4);
  113.         $crch="";
  114.         for ($i=0;$i<4;$i++)
  115.             $crch.=str_pad(dechex(ord($crc[$i])),2,"0",STR_PAD_LEFT);
  116.         $outs=substr($outs,0,strlen($outs)-4);
  117.                 $crc=str_pad(dechex(crc32($outs)),8,"0",STR_PAD_LEFT);
  118.         if ($crch!=$crc)
  119.             return "";
  120.         $outs=str_replace("&","%26",$outs);
  121.         $outs=str_replace("=","%3D",$outs);
  122.         $outs=rawurldecode($outs);
  123.         $outs="CRYPTO=1&".$outs;
  124.         return $outs;
  125.     }
  126.  
  127.  
  128.  
  129.     // Példa a használatra
  130.  
  131.    
  132.     $cleartext="PID=IEB0001&CRYPTO=1&MSGT=10&TRID=1234123412341234&UID=IEB00000001&LANG=HU&TS=19700101000000&AUTH=0&AMO=10000&URL=http://localhost/";
  133.     echo "Cleartext: ".$cleartext."<br>";
  134.     $crypto=ekiEncodeUrl($cleartext,"IEB.des");
  135.     echo "Crypted: ".$crypto."<br>";
  136.     $cleartext2=ekiDecodeUrl($crypto,"IEB.des");
  137.     echo "Cleartext: ".$cleartext2."<br>";
  138.    
  139.  
  140.  
  141. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement