free hit counter

Encrypt Credit Card Data for Storage in a Database Using PHP

One of the issues with working in the software-as-a-service industry is that credit card numbers often have to be stored locally in a database. Keeping it on file with your payment gateway alone has a few limitations. The business folks may request storage for various reasons such as:

  • Recurring subscriptions (often with variable amounts).
  • Customers wanting to keep their card on file for future purchases.
  • Transactional based software providers (such as cloud providers).
  • Customer service wanting to verify the card on file with a customer.
  • Managers wanting to authorize certain charges for employees.
encryption component in cakephp

encryption component in cakephp

Create a class ( name ? anything )

make sure mcrypt enable in your server

var $key =’H%&gdfoRE^#$@#dfesdfd;  

// * Do not change this key after we are in production unless you know what

If you change this key, no one can process * payments!

This function will encrypt the string that is passed to it 

  1. function enCrypt($data = null) {   
  2.     if ($data != null) {   
  3.   
  4.         $td = mcrypt_module_open(‘cast-256′‘ecb’);   
  5.         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);   
  6.         mcrypt_generic_init($td$this->key, $iv);   
  7.         $encrypted_data = mcrypt_generic($td$data);   
  8.         $encoded = base64_encode($encrypted_data);   
  9.         if (!mcrypt_generic_deinit($td) || !mcrypt_module_close($td)) {   
  10.             $encoded = false;   
  11.         }   
  12.     } else {   
  13.         $encoded = false;   
  14.     }   
  15.     return $encoded;   
  16. }   

 

* This function will de-crypt the string that is passed to it 

  1. function deCrypt($data = null) {   
  2.     if ($data != null) {   
  3.         // The reverse of encrypt.  See that function for details   
  4.         $data = (string) base64_decode(trim($data));   
  5.         $td = mcrypt_module_open(‘cast-256′‘ecb’);   
  6.         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);   
  7.         mcrypt_generic_init($td$this->key, $iv);   
  8.         $data = (string) trim(mdecrypt_generic($td$data));   
  9.         // Make sure the encryption modules get un-loaded   
  10.         if (!mcrypt_generic_deinit($td) || !mcrypt_module_close($td)) {   
  11.             $data = false;   
  12.         }   
  13.     } else {   
  14.         $data = false;   
  15.     }   
  16.     return $data;   
  17. }   

* This method will mask a credit card number  (eg . shos last 4 char  XXXXXXXXXXXX287)

  1. function maskCardNumber($cardNumber) {   
  2.     $cardArray = str_split($cardNumber);   
  3.     $length = count($cardArray);   
  4.     $maskedCardNumber = “”;   
  5.     // Mask all numbers except the last 4   
  6.     for ($i = 0; $i < $length -4; $i++) {   
  7.         $cardArray[$i] = ‘X’;   
  8.     }   
  9.     // Turn back into a string   
  10.     for ($i = 0; $i < $length$i++) {   
  11.         $maskedCardNumber = $maskedCardNumber . $cardArray[$i];   
  12.     }   
  13.     return $maskedCardNumber;   
  14. }   

Leave a Reply