Encrpy & Decrypt your Message using PHP

Wednesday, March 14, 2012 0 comments


Today thought of writing simple program in PHP implementing Julius Caesar Encryption / Decryption technique. In this encryption character are shift by certain numbers i.e for e.g. if shifted by 3 bits.In encrytion A will be replaced by D ,similarry B-E,C-F,.................. and vice versa for decrpytion. This is a simple algortihm. If you have an object oriented principle it will be piece of cake for you to understand.

Here's the code for it


 class CaesarCipher
 {

  const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

  protected $encrypt_sequence = array();
  protected $decrypt_sequence = array();

  public function __construct($seed = 1)
  {
   $total_chars = strlen(self::CHARS);

   // Normalize seed
   $seed = $seed % $total_chars;

   // Generate encryption and decryption sequences on the basis of seed passed 
   for ($i = 0; $i < $total_chars; $i++)
   {
    $src = substr(self::CHARS, $i, 1);
    $dst = substr(self::CHARS, ($i + $seed) % $total_chars, 1);
    $this->encrypt_sequence[$src] = $dst;
    $this->decrypt_sequence[$dst] = $src;
   }

   // Also add space
   $this->encrypt_sequence[' '] = ' ';
   $this->decrypt_sequence[' '] = ' ';
  }

  
/*
After generating the sequence for decryption of message we simply replace each bit of message with some sequence generated. Here I have used strstr function which replace each character of message with another character. 
*/


public function encrypt($value)
  {
   $value = strtoupper($value);
   return strtr($value,$this->encrypt_sequence );
  }

  public function decrypt($value)
  {
   $value = strtoupper($value);
   return strtr($value,$this->decrypt_sequence );
  }


/*This simply returns the array as array being protected it is not accessible to others i.e. not in this package so a public function is created to return array
*/ 
public function getEncryptSequence()
  {
   return $this->encrypt_sequence;
  }
  public function getDecryptSequence()
  {
   return $this->decrypt_sequence;
  }

  }


  // Run cypher

/*
*Although Random value is used you can use one constant value inorder to encrypt and decrypt you *message in future also, or remeber the seed value while encrypting and decrypting and make changes in 
* seed value for correct output
*/ 
$seed = mt_rand(1, 35);
  $cipher = new CaesarCipher($seed);

  $source = 'ADD YOUR MESSAGE HERE TO ENCRYPT';

  $encrypted = $cipher->encrypt($source);
  $decrypted = $cipher->decrypt($encrypted);



  // Print results
  echo "
CAESAR CIPHER (seed={$seed})\n\n";

  echo "Source:    {$source}\n";

  echo "Encrypted: {$encrypted}";

  echo "   ".($encrypted === $source ? "NOT ENCRYPTED :(" : "ENCRYPTED :)")."\n";



  echo "Decrypted: {$decrypted}";

  echo "   ".($decrypted === $source ? "MATCHES SOURCE! :)" : "DOES NOT MATCH SOURCE :(")."\n";



  echo "\n\n";





  // Print encryption/decryption sequences

  echo "Encryption:";

  foreach ($cipher->getEncryptSequence() as $k => $v)

  {

  if ($k === ' ') continue; // Don't display space

  echo " {$k}>{$v}";

  }

  echo "\n";



  echo "Decryption:";

  foreach ($cipher->getDecryptSequence() as $k => $v)

  {

  if ($k === ' ') continue; // Don't display space

  echo " {$k}>{$v}";

  }

  echo "\n";








0 comments:

Post a Comment

Recent Posts

WebInfo

 

©Copyright 2011 WebInfo