TRIPLE DES encryption/decryption using php

Working with the back-end APIs would require you to apply additional security layers such as Data Encryption. This is to ensure a secured handshake to every client application that will consume it. One of the common problems is how you will translate encryption to a different language such as C# to Php. Today in this article, I will create a sample for TRIPLE DES encryption/decryption using PHP.

I have encountered this problem in translating C# triple des encryption to PHP. Here is the code that I found on the internet that solved my problem. I hope this helps someone who is also looking for this same problem.

Please also visit my Blog Page for more programming topics.

Let’s Start:

First, Create a php file inside htdocs folder if you are using XAMP or WAMP navigate to C:\xampp\htdocs.

Below are the codes for TRIPLE DES encryption/decryption using PHP.

Encryption:

function Encryption($data, $secret)

{  

           //Generate a key from a hash    

            $key = md5(utf8_encode($secret), true);    

            $data2 = utf8_encode($data);     

            $iv =utf8_encode("aUrxt1ry");    

            //Take first 8 bytes of $key and append them to the end of $key.   

            $key .= substr($key, 0, 8);       

            //Pad for PKCS7    $blockSize = mcrypt_get_block_size('tripledes', 'cbc');      

             //Encrypt data    $encData = mcrypt_encrypt('tripledes', $key, $data2, MCRYPT_MODE_CBC, $iv);     

             return urlencode(base64_encode($encData));   

}

Decryption:

function Decryption($data, $secret)

{     

           //Generate a key from a hash    

            $data2 = urldecode($data);       

             $iv =utf8_encode("aUrxt1ry");   

             $key = md5(utf8_encode($secret), true);     

             //Take first 8 bytes of $key and append them to the end of $key.    

              $key .= substr($key, 0, 8);     

              $data3 = base64_decode($data2);   

             return $data4 = mcrypt_decrypt('tripledes', $key, $data3, MCRYPT_MODE_CBC, $iv);

}

To use and try this function

To execute the function above you can directly echo the result by simply using the code below.

echo Encryption("The quick brown fox","Q4Bz89FGc");echo "<br/>";

echo Decryption("lDWFOLQFkmXkbrmEa0i1OsM2Dqnv5b7T","Q4Bz89FGc")

Code Summary:

This is the full source code. You may create a sing PHP file, paste the code below, and try running it in your Apache server. You can use Xamp for a cross-platform server or WAMP for Windows.

<?php 

function Encryption($data, $secret)

     {

            //Generate a key from a hash   

              $key = md5(utf8_encode($secret), true);   

              $data2 = utf8_encode($data);    

              $iv =utf8_encode("aUrxt1ry");  

              //Take first 8 bytes of $key and append them to the end of $key.   

              $key .= substr($key, 0, 8);      

              //Pad for PKCS7    

             $blockSize = mcrypt_get_block_size('tripledes', 'cbc');     

              //Encrypt data   

             $encData = mcrypt_encrypt('tripledes', 

             $key, $data2, MCRYPT_MODE_CBC, $iv);     

             return urlencode(base64_encode($encData));  

     } 

function Decryption($data, $secret)

     {    

             //Generate a key from a hash   

               $data2 = urldecode($data);      

               $iv =utf8_encode("aUrxt1ry");  

               $key = md5(utf8_encode($secret), true);   

               //Take first 8 bytes of $key and append them to the end of $key.   

               $key .= substr($key, 0, 8);     

               $data3 = base64_decode($data2);  

               return $data4 = mcrypt_decrypt('tripledes', $key, $data3, MCRYPT_MODE_CBC, $iv);

      }  

echo Encryption("The quick brown fox","Q4Bz89FGc");echo "<br/>"; 

echo Decryption("lDWFOLQFkmXkbrmEa0i1OsM2Dqnv5b7T","Q4Bz89FGc")

?>