Simple solution to store credit card data

I has been getting request to build web pages and applications that would receive credit card for years.

And I always rejected that as I didn’t want to get responsibility of getting those data leaked.

But recently I got to situation that I couldn’t refuse it anymore.

So I was looking how to do it safe and I got an idea – to use SSL mechanism and keep data stored at the server in encrypted form.


1) you will generate private key – doesn’t need to be signed and it’s for free.

There are many manuals but you can use this:

openssl genrsa -des3 -out webpage.key 2048

Put in some pass phrase.

And then:

openssl req -new -key webpage.key -out webpage.crt

You don’t need to enter any info, just press enter.

And last step:

openssl x509 -req -days 36500 -in webpage.csr -signkey webpage.key -out webpage.crt

Result are files webpage.crt, webpage.csr, webpage.key.

Make sure that at the production server where you have the webpages is located only webpage.crt!!

2) create page for user to enter the data. The page should be accessible only over HTTPS (I like RapidSSL).

3) Don’t save data from the credit card to DB, but call my function creditCardEncrypt:

function XXXCreditCard($creditCard) {
 $creditCard=str_replace(' ','',$creditCard);
 for ($i=0; $i<$l-4; $i++) {
 if ($i % 4 == 3)
 $res.=" ";
 return $res.substr($creditCard, $l-4);
function creditCardEncrypt($creditType, $creditCard,$cardHolder,$expiration,$cvv) {
 $text="Credit Type: ".$creditType."\n".
 "Credit Card: ".$creditCard."\n".
 "Card Holder: ".$cardHolder."\n".
 "Expiration: ".$expiration."\n".
 "CVV: ".$cvv;
 openssl_public_encrypt($text, $crypttext, $pub_key);

 $res = new stdClass();
 return $res;

Result of this function is an object, with variables:

creditCard – credit card number where all except last 4 digits are replaced with X
rawData – raw encrypted data
base64 – encrypted data in base 64 format. Such data can be even send over an email.


$res=creditCardEncrypt('VISA', '1233 1233 3434 1231','Pavel Jiri Strnad', '12/23', '123');

NOTE: in my code is webpage.crt is expected to be at same folder. Place it anywhere you like and change the path.


4) Now you can store to the db values. You can even send the Credit Card data in base64 over email. It looks like this:



5) Now the decoding. It’s not secured to have decoding at the production server. For decoding you need SSL Key and even it’s secured with the password attacker can use brute force and capture the pass phrase once you will type it.

The decoding must be done at some private server of the customer. If he don’t have any, you just need for him a PHP to run decode.

This function creditCardDecrypt will decode the base64 data:

function creditCardDecrypt($base64, $passphrase) {
 $text = base64_decode($base64);
 $res1= openssl_get_privatekey($priv_key,$passphrase); 
 openssl_private_decrypt($text, $res, $res1);
 return $res;


and the result looks like this:

Credit Type: VISA
Credit Card: 1233 1233 3434 1231 
Card Holder: Pavel Jiri Strnad 
Expiration: 12/23 
CVV: 123


You can create just simple page where use will put the base64 text from an email and you will decode him the credit card data.

This whole concept is for small businesses where they will proceed the credit card manually.

Good on this solutions is that no attacker can steal your credit card backward or via SQL injection.

Weak point of this whole solution is that that user still enter credit card data to your web pages. If the attacker will take control over your server he can’t steal all the credit card data backward, but he can steal any new credit card data entered. So make sure you run security checks on your server and for example run every hour sha1 sum check on the pages to collect and save the credit card data – if they will be changed, I suggest to delete them and notify your self that server was compromised.



Leave a Reply

Your email address will not be published. Required fields are marked *