Contact Us

Scripts: PHP :: PHP security and encryption >> Library Article #12

Developer's Section

Encrypting and Decrypting a String using Base 64 Encoding
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (2629)

In this tutorial we develop a function to encrypt and decrypt any string using base 64 encoding and decoding.

If your using PHP > 4 then we should have available the methods base64_encode, as well as base64_decode to create a simple encryption algorithm. The script shown below takes as input a string value and a key (optional) to encrypt and decrypt the string using base 64 encoding.

Code for Encryption:

 Code Snippet 1

function encrypt_string_64($inputValue, $key) 

  $encryptedString = ''; 
  for($i=0; $i<strlen($inputValue); $i++) { 
    $char = substr($inputValue, $i, 1); 
    $keychar = substr($key, ($i % strlen($key))-1, 1); 
    $char = chr(ord($char)+ord($keychar)); 

  return base64_encode($encryptedString); 

Code for Decryption:

 Code Snippet 2

function decrypt_string_64($encoded64Input, $key)

  $decryptedString = ''; 
  $encoded64Input = base64_decode($encoded64Input); 
  for($i=0; $i<strlen($encoded64Input); $i++) { 
    $char = substr($encoded64Input, $i, 1); 
    $keychar = substr($key, ($i % strlen($key))-1, 1); 
    $char = chr(ord($char)-ord($keychar)); 

  return $decryptedString; 

See other Scripts in PHP security and encryption

Submit Your Scripts:

If you would like to have your PHP scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]