Basic Data Encryption via Base64

May 08, 2008

Security is important, especially to those application which deals with the sensitive data accross internet network. When a data is sent out from your computer, while it is on its way to the destinated terminal, it is exposed to the security threats which may either harm or sniff your data. If that is the case, precaution steps should be taken to minimized the possibilities of data stolen/sniffed.

You may utilize the available security options such as using SSL or Certificates. Another way will be done in the application it self where the data is already encrypted before sealed with SSL or certificate.

For PHP, you may utilize the most common base64 conversion to secure your data.

base64_encode() function

base64_encode ( string $data )

This function accept one parameter which will be a string value. The function encode the given string with Base64 and return the value back to the caller in string format.

Below are the sample on how the function is being used:

$myencrypteddata = base64_encode(”This is a sample string”);

base64_decode() function

base64_decode ( string $data )

This function accept one parameter which will be a string value. The function decode the given string with Base64 and return the value back to the caller in string format.

Below are the sample on how the function is being used:

$myencrypteddata = base64_encode (”This is a sample string”);
$myencrypteddata = base64_decode($myencrypteddata);

Using the string that passed by the base64_encode seems to be secure, but it is actually not. This is due to there are many web tools available online which enable you to decode the encoded string. For example:

http://makcoder.sourceforge.net/demo/base64.php
http://base64-encoder-online.waraxe.us/

In order to have the data secured, you may form your own security algorithm by using base64 functions. There is a simple hint which you may refer:

  1. Add some alphabet into certain position of the data
  2. Encode it with base64_encode() function
  3. Add some alphabet into certain position of the string returned from the base64_encode() executed on option (2).
  4. Encode it with base64_encode() function again
  5. You can use the string returned from the base64_encode() funtion executed on option (4)

The above is the steps that you will need to encode/encrypt the data. You will need to know how to reverse it so that you can get back the raw data. The encoding will be rather simple if you understand your encoding algorithm. By referring to the above hints, the encoding is just a reverse steps start from (5) back to (1).

Practice will makes you understand more. You may download the sample source code here for further study and understand on the base64 functions.

Leave a Reply