Read Any Magnetic Strip Card With a Square Reader and an Android Device

194,317

94

37

The Square credit card reading device is basically just a magnetic read head, resistor, and microphone connection.  By implementing an algorithm to decode the standardized audio encoding, it's possible to read arbitrary magnetic cards, not just credit cards.  Skip to the end to find a link to my completed app.

Step 1: Understand the Format

The encoding for magnetic stripe data follows a common standard.  The magnetic stripe consists of 3 physically separated "tracks". Track 1 is closest to the bottom of the card, and track 3 is the highest. Square's reader is positioned to read track 2. Track 2 is the most commonly used track, but most credit cards also use track 1. Track 2 includes card numbers and expiration dates. Track 1 includes that plus names. There may be other data too, depending on the particular card. These tracks are specced to be .11 inches wide, so to read track 1 with Square's reader, we just need to reposition the stripe so that track 1 is lined up with the read head.

Data in each track is encoded via magnetic domain flipping. Long story short: The series of domain flips encodes a waveform, that waveform is interpreted as binary. A binary 0 in this encoding is some arbitrary frequency. A 1 is twice that frequency.

The data starts with a set of leading zeros to establish the base frequency.  After a variable number of zeros, the start sentinel appears.  For track 2, the start sentinel is ";".  Each character is encoded as a integer with the least significant bits first.  For track 2, each character consists of 4 data bits and 1 parity bit.  The parity bit is set for each character so that the number of 1s is odd.  If you add 48  (the ASCII encoding for "0") to the integer value for each character, you get the ASCII character to display.  Other than the digits "0" through "9", track 2 can also encode some other characters, including ";" (start sentinel), "=" (field separator), and "?" (end sentinel). ":", "<", and ">" are not used much in practice.

Step 2: Make a Shim to Read Track 1

Track 1 of a magnetic card is .11inches closer to the edge of the card than track 2.  Since the Square reader is set up to read track 2, if we stick something in the reader to raise the card by .11 inches, the read head will be aligned with track 1 rather than track 2.

You can create a shim by cutting a .11 inch strip from another card.  I have also found that the twist ties from cheap garbage bags are just about right too.

Step 3: Record Some Audio

As far as your Android phone is concerned, the Square reader is just a microphone.  So to get data from a card, we need to record audio.  Refer to other Android documentation (such as this tutorial: http://eurodev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html) for detailed instructions, or use RhombusLib (see links at the end).

Here's some java code to start recording audio in an Android app:

AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
       frequency, channelConfiguration,
       audioEncoding, bufferSize);
  audioRecord.startRecording();

While recording, we need to continually read data from the recorder and place it into a buffer.

// Create a DataOutputStream to write the audio data
     ByteArrayOutputStream os = new ByteArrayOutputStream();
     BufferedOutputStream bos = new BufferedOutputStream(os);
     DataOutputStream dos = new DataOutputStream(bos);
 
     short bufferVal;
     short[] buffer = new short[bufferSize];

     while(recording){
       bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
       for (int i = 0; i < bufferReadResult; i++){
        bufferVal = buffer[i];
        dos.writeShort(buffer[i]);
       }
     }
     dos.close();
     byte[] audioBytes = os.toByteArray();


The above code is extracted and simplified from RhombusLib.  After recording, you'll have an array of bytes representing the samples from the microphone, ready to be analyzed.

Step 4: Decode the Audio

So, now we've got a bunch of audio on our device. How do we decode it? I based my code on an Android tutorial which shows how to record data and then play it back. In my case, I made sure to save that audio as 16bit PCM encoded. I sampled at 44100hz. On Android (and elsewhere, I suppose) 16bit PCM data means that each sample is a signed 16bit value. Since we only care about the frequency, we only need to care about how much time there is between "zero-crossings". A zero-crossing is when the signal goes from postive to negative or vice-versa. A 0 bit will be represented by the space between 2 crossings, and a 1 will have an extra crossing in approximately the same time period.
Card data in each track starts off with some (variable) number of 0s, to establish the base frequency. What I did was listen for the first sample above a certain "quiet" threshold, then count the number of samples between zero-crossings. That number becomes the base value for a 0. Since these cards are hand-swiped, the actual frequencies will change somewhat from the start of the scan to the end. So, I made a simple method which determines if the number of samples since the last zero-crossing is closer to the base frequency or twice the base frequency (half the base number of samples). It then adjusts the expected base frequency accordingly. This works well, so long as the changes between any two logical bits are fairly small. And they almost certainly will be.

To detect a zero-crossing, we need to look at the sign of each sample and compare it to the sign of the previous sample.  If they differ (one positive, one negative) then the signal crossed 0 between those samples.

The basic algorithm is to iterate through the byte array, extracting samples.  Count the number of samples between zero-crossings, and compare the count to the expected count for a 0 or 1.

Okay, after some hand-waving, we now have a binary sequence of data, which we want to turn back into ASCII. The most common encoding (and the only one I wrote a handler for) encodes each character as some number of bits, plus one parity bit. In the case of track 2, that's 4 bits for the character, and 1 for parity, making 5 bit groups. The bits are read from least significant to most, with the parity bit last. The parity bit is set to make the number of 1s in the group odd. In my implementation, I just disregard the parity bit, but it would help determine whether the read was good or not. In track 1, it's 6 bits for the character, plus the parity.
The character set of the tracks differ too, but both are subsets of ASCII with some offset. In the case of track 2, which only encodes some symbols and digits, the character set starts at 48, which is the ASCII code for "0". So if we get 0,0,0,0,1 as our character, we turn that into 0, add 48, and get 48. Similarly, 1,0,0,0,0 is 1. 1+48 = 49 = ASCII "1".
For track 1, the character set starts with " " (space) which is ASCII 32. So we add 32 to the decoded numeric value and get our ASCII character. After that, we have the data, so all that remains is hooking up the UI glue.

Step 5: Wrapping It Up, Some Resources

Here are some resources that you might find useful.
My original blog post about this project: http://cosmodro.me/blog/2011/mar/25/rhombus-square-iskewedi/

A Phrack article explaining magnetic stripe encoding: http://stripesnoop.sourceforge.net/devel/phrack37.txt  

My open-source RhombusLib library: https://github.com/sshipman/RhombusLib 

The already-implemented Rhombus app which puts it all together: https://play.google.com/store/apps/details?id=me.cosmodro.app.rhombus  

Unfortunately, the new Square readers which include encryption do not provide the raw data necessary to decode in this manner.  It should be possible to construct your own reader using a magnetic read head, resistor, and TRRS headphone jack.  But that's another project.
Some Android phones have noisier audio recording hardware than others.  RhombusLib does not work on all devices right now, though I am working on improving the error correction.  

Share

    Recommendations

    • First Time Author

      First Time Author
    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Big and Small Contest

      Big and Small Contest

    37 Discussions

    0
    None
    jlaveauchris12341

    Reply 1 year ago

    Hi Chris. I'd like to take up your offer of help. I have a few magnetic card readers that I am trying to use on my Surface Tablets and I need some help getting them to work. They are Surface Pro 3 tablets running Windows 10 (1 of them is running Windows 8.1). I have read a couple of forums that mentioned about adjusting the gain on the microphone settings but this didn't work. I have a Square card reader, 1 from Payanywhere.com and another chinese one that I got. Reading through this forum I am starting to understand what the problem is, I just have no idea how to fix it. I am not a programmer but I am fairly knowledgeable with computers. Can you help?

    0
    None
    shidocain

    1 year ago

    i am from venezuela ...we like use the square reader to our university...your lib work??? we buy the square dongle but in the rhombus app dont see anything...there is a sdk files or something we can use to read the cards with the hardware

    1 reply
    0
    None
    sshipman0shidocain

    Reply 1 year ago

    No, Square readers are all encrypted now, which means 3rd party libraries like Rhombus cannot make use of them.
    You can get unencrypted readers from alibaba, but I am no longer supporting Rhombus. Feel free to use RhombusLib on github as a base for your own code.

    0
    None
    ScottT73

    2 years ago

    Is it possible to use the square reader as part of a library check-out system? We are a not-for-profit literacy foundation working in Haiti. Built a new library and need a cheap reader system to read a library card and book card.

    3 replies
    0
    None
    michaelsandiego5959ScottT73

    Reply 2 years ago

    yes scott very cheap reader I use simple and small spy camera this is a sample at vid.me search soundwavesmp4

    0
    None
    humoogousScottT73

    Reply 2 years ago

    I use http://www.booksource.com/for my class library.

    I just scan the ISBN code with my iphone/ipad. Most books are imported correctly. I believe it also works with barcode readers on the computer. I hope this helps

    0
    None
    michaelsandiego5959

    2 years ago

    I already made this software.. u mean a soundwaves I try also a small spycamera I take off the small mic and then I change it to magnetic reader then I swaaap it and spycamera record it then I convert it to software I see my complete track2...

    0
    None
    michaelsandiego5959

    2 years ago

    contact me bro i have a software for that audio reader michaelsandiego59@yahoo.com

    0
    None
    blakeanator

    3 years ago on Introduction

    You suggested one unencrypted reader 3 months ago (http://www.aliexpress.com/item/free-shipping-Mobile-Credit-Debit-Card-Reader-for-Apple-Android-iOS-3-5mm-Black-and-white/1071970592.html) but that one is no longer available. Is there another we can use?

    0
    None
    enigmaprime

    3 years ago on Introduction

    Is there a way to implement this to read identification cards. Specifically to get the age of the ID holder.

    0
    None
    decodewav

    3 years ago on Introduction

    hi, im professional decoder, i can decode any sound file to track2!

    if you need a good decoder contactme
    this is im ICQ: 655803449
    jabber : decode.wav@jabbim.cz

    0
    None
    gtoal

    3 years ago on Introduction

    Do you know if the paypal reader is encrypted? Why did they start encrypting these readers? I can't see a valid security reason, so I'm guessing it is specifically to stop rival companies' apps from using their free hardware?

    0
    None
    sonry101

    3 years ago on Introduction

    Hi! do you have an example of how to implement the lib? Thanks in advance!

    0
    None
    CraigB11

    3 years ago on Introduction

    I was looking for a general magnetic stripe reader and thought that this might be a solution. You mention that this won't work with a newer square device - how would if my square reader is the newer model. Trying to run this on the galaxy S5 and it doesn't seem to be reading...

    3 replies
    0
    None
    sshipman0CraigB11

    Reply 3 years ago on Introduction

    It's been years since Square has produced unencrypted devices. If your device isn't working, that is probably the cause. Sorry. There are unencrypted readers you can order from China. They should only be a few dollars from alibaba or dx.com.

    If you really want to see whether yours is encrypted, you could open it up and look for a battery and PCB/chip. The unencrypted readers were just a read head and some wires to a headphone jack.

    0
    None
    sshipman0CraigB11

    Reply 3 years ago on Introduction

    I doubt it. I'd never heard of them before, but a quick google search says that they encrypt data, and it seems they also support chip and pin, which is much more involved.

    Here's a link to the unencrypted chinese readers I've bought and used successfully: http://www.aliexpress.com/item/free-shipping-Mobile-Credit-Debit-Card-Reader-for-Apple-Android-iOS-3-5mm-Black-and-white/1071970592.html