Simple Obfuscation of .txt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abe
Forum Newbie
Posts: 1
Joined: Tue Nov 12, 2013 11:04 pm

Simple Obfuscation of .txt file

Post by abe »

I am trying to use this code for simple obfuscation. But I presume nothing is returned to the function. Here's the code:

Code: Select all

<?php
    $source = "setup.txt";
    $destination = "setup.ini";
    function enc($temp)
    {
      $len = strlen($temp);
      for($i=0;$i<$len;$i++)
      {
        $ch = $temp[$i];
        $ch = ~$ch;
        $temp[$i]=$ch;
      }
    }
    function createid($src,$dest)
    {
      $handle_1 = fopen($src, "rb");
      $handle_2 = fopen($dest,"rb");
      while(!feof($handle_1))
      {
        $Byte = fgetc($handle_1);
        sprintf($tmp,"%c",$Byte);
        enc($tmp);
        $len = strlen($tmp);
        $newByte = $tmp[0];
        fputs($newByte,$handle_2);
      }
      fclose($handle_1);
      fclose($handle_2);
    }
    createid($source,$destination);
?>
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Simple Obfuscation of .txt file

Post by Eric! »

There's a couple of issues with the code, but a better question is why do you think this is necessary? Is this for something that is not web related?

To help you with the code:
After the for loop in the enc() function you need: return $temp; (otherwise as you said nothing gets returned.)
You are reading these files bytewise, but then treating them like an array of bytes...why?
The bitwise operator ~ is going to change some standard ASCII characters into binary values which your .txt file system might not support or may get garbled.
Post Reply