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

202,203

97

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.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## 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){
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.

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

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

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.

Participated in the
Make It Real Challenge

152 7.2K
10 829
21 2.7K

## 37 Discussions

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?

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

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.

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

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

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

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...

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

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?

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

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

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?

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

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

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.

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.