Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- /*
- Titkosítás a saki protokoll alapján
- Paraméterek:
- $plaintext: a titkosítandó string
- $keyfile: a titkosításhoz szükséges kulcsfile neve
- Visszatérő érték:
- A titkosított üzenet
- */
- function ekiEncodeUrl($plaintext,$keyfile)
- {
- $f=fopen($keyfile,"r");
- $keyinfo=fread($f,38);
- fclose($f);
- $k1=substr($keyinfo,14,8);
- $k2=substr($keyinfo,22,8);
- $iv=substr($keyinfo,30,8);
- $key=$k1.$k2.$k1;
- $arr=split("&",$plaintext);
- $outs="";
- $pid="";
- for ($i=0;$i<count($arr);$i++)
- {
- if (strtoupper($arr[$i])!="CRYPTO=1")
- $outs.="&".$arr[$i];
- if (substr(strtoupper($arr[$i]),0,4)=="PID=")
- $pid=substr(strtoupper($arr[$i]),4,7);
- }
- $outs=substr($outs,1);
- $outs=rawurlencode($outs);
- $outs=str_replace("%3D","=",$outs);
- $outs=str_replace("%26","&",$outs);
- $crc=str_pad(dechex(crc32($outs)),8,"0",STR_PAD_LEFT);
- for ($i=0;$i<4;$i++)
- $outs.=chr(base_convert(substr($crc,$i*2,2),16,10));
- $pad=8-(strlen($outs) % 8);
- for ($i=0;$i<$pad;$i++)
- $outs.=chr($pad);
- $td=mcrypt_module_open("tripledes","","cbc","");
- mcrypt_generic_init($td,$key,$iv);
- $outs=mcrypt_generic($td,$outs);
- mcrypt_module_close($td);
- $pad=3-(strlen($outs) % 3);
- for ($i=0;$i<$pad;$i++)
- $outs.=chr($pad);
- $outs=base64_encode($outs);
- $outs=rawurlencode($outs);
- $outs="PID=".$pid."&CRYPTO=1&DATA=".$outs;
- return $outs;
- }
- /*
- Kititkosítás a saki protokoll alapján
- Paraméterek:
- $crypto: a kititkosítandó string
- $keyfile: a titkosításhoz szükséges kulcsfile neve
- Visszatérő érték:
- A kititkosított üzenet, vagy crc hiba esetén üres string
- */
- function ekiDecodeUrl($crypto,$keyfile)
- {
- $f=fopen($keyfile,"r");
- $keyinfo=fread($f,38);
- fclose($f);
- $k1=substr($keyinfo,14,8);
- $k2=substr($keyinfo,22,8);
- $iv=substr($keyinfo,30,8);
- $key=$k1.$k2.$k1;
- $arr=split("&",$crypto);
- $outs="";
- $pid="";
- for ($i=0;$i<count($arr);$i++)
- {
- if (substr(strtoupper($arr[$i]),0,5)=="DATA=")
- $outs=substr($arr[$i],5);
- if (substr(strtoupper($arr[$i]),0,4)=="PID=")
- $pid=substr(strtoupper($arr[$i]),4,7);
- }
- $outs=rawurldecode($outs);
- $outs=base64_decode($outs);
- $lastc=ord($outs[strlen($outs)-1]);
- $validpad=1;
- for ($i=0;$i<$lastc;$i++)
- if (ord(substr($outs,strlen($outs)-1-$i,1))!=$lastc)
- $validpad=0;
- if ($validpad==1)
- $outs=substr($outs,0,strlen($outs)-$lastc);
- $td=mcrypt_module_open("tripledes","","cbc","");
- mcrypt_generic_init($td,$key,$iv);
- $outs=mdecrypt_generic($td,$outs);
- mcrypt_module_close($td);
- $lastc=ord($outs[strlen($outs)-1]);
- $validpad=1;
- for ($i=0;$i<$lastc;$i++)
- if (ord(substr($outs,strlen($outs)-1-$i,1))!=$lastc)
- $validpad=0;
- if ($validpad==1)
- $outs=substr($outs,0,strlen($outs)-$lastc);
- $crc=substr($outs,strlen($outs)-4);
- $crch="";
- for ($i=0;$i<4;$i++)
- $crch.=str_pad(dechex(ord($crc[$i])),2,"0",STR_PAD_LEFT);
- $outs=substr($outs,0,strlen($outs)-4);
- $crc=str_pad(dechex(crc32($outs)),8,"0",STR_PAD_LEFT);
- if ($crch!=$crc)
- return "";
- $outs=str_replace("&","%26",$outs);
- $outs=str_replace("=","%3D",$outs);
- $outs=rawurldecode($outs);
- $outs="CRYPTO=1&".$outs;
- return $outs;
- }
- // Példa a használatra
- $cleartext="PID=IEB0001&CRYPTO=1&MSGT=10&TRID=1234123412341234&UID=IEB00000001&LANG=HU&TS=19700101000000&AUTH=0&AMO=10000&URL=http://localhost/";
- echo "Cleartext: ".$cleartext."<br>";
- $crypto=ekiEncodeUrl($cleartext,"IEB.des");
- echo "Crypted: ".$crypto."<br>";
- $cleartext2=ekiDecodeUrl($crypto,"IEB.des");
- echo "Cleartext: ".$cleartext2."<br>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement