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

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.  

Comments

author
chris12341 made it!(author)2015-02-11

For those of you who needs help get in touch with i can help you with almost everything you need

author
jlaveau made it!(author)2017-07-20

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?

author
sonry101 made it!(author)2015-04-20

Hi! do you have an example of the implementation of the lib?

author
shidocain made it!(author)2017-01-17

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

author
sshipman0 made it!(author)2017-01-17

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.

author
ScottT73 made it!(author)2016-03-20

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.

author
michaelsandiego5959 made it!(author)2016-08-09

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

author
humoogous made it!(author)2016-07-23

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

author
humoogous made it!(author)2016-07-23

http://www.booksource.com/

author
michaelsandiego5959 made it!(author)2016-08-09

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

author
michaelsandiego5959 made it!(author)2016-04-02

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

author
blakeanator made it!(author)2015-08-25

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?

author
enigmaprime made it!(author)2015-08-20

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

author
decodewav made it!(author)2015-07-25

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

author
gtoal made it!(author)2015-06-17

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?

author
sonry101 made it!(author)2015-05-17

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

author
CraigB11 made it!(author)2015-04-28

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

author
sshipman0 made it!(author)2015-04-28

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.

author
CraigB11 made it!(author)2015-04-28

Do you know if similar devices like one from beanstream would work?

author
sshipman0 made it!(author)2015-04-28

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

author
eried made it!(author)2013-10-04

Excelent instructable!

author
ashiq1986 made it!(author)2014-10-11

Hi Eried, Am from UAE, can I have any ideas of dumps? Coz I can do nice works....if u have 101 tracks, I would like to get it....please reply me

author
chris12341 made it!(author)2015-02-11

Ashiq please get in touch with i might be able help you. Thanks

author
track000000000009 made it!(author)2015-01-12

http://1wutrans3er.blogspot.com Dumps track1&2 with pin.

author
eried made it!(author)2014-10-13

No I don't

author
track000000000009 made it!(author)2015-01-12

http://1wutrans3er.blogspot.com Dumps track1&2 with pin.

author
sangram21 made it!(author)2014-05-05

Hi,

Can you send me me the sample code of implementing Rhombus Lib in android app?

I did not get the solution on google and I need to implement it in my android app ASAP.

author
StevenMarquis made it!(author)2014-01-04

I've purchased several square readers. They all have several ICs so its not just a head and a resistor! Perhaps the preamp in the smart phone can pull out the signal, but you may need an amp. I used a gain of 100 to bring the signal up to a good volt so maybe a preamp set at 20X might do the trick too. The actual voltage reading coming off the head is only a few mV at best.

author
calendo made it!(author)2013-06-24

Hi, first thanks a lot for the great tutorial!. I am trying to buy a square reader, however as you say in the last part of the tutorial I am afraid it would be one of the encrypted ones and it won't be useful. Any chance you can tell me the square reader you used to buy the same or a equivalent one?

Thanks!.

author
Merpyhead made it!(author)2013-03-11

Is there any way to be able to read the information placed on all three tracks... at once?

author
sshipman0 made it!(author)2013-03-11

Not with the square reader, or any reader that simple. There's only one microphone channel in the headphone jack, so to get multiple signals in you'd have to multiplex them somehow. That would involve "smart" reader hardware and much smarter decoding software. There are 3 track magnetic read heads available, and I'm sure it would be possible to construct a 3 channel raw reader with a microcontroller, but you'd still need to get that data to the phone somehow.

author
hkblacocow made it!(author)2013-01-30

Thx for your reply

author
hkblacocow made it!(author)2013-01-30

One more thing, does the lib only reads track2 data? Once I swipe card, how can I interpret track 1 and 2 separately?

author
sshipman0 made it!(author)2013-01-30

The RhombusLib library actually can decode either track 1 or track 2, but not both from the same swipe. The square reader provides only a single channel of data, and is physically aligned to read track 2. You can offset the read head relative to the magnetic stripe by inserting a small shim to align the read head with track 1. This is obviously not an exact science.

author
hkblacocow made it!(author)2013-01-30

Hi,
Can you send me or post the code sample how to use the lib? I've no idea when to call those classes in my app. Thx in advance.

author
sshipman0 made it!(author)2013-01-30

I have a demo program that I will create a github repository for, probably this evening. I'll update with a link to it when it exists.

author
sekitori made it!(author)2012-11-26

How about writing using the same technique, the other way around ?
I mean playing the audio, moving the card at a certain rate, and using the (cassette-recorder?) head to write....

About This Instructable

182,431views

89favorites

License:

More by sshipman0:Read any magnetic strip card with a Square reader and an Android device
Add instructable to: