65.9K
CodeProject is changing. Read more.
Home

Free PHP Encoder Application

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (18 votes)

Aug 4, 2009

CPOL

2 min read

viewsIcon

141905

downloadIcon

12984

A free PHP encoder application.

Introduction

This free PHP Encoder Application allows to encode PHP scripts before distributing them. It is all PHP compatible. The encoded scripts are 100% PHP featured, and works on all servers including Windows, Linux, FreeBSD, MacOSX, and others. It is possible to combine encoded and Open Source scripts in one site. The usage of encoded scripts is transparent to your visitors.

This tool allows you to encode immediately, without any installations.

Background

The PHP Encoder Application is a powerful application developed by Illumination to protect your source code. This software will encrypt PHP source code using System.Text.Encoding of the .NET Framework. The PHP code that is encoded by this application is protected against unauthorized modification and theft. The encoding also prevents hackers from looking into your code to find security weaknesses.

Using the code

Now, we are looking for a way to encrypt the PHP code. In C#, you need to have two base functions:

public string Decrypt64(string strToDecrypt)
{
    try
    {
        byte[] decodedByteArray =
                      Convert.FromBase64CharArray(strToDecrypt.ToCharArray(),
                                                    0, strToDecrypt.Length);
        return Encoding.UTF8.GetString(decodedByteArray);
    }
    catch (Exception ExceptionErr)
    {
        throw new System.Exception(ExceptionErr.Message,
            ExceptionErr.InnerException);
    }
}

and:

public string Encrypt64(string strToEncrypt)
{
    try
    {
        byte[] bytInput = Encoding.UTF8.GetBytes(strToEncrypt);
        return Convert.ToBase64String(bytInput);
    }
    catch (Exception ExceptionErr)
    {
        throw new System.Exception(ExceptionErr.Message,
            ExceptionErr.InnerException);
    }
}

Please download the source code and look at the IEncodePHP class.

  1. Write an error log or any information in the log file. In this article, I write the log file to Log.txt:
  2. string LogFileName = "Log.txt";
    FileInfo f = new FileInfo(LogFileName); 
    StreamWriter writeLog = f.CreateText(); 
    IEncodePHP iEncodePHP = new IEncodePHP(writeLog);

    The constructor of the IEncodePHP class for the write log:

    StreamWriter writeLog;
    
    public IEncodePHP(){}
    
    public IEncodePHP(StreamWriter prmWriteLog)
        : base()
    {
        this.writeLog = prmWriteLog;
    }
  3. Create all files and folders in the target folder like the source folder by following a rotation method:
  4. public void GetFileInDirectory(string encodePath, int level, string decodePath)
    {
        DirectoryInfo dir = new DirectoryInfo(encodePath.Trim());
        DirectoryInfo[] directory = dir.GetDirectories();
        FileInfo[] bmpfiles = dir.GetFiles("*.*");
    
        int totalFile = bmpfiles.Length;
        foreach (FileInfo f in bmpfiles)
        {
            string fileName = f.Name.ToString();
            long lengthOfFile = f.Length;
            DateTime createTimeFile = f.CreationTime;
            FileAttributes fileAttributes = f.Attributes;
            string sourceFilePath = encodePath + @"\" + fileName;
            string encodeFilePath = decodePath + @"\" + fileName;
            if (File.Exists(sourceFilePath))
            {
                if (!File.Exists(encodeFilePath))
                {
                    CreateFile(sourceFilePath, encodeFilePath);
                }
            }
        }
    
        foreach (DirectoryInfo f in directory)
        {
            string folderName = f.Name.ToString();
            DateTime createTimeFile = f.CreationTime;
            FileAttributes folderAttributes = f.Attributes;
    
            level++;
            string strEncodepath = encodePath + @"\" + folderName;
            string strDecodePath = decodePath + @"\" + folderName;
            //Create decode path
    
            if (!File.Exists(strDecodePath))
            {
                CreateFolder(decodePath, folderName);
            }
            GetFileInDirectory(strEncodepath, level, strDecodePath);
        }
    }
    1. Create a folder:
    2. private void CreateFolder(string parentPath, string folder)
      {
          try
          {
              string path = parentPath + @"\" + folder;
              if (!File.Exists(path))
              {
                  DirectoryInfo dir = new DirectoryInfo(parentPath);
                  dir.CreateSubdirectory(folder);
      
              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
    3. Create a file: Check that the file is a PHP file. If the file is a PHP file, then create a new file, else copy the source files to the target files.
    4. private void CreateFile(string sourceFilePath, string encodeFilePath)
      {
      
          string sourceExt = GetExtOfFile(sourceFilePath);
          string[] arrExtAllow = { "php", "inc" };//review the rem code // of js
      
          if (!IsAllowEncode(arrExtAllow, sourceExt))
          {
              File.Copy(sourceFilePath, encodeFilePath, true);
          }
          else
          {
              FileInfo fi = new FileInfo(encodeFilePath);
              FileStream fstr = fi.Create();
              fstr.Close();
      
              FileInfo f = new FileInfo(encodeFilePath);
              StreamWriter w = f.CreateText();
      
              StreamReader SR;
              SR = File.OpenText(sourceFilePath);
              encodeFile(SR, w);
              w.Close();
          }
      }
  5. Encode the PHP code:
  6. private string EncodePHP(ArrayList arrToEncode)
    {
        this.writeLog.WriteLine(ConvertToString(arrToEncode));
        string strToEncode = ConvertToString(arrToEncode);
        strToEncode = strToEncode.Replace(IlluminationConstants.newLineScript, "");
        string strEncode1 = EncodePHP(objCrypto.Encrypt64(strToEncode));
        this.writeLog.WriteLine(strEncode1);
        for (int i = 0; i < 4; i++)
        {
            strEncode1 = EncodePHP(objCrypto.Encrypt64(strEncode1));
        }
        return EncodePHP(objCrypto.Encrypt64(strEncode1));
    }
    
    private string EncodePHP(string strEncoded)
    {
        string variable1 = IRandom.GetRandomString(4, false, false);
        string variable2 = IRandom.GetRandomString(3, false, false);
        string variable3 = IRandom.GetRandomString(2, false, false);
    
        string startVariable = " $" + variable1 + " = '";
        string endVariable = "';";
        string strDecode = "$" + variable3 + " = '$" + variable2 +
            " = base64_decode($" + variable1 + "); eval($" + variable2 + ");';";
        string strEval = "eval($" + variable3 + ");";
        strEncoded = startVariable + strEncoded + endVariable + strDecode + strEval;
        return strEncoded;
    }

How to encode a PHP file using the Illumination tool?

You can run the Illumination tool by specifying the source directory and the target directory as follows:

  1. Click Browse... in the source to find a folder that contains PHP files.
  2. Click Browse... in the target to find a destination folder that you want the encoded file to be located in.
  3. Click the "Encode" button to encode all PHP files in your selected folder.
  4. Wait for the Congratulation dialog and enjoy your encoded file.

When you start with the "Encode" button, the Illumination tool will encode the files in the source-directory and save the results in the target-directory.

Points of interest

In this tutorial, you learned about the many features of the Illumination tool and how to use them to quickly protect your PHP scripts. Additional technical information is available from the Illumination blog. Visit the following link for more information: http://vn.myblog.yahoo.com/quangw5000.