php로 된 xor 연산 c로 컨버팅 하는거점 도와 주세요.

mrbaen의 이미지

//php 소스
function bytexor($a,$b)
{
$c="";
for($i=0;$i<16;$i++)
{
$c.=$a{$i}^$b{$i};

//echo "$c \n";
}
return $c;
}

function decrypt_md5($msg,$key)
{
$string="";
$buffer="";
$key2="";

while($msg)
{
$key2=pack("H*",md5($key.$key2.$buffer));
$buffer=bytexor(substr($msg,0,16),$key2);
$string.=$buffer;
$msg=substr($msg,16);
}
return($string);
}

function encrypt_md5($msg,$key)
{
$string="";
$buffer="";
$key2="";

while($msg)
{
$i++;
$key2=pack("H*",md5($key.$key2.$buffer));
$buffer=substr($msg,0,16);
$string.=bytexor($buffer,$key2);
$msg=substr($msg,16);
}
return($string);
}

// 사용 예
$message = "☆━2008년*♡*┐┃설날부턴 ° ː│。°더많이°│ː 。사랑할께~┃└*♡* *‥─━★";
$key = "985786";

$encrypted = encrypt_md5($message, $key);
$decrypted = decrypt_md5($encrypted, $key);

echo "Encrypted = $encrypted";
echo " \n";
echo "Decrypted = $decrypted";
echo " \n";

--php 소스 끝 --

이걸 c(gcc)로 컨버팅 해야 하는데요.
bytexor 이 함수 변환하는데서 막혔어요.
c는 잘하지 못해서 비트 연산에 대해서 정리가 잘 안되네요.
고수님의 답변 부탁 드립니다.

즐프 하세요.

chadr의 이미지

c도 똑같습니다.
bytexor 함수가 두 배열을 받아서 각각 xor연산을 하는것 같습니다.

아래같은식으로 하시면 됩니다.

void bytexor(char buf[16], char a[16], char b[16])
{
  for(int i = 0; i < 16; i++)
  {
    buf[i] = a[i] ^ b[i];
  }
}

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.