Parse Credit Card input from Magnetic Stripe Parse Credit Card input from Magnetic Stripe asp.net asp.net

Parse Credit Card input from Magnetic Stripe


See the Magnetic Stripe Card entry @ Wikipedia:


Track one, Format B:

  • Start sentinel — one character (generally '%')
  • Format code="B" — one character (alpha only)
  • Primary account number (PAN) — up to 19 characters. Usually, but not always, matches the credit card number printed on the front of the card.
  • Field Separator — one character (generally '^')
  • Name — two to 26 characters
  • Field Separator — one character (generally '^')
  • Expiration date — four characters in the form YYMM.
  • Service code — three characters
  • Discretionary data — may include Pin Verification Key Indicator (PVKI, 1 character), PIN Verification Value (PVV, 4 characters), Card Verification Value or Card Verification Code (CVV or CVK, 3 characters)
  • End sentinel — one character (generally '?')
  • Longitudinal redundancy check (LRC) — one character (Most reader devices do not return this value when the card is swiped to the presentation layer, and use it only to verify the input internally to the reader.)

I hope the data is fake, otherwise Anyone could get the:

  • Name
  • Expiration Date
  • CVV

And I'm not sure but I think the credit card number (or # of possibilities) can be computed using the LRC.


I did you one better: I made a video showing how to do exactly this with ASP.Net/c#:

http://www.markhagan.me/Samples/CreditCardSwipeMagneticStripProcessing

Here is the section of code that you probably care about:

    protected void CardReader_OTC(object sender, EventArgs e)    {        bool CaretPresent = false;        bool EqualPresent = false;        CaretPresent = CardReader.Text.Contains("^");        EqualPresent = CardReader.Text.Contains("=");        if (CaretPresent)        {            string[] CardData = CardReader.Text.Split('^');            //B1234123412341234^CardUser/John^030510100000019301000000877000000?            PersonName.Text = FormatName(CardData[1]);            CardNumber.Text = FormatCardNumber(CardData[0]);            CardExpiration.Text = CardData[2].Substring(2, 2) + "/" + CardData[2].Substring(0, 2);        }        else if (EqualPresent)        {            string[] CardData = CardReader.Text.Split('=');            //1234123412341234=0305101193010877?            CardNumber.Text = FormatCardNumber(CardData[0]);            CardExpiration.Text = CardData[1].Substring(2, 2) + "/" + CardData[1].Substring(0, 2);        }    }

The complete code is on that website I linked above.


From what I can remember:

That is a two-track magnetic strip data - first track starts with % and ends with ?, the second track starts with ; and ends with ?. These are Start/End markers.

The first track is alphanumeric, the second track is numeric, and there is a third track which is numeric also (if my memory serves correct).

The data between the start/end markers can be variable depending on the recording density of the magnetic strip. The higher the density, the more it can be recorded on one track.

Using a regex to get at the data may not be a reliable method to pick out the information required.

And not all credit cards have exactly two tracks, some uses three tracks.