Over the weekend I made a tweet that several people asked about:
Basically, they wanted to know how to convert DRM protected Kindle books over to ePub books that will work on the iPad.
I love the fact that Amazon was able to release a Kindle app for the iPad on the very first day, but in a side-by-side comparison, I found Apple’s iBooks to be a little more useful to me. It’s a serious set of trade-offs. On the one hand, the Kindle app synchronizes with the cloud and remembers what page I last viewed. If I pick up my actual Kindle or use the Kindle app on the iPhone, then I can resume exactly where I was. There is no equivalent in iBooks. On the other hand, the Kindle app has no dictionary, whereas the iBooks dictionary is streamlined and non-obtrusive. Weighing the two features, the dictionary is more important to me. I can manually figure out where I am in a book if I switch devices, but I often run into words I do not know — for instance “bluestocking” (a derogatory term to describe an intelligent or literary woman) — meaning I have to jump out of the Kindle app, load up a dictionary app, wait for it to finish loading, type in the word, then switch back to the Kindle app. It is just so much easier to tap the word and get a definition in a pop-up bubble.
Given this, I started converting my Kindle books to ePub format. This is a two step process. For the folks that just want to do it and figure out the details on their own, those steps are:
- Use the “dedrm” Python script to strip the DRM from the Kindle *.azw file
- Use Calibre to convert from azw (it’s really MOBI) to ePub
Strip the DRM from the Kindle file
This step is the most difficult and takes a little Terminal mojo. The following directions assume you are running on a Mac (I am sure Python exists for Windows, but you’re on your own if you have a Microsoft box) and that you can do some basic navigation around the Terminal command prompt.
The Kindle AZW files are really just MOBI (a standard eBook format) with some special DRM applied. The DRM is a symmetric cipher, meaning it needs a keyword to encrypt and the same keyword to decrypt. That keyword is just a reshuffling of your Kindle’s serial number.
First, you will need your Kindle, with the purchased book downloaded onto it. Next, you will need a set of Python scripts that go by the name dedrm (including kindlepid.py, mobidedrm.py, and monidedrm2.py). I am not a lawyer, so I do not know if I can provide a direct download link here. I assume this stuff is covered under Fair Use, but I am going to go the conservative route and just tell you to do a Google search.
Plug in your Kindle, and copy the AZW file out of its /Documents folder and into the same folder as your Python scripts. To get the encryption key that Amazon uses to encrypt your eBooks,
cd to the directory containing the Python scripts and use this command, substituting the B12345678 with your actual Kindle serial number:
python ./kindlepid.py B12345678
This should come back with a result along the lines of:
Mobipocked PID for Kindle serial# B12345678 is 1Q4Y2VZ*RE
Save that mix of numbers, letters, and asterisk. That’s the key that Amazon uses to encrypt all eBooks for your specific Kindle.
Next, use that key to decrypt the book. You’re actually going to have to do this twice. One will work and one will not (and it will be difficult to know which one worked until you load it in a viewer in the next step). You will need to run the dedrm v0.01 and dedrm 0.02 commands. They differ slightly in the compression method used, but the wrong compression adds some garbage and bad metadata to your book. Be sure to replace the 1Q4Y2VZ*RE with your key derived from the above command. Also, leave those single-quotes in there.
python ./mobidedrm.py mybook.azw mybook1.mobi '1Q4Y2VZ*RE'
python ./mobidedrm2.py mybook.azw mybook2.mobi '1Q4Y2VZ*RE'
Either mybook1.mobi or mybook2.mobi should be the correct decrypted version of your eBook. The next step is to figure out which one is the good one and to convert it to ePub.
Convert AZW/MOBI to ePub
Import the first MOBI file into Calibre and use the “View” icon in the toolbar to see it in the viewer. Verify that this is a non-broken document.
You first want to bring up the table of contents by using the icon in the toolbar on the left. Click around in the table of contents to verify that chapter markers really do line up with the chapter text. Skim through a few dozen pages looking for weird HTML or XML fragments (angle brackets and equal signs that look out of place).
If everything looks good, you imported the good document. If things look funky, close the viewer, delete the document from Calibre, then import the other one.
Once you have the good document imported, select it and click on the “Convert E-Books” icon in the toolbar. Be sure to change the output format to EPUB. You can customize other options as you see fit. Click “OK” to convert.
Once converted, you can find the file in your Calibre folder or you can use the export functionality to save it to disk.
Drag the resulting *.epub file into the Books section of iTunes and you’re set. The book you purchased on Amazon can now be read in Apple’s iBooks application.