Home arrow PHP arrow Page 2 - PHP Encryption and Decryption Methods

Base 64 Encoding and Compression/Decompression Techniques - PHP

PHP encryption is a method of obfuscating scripts in such a way that it offers additional protection and prevents unauthorized editing of the scripts. This article discusses both encryption and decryption.

  1. PHP Encryption and Decryption Methods
  2. Base 64 Encoding and Compression/Decompression Techniques
  3. PHP Eval Function in Encoding Methods
  4. Case Example: Encoding a Working PHP Script
By: Codex-M
Rating: starstarstarstarstar / 13
November 03, 2009

print this article



PHP widely supports the base 64 encoding technique because it is ubiquitous in programming and IT practices. If you are interested in learning other applications of base 64 encoding, you can find a number of references.

In PHP, to convert a certain string to Base 64, you use a built-in function, namely base64_encode()


$string = 'The quick brown fox jumps over the lazy dog';

$base64 =base64_encode($string);

echo $base64;


The above script converts the string “The quick brown fox jumps over the lazy dog” to base 64 format, which is:


Applying the above concept to PHP scripts, we should treat the PHP code as a “string” except for the PHP tags. For example, say you have written a simple PHP script below:



echo $sum;


To encrypt is to encode only:


echo $sum;

The equivalent base 64 script is:



echo $sum;');

echo $a;


This will output: JHN1bT01KzU7DQplY2hvICRzdW07

According to the PHP.net page: http://www.php.net/base64_encode , base 64 encoded data takes up to ~33% more space than the usual data when it is not encoded. This means that the encoded/encrypted PHP script will take up a lot of space as compared to the same script when unencrypted. Because of this, we need to compress the encoded base 64 data. See file size comparison below:


In PHP, two common functions are used to compress and decompress strings. These are the gzinflate() and gzdeflate() functions. Below is an example script for compressing a string using gzdeflate function:


//compress string

$compress = gzdeflate('The quick brown fox jumps over the lazy dog', 9);


$decompress = gzinflate($compress);

echo $decompress;


This is effective in reducing the file size of base 64 encoded data. Note that to retrieve or decompress, you use the opposite function (gzinflate). The figure “9” in the gzdeflate function means maximum compression level.

>>> More PHP Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: