2012-12-19 13:48:11 +00:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
2009-12-22 10:54:19 +00:00
|
|
|
|
|
2011-01-17 07:24:53 +00:00
|
|
|
|
from __future__ import with_statement
|
|
|
|
|
|
2013-04-05 16:44:48 +00:00
|
|
|
|
# ignoblekeygen.pyw, version 2.5
|
|
|
|
|
# Copyright © 2009-2010 i♥cabbages
|
2009-12-22 10:54:19 +00:00
|
|
|
|
|
2012-12-19 13:48:11 +00:00
|
|
|
|
# Released under the terms of the GNU General Public Licence, version 3
|
|
|
|
|
# <http://www.gnu.org/licenses/>
|
|
|
|
|
|
2013-03-20 10:23:54 +00:00
|
|
|
|
# Modified 2010–2013 by some_updates, DiapDealer and Apprentice Alf
|
2012-12-19 13:48:11 +00:00
|
|
|
|
|
2013-04-05 16:44:48 +00:00
|
|
|
|
# Windows users: Before running this program, you must first install Python.
|
|
|
|
|
# We recommend ActiveState Python 2.7.X for Windows (x86) from
|
|
|
|
|
# http://www.activestate.com/activepython/downloads.
|
|
|
|
|
# You must also install PyCrypto from
|
|
|
|
|
# http://www.voidspace.org.uk/python/modules.shtml#pycrypto
|
|
|
|
|
# (make certain to install the version for Python 2.7).
|
|
|
|
|
# Then save this script file as ignoblekeygen.pyw and double-click on it to run it.
|
2012-12-19 13:48:11 +00:00
|
|
|
|
#
|
2013-04-05 16:44:48 +00:00
|
|
|
|
# Mac OS X users: Save this script file as ignoblekeygen.pyw. You can run this
|
|
|
|
|
# program from the command line (python ignoblekeygen.pyw) or by double-clicking
|
2012-12-19 13:48:11 +00:00
|
|
|
|
# it when it has been associated with PythonLauncher.
|
2009-12-22 10:54:19 +00:00
|
|
|
|
|
|
|
|
|
# Revision history:
|
|
|
|
|
# 1 - Initial release
|
2013-04-05 16:44:48 +00:00
|
|
|
|
# 2 - Add OS X support by using OpenSSL when available (taken/modified from ineptepub v5)
|
|
|
|
|
# 2.1 - Allow Windows versions of libcrypto to be found
|
|
|
|
|
# 2.2 - On Windows try PyCrypto first and then OpenSSL next
|
|
|
|
|
# 2.3 - Modify interface to allow use of import
|
|
|
|
|
# 2.4 - Improvements to UI and now works in plugins
|
|
|
|
|
# 2.5 - Additional improvement for unicode and plugin support
|
|
|
|
|
# 2.6 - moved unicode_argv call inside main for Windows DeDRM compatibility
|
|
|
|
|
# 2.7 - Work if TkInter is missing
|
2011-01-17 07:24:53 +00:00
|
|
|
|
|
2009-12-22 10:54:19 +00:00
|
|
|
|
"""
|
2013-04-05 16:44:48 +00:00
|
|
|
|
Generate Barnes & Noble EPUB user key from name and credit card number.
|
2009-12-22 10:54:19 +00:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
2013-04-05 16:44:48 +00:00
|
|
|
|
__version__ = "2.7"
|
2009-12-22 10:54:19 +00:00
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
2013-04-05 16:44:48 +00:00
|
|
|
|
import hashlib
|
2009-12-22 10:54:19 +00:00
|
|
|
|
|
2012-12-19 13:48:11 +00:00
|
|
|
|
# Wrap a stream so that output gets flushed immediately
|
|
|
|
|
# and also make sure that any unicode strings get
|
|
|
|
|
# encoded using "replace" before writing them.
|
|
|
|
|
class SafeUnbuffered:
|
|
|
|
|
def __init__(self, stream):
|
|
|
|
|
self.stream = stream
|
|
|
|
|
self.encoding = stream.encoding
|
|
|
|
|
if self.encoding == None:
|
|
|
|
|
self.encoding = "utf-8"
|
|
|
|
|
def write(self, data):
|
|
|
|
|
if isinstance(data,unicode):
|
|
|
|
|
data = data.encode(self.encoding,"replace")
|
|
|
|
|
self.stream.write(data)
|
|
|
|
|
self.stream.flush()
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
|
return getattr(self.stream, attr)
|
|
|
|
|
|
2013-01-19 14:50:57 +00:00
|
|
|
|
try:
|
|
|
|
|
from calibre.constants import iswindows, isosx
|
|
|
|
|
except:
|
|
|
|
|
iswindows = sys.platform.startswith('win')
|
|
|
|
|
isosx = sys.platform.startswith('darwin')
|
2012-12-19 13:48:11 +00:00
|
|
|
|
|
|
|
|
|
def unicode_argv():
|
|
|
|
|
if iswindows:
|
|
|
|
|
# Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
|
|
|
|
|
# strings.
|
|
|
|
|
|
|
|
|
|
# Versions 2.x of Python don't support Unicode in sys.argv on
|
|
|
|
|
# Windows, with the underlying Windows API instead replacing multi-byte
|
2013-04-05 16:44:48 +00:00
|
|
|
|
# characters with '?'. So use shell32.GetCommandLineArgvW to get sys.argv
|
|
|
|
|
# as a list of Unicode strings and encode them as utf-8
|
2012-12-19 13:48:11 +00:00
|
|
|
|
|
|
|
|
|
from ctypes import POINTER, byref, cdll, c_int, windll
|
|
|
|
|
from ctypes.wintypes import LPCWSTR, LPWSTR
|
|
|
|
|
|
|
|
|
|
GetCommandLineW = cdll.kernel32.GetCommandLineW
|
|
|
|
|
GetCommandLineW.argtypes = []
|
|
|
|
|
GetCommandLineW.restype = LPCWSTR
|
|
|
|
|
|
|
|
|
|
CommandLineToArgvW = windll.shell32.CommandLineToArgvW
|
|
|
|
|
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
|
|
|
|
|
CommandLineToArgvW.restype = POINTER(LPWSTR)
|
|
|
|
|
|
|
|
|
|
cmd = GetCommandLineW()
|
|
|
|
|
argc = c_int(0)
|
|
|
|
|
argv = CommandLineToArgvW(cmd, byref(argc))
|
|
|
|
|
if argc.value > 0:
|
|
|
|
|
# Remove Python executable and commands if present
|
|
|
|
|
start = argc.value - len(sys.argv)
|
|
|
|
|
return [argv[i] for i in
|
|
|
|
|
xrange(start, argc.value)]
|
2013-04-05 16:44:48 +00:00
|
|
|
|
# if we don't have any arguments at all, just pass back script name
|
|
|
|
|
# this should never happen
|
|
|
|
|
return [u"ignoblekeygen.py"]
|
2012-12-19 13:48:11 +00:00
|
|
|
|
else:
|
|
|
|
|
argvencoding = sys.stdin.encoding
|
|
|
|
|
if argvencoding == None:
|
|
|
|
|
argvencoding = "utf-8"
|
|
|
|
|
return [arg if (type(arg) == unicode) else unicode(arg,argvencoding) for arg in sys.argv]
|
2010-10-18 20:06:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IGNOBLEError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def _load_crypto_libcrypto():
|
|
|
|
|
from ctypes import CDLL, POINTER, c_void_p, c_char_p, c_int, c_long, \
|
|
|
|
|
Structure, c_ulong, create_string_buffer, cast
|
|
|
|
|
from ctypes.util import find_library
|
|
|
|
|
|
2012-12-19 13:48:11 +00:00
|
|
|
|
if iswindows:
|
2010-11-11 22:11:36 +00:00
|
|
|
|
libcrypto = find_library('libeay32')
|
|
|
|
|
else:
|
|
|
|
|
libcrypto = find_library('crypto')
|
2012-12-19 13:48:11 +00:00
|
|
|
|
|
2010-10-18 20:06:58 +00:00
|
|
|
|
if libcrypto is None:
|
|
|
|
|
raise IGNOBLEError('libcrypto not found')
|
|
|
|
|
libcrypto = CDLL(libcrypto)
|
|
|
|
|
|
|
|
|
|
AES_MAXNR = 14
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
2010-10-18 20:06:58 +00:00
|
|
|
|
c_char_pp = POINTER(c_char_p)
|
|
|
|
|
c_int_p = POINTER(c_int)
|
|
|
|
|
|
|
|
|
|
class AES_KEY(Structure):
|
|
|
|
|
_fields_ = [('rd_key', c_long * (4 * (AES_MAXNR + 1))),
|
|
|
|
|
('rounds', c_int)]
|
|
|
|
|
AES_KEY_p = POINTER(AES_KEY)
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
2010-10-18 20:06:58 +00:00
|
|
|
|
def F(restype, name, argtypes):
|
|
|
|
|
func = getattr(libcrypto, name)
|
|
|
|
|
func.restype = restype
|
|
|
|
|
func.argtypes = argtypes
|
|
|
|
|
return func
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
2013-04-05 16:44:48 +00:00
|
|
|
|
AES_set_encrypt_key = F(c_int, 'AES_set_encrypt_key',
|
2010-10-18 20:06:58 +00:00
|
|
|
|
[c_char_p, c_int, AES_KEY_p])
|
|
|
|
|
AES_cbc_encrypt = F(None, 'AES_cbc_encrypt',
|
|
|
|
|
[c_char_p, c_char_p, c_ulong, AES_KEY_p, c_char_p,
|
|
|
|
|
c_int])
|
2012-12-19 13:48:11 +00:00
|
|
|
|
|
2010-10-18 20:06:58 +00:00
|
|
|
|
class AES(object):
|
2013-04-05 16:44:48 +00:00
|
|
|
|
def __init__(self, userkey, iv):
|
2010-10-18 20:06:58 +00:00
|
|
|
|
self._blocksize = len(userkey)
|
2013-04-05 16:44:48 +00:00
|
|
|
|
self._iv = iv
|
2010-10-18 20:06:58 +00:00
|
|
|
|
key = self._key = AES_KEY()
|
2013-04-05 16:44:48 +00:00
|
|
|
|
rv = AES_set_encrypt_key(userkey, len(userkey) * 8, key)
|
2010-10-18 20:06:58 +00:00
|
|
|
|
if rv < 0:
|
2013-04-05 16:44:48 +00:00
|
|
|
|
raise IGNOBLEError('Failed to initialize AES Encrypt key')
|
2012-03-06 18:24:28 +00:00
|
|
|
|
|
2013-04-05 16:44:48 +00:00
|
|
|
|
def encrypt(self, data):
|
2010-10-18 20:06:58 +00:00
|
|
|
|
out = create_string_buffer(len(data))
|
2013-04-05 16:44:48 +00:00
|
|
|
|
rv = AES_cbc_encrypt(data, out, len(data), self._key, self._iv, 1)
|
2010-10-18 20:06:58 +00:00
|
|
|
|
if rv == 0:
|
2013-04-05 16:44:48 +00:00
|
|
|
|
raise IGNOBLEError('AES encryption failed')
|
2010-10-18 20:06:58 +00:00
|
|
|
|
return out.raw
|
|
|
|
|
|
|
|
|
|
return AES
|
|
|
|
|
|
|
|
|
|
def _load_crypto_pycrypto():
|
|
|
|
|
from Crypto.Cipher import AES as _AES
|
|
|
|
|
|
|
|
|
|
class AES(object):
|
2013-04-05 16:44:48 +00:00
|
|
|
|
def __init__(self, key, iv):
|
|
|
|
|
self._aes = _AES.new(key, _AES.MODE_CBC, iv)
|
2010-10-18 20:06:58 +00:00
|
|
|
|
|
2013-04-05 16:44:48 +00:00
|
|
|
|
def encrypt(self, data):
|
|
|
|
|
return self._aes.encrypt(data)
|
2010-10-18 20:06:58 +00:00
|
|
|
|
|
|
|
|
|
return AES
|
|
|
|
|
|
|
|
|
|
def _load_crypto():
|
2009-12-22 10:54:19 +00:00
|
|
|
|
AES = None
|
2011-01-06 07:10:38 +00:00
|
|
|
|
cryptolist = (_load_crypto_libcrypto, _load_crypto_pycrypto)
|
|
|
|
|
if sys.platform.startswith('win'):
|
|
|
|
|
cryptolist = (_load_crypto_pycrypto, _load_crypto_libcrypto)
|
|
|
|
|
for loader in cryptolist:
|
2010-10-18 20:06:58 +00:00
|
|
|
|
try:
|
|
|
|
|
AES = loader()
|
|
|
|
|
break
|
|
|
|
|
except (ImportError, IGNOBLEError):
|
|
|
|
|
pass
|
|
|
|
|
return AES
|
|
|
|
|
|
|
|
|
|
AES = _load_crypto()
|
2009-12-22 10:54:19 +00:00
|
|
|
|
|
2013-04-05 16:44:48 +00:00
|
|
|
|
def normalize_name(name):
|
|
|
|
|
return ''.join(x for x in name.lower() if x != ' ')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_key(name, ccn):
|
|
|
|
|
# remove spaces and case from name and CC numbers.
|
|
|
|
|
if type(name)==unicode:
|
|
|
|
|
name = name.encode('utf-8')
|
|
|
|
|
if type(ccn)==unicode:
|
|
|
|
|
ccn = ccn.encode('utf-8')
|
|
|
|
|
|
|
|
|
|
name = normalize_name(name) + '\x00'
|
|
|
|
|
ccn = normalize_name(ccn) + '\x00'
|
|
|
|
|
|
|
|
|
|
name_sha = hashlib.sha1(name).digest()[:16]
|
|
|
|
|
ccn_sha = hashlib.sha1(ccn).digest()[:16]
|
|
|
|
|
both_sha = hashlib.sha1(name + ccn).digest()
|
|
|
|
|
aes = AES(ccn_sha, name_sha)
|
|
|
|
|
crypt = aes.encrypt(both_sha + ('\x0c' * 0x0c))
|
|
|
|
|
userkey = hashlib.sha1(crypt).digest()
|
|
|
|
|
return userkey.encode('base64')
|
|
|
|
|
|
|
|
|
|
|
2009-12-22 10:54:19 +00:00
|
|
|
|
|
2011-01-17 07:24:53 +00:00
|
|
|
|
|
2013-03-26 16:38:18 +00:00
|
|
|
|
def cli_main():
|
2013-04-05 16:44:48 +00:00
|
|
|
|
sys.stdout=SafeUnbuffered(sys.stdout)
|
|
|
|
|
sys.stderr=SafeUnbuffered(sys.stderr)
|
2013-03-26 16:38:18 +00:00
|
|
|
|
argv=unicode_argv()
|
2011-01-17 07:24:53 +00:00
|
|
|
|
progname = os.path.basename(argv[0])
|
2013-04-05 16:44:48 +00:00
|
|
|
|
if AES is None:
|
|
|
|
|
print "%s: This script requires OpenSSL or PyCrypto, which must be installed " \
|
|
|
|
|
"separately. Read the top-of-script comment for details." % \
|
|
|
|
|
(progname,)
|
|
|
|
|
return 1
|
2011-01-17 07:24:53 +00:00
|
|
|
|
if len(argv) != 4:
|
2013-04-05 16:44:48 +00:00
|
|
|
|
print u"usage: {0} <Name> <CC#> <keyfileout.b64>".format(progname)
|
2011-01-17 07:24:53 +00:00
|
|
|
|
return 1
|
2013-04-05 16:44:48 +00:00
|
|
|
|
name, ccn, keypath = argv[1:]
|
|
|
|
|
userkey = generate_key(name, ccn)
|
|
|
|
|
open(keypath,'wb').write(userkey)
|
|
|
|
|
return 0
|
|
|
|
|
|
2011-01-17 07:24:53 +00:00
|
|
|
|
|
2009-12-22 10:54:19 +00:00
|
|
|
|
def gui_main():
|
2013-04-05 16:44:48 +00:00
|
|
|
|
try:
|
|
|
|
|
import Tkinter
|
|
|
|
|
import Tkconstants
|
|
|
|
|
import tkMessageBox
|
|
|
|
|
import traceback
|
|
|
|
|
except:
|
|
|
|
|
return cli_main()
|
2012-11-07 13:14:25 +00:00
|
|
|
|
|
|
|
|
|
class DecryptionDialog(Tkinter.Frame):
|
|
|
|
|
def __init__(self, root):
|
|
|
|
|
Tkinter.Frame.__init__(self, root, border=5)
|
2013-04-05 16:44:48 +00:00
|
|
|
|
self.status = Tkinter.Label(self, text=u"Enter parameters")
|
2012-11-07 13:14:25 +00:00
|
|
|
|
self.status.pack(fill=Tkconstants.X, expand=1)
|
|
|
|
|
body = Tkinter.Frame(self)
|
|
|
|
|
body.pack(fill=Tkconstants.X, expand=1)
|
|
|
|
|
sticky = Tkconstants.E + Tkconstants.W
|
|
|
|
|
body.grid_columnconfigure(1, weight=2)
|
2013-04-05 16:44:48 +00:00
|
|
|
|
Tkinter.Label(body, text=u"Account Name").grid(row=0)
|
|
|
|
|
self.name = Tkinter.Entry(body, width=40)
|
|
|
|
|
self.name.grid(row=0, column=1, sticky=sticky)
|
|
|
|
|
Tkinter.Label(body, text=u"CC#").grid(row=1)
|
|
|
|
|
self.ccn = Tkinter.Entry(body, width=40)
|
|
|
|
|
self.ccn.grid(row=1, column=1, sticky=sticky)
|
2013-01-19 14:50:57 +00:00
|
|
|
|
Tkinter.Label(body, text=u"Output file").grid(row=2)
|
2013-04-05 16:44:48 +00:00
|
|
|
|
self.keypath = Tkinter.Entry(body, width=40)
|
|
|
|
|
self.keypath.grid(row=2, column=1, sticky=sticky)
|
|
|
|
|
self.keypath.insert(2, u"bnepubkey.b64")
|
|
|
|
|
button = Tkinter.Button(body, text=u"...", command=self.get_keypath)
|
2012-11-07 13:14:25 +00:00
|
|
|
|
button.grid(row=2, column=2)
|
|
|
|
|
buttons = Tkinter.Frame(self)
|
|
|
|
|
buttons.pack()
|
|
|
|
|
botton = Tkinter.Button(
|
2013-04-05 16:44:48 +00:00
|
|
|
|
buttons, text=u"Generate", width=10, command=self.generate)
|
2012-11-07 13:14:25 +00:00
|
|
|
|
botton.pack(side=Tkconstants.LEFT)
|
|
|
|
|
Tkinter.Frame(buttons, width=10).pack(side=Tkconstants.LEFT)
|
|
|
|
|
button = Tkinter.Button(
|
2012-12-19 13:48:11 +00:00
|
|
|
|
buttons, text=u"Quit", width=10, command=self.quit)
|
2012-11-07 13:14:25 +00:00
|
|
|
|
button.pack(side=Tkconstants.RIGHT)
|
2012-12-19 13:48:11 +00:00
|
|
|
|
|
2012-11-07 13:14:25 +00:00
|
|
|
|
def get_keypath(self):
|
2013-04-05 16:44:48 +00:00
|
|
|
|
keypath = tkFileDialog.asksaveasfilename(
|
|
|
|
|
parent=None, title=u"Select B&N ePub key file to produce",
|
2012-12-19 13:48:11 +00:00
|
|
|
|
defaultextension=u".b64",
|
2012-11-07 13:14:25 +00:00
|
|
|
|
filetypes=[('base64-encoded files', '.b64'),
|
|
|
|
|
('All Files', '.*')])
|
|
|
|
|
if keypath:
|
|
|
|
|
keypath = os.path.normpath(keypath)
|
|
|
|
|
self.keypath.delete(0, Tkconstants.END)
|
|
|
|
|
self.keypath.insert(0, keypath)
|
|
|
|
|
return
|
2012-12-19 13:48:11 +00:00
|
|
|
|
|
2013-04-05 16:44:48 +00:00
|
|
|
|
def generate(self):
|
|
|
|
|
name = self.name.get()
|
|
|
|
|
ccn = self.ccn.get()
|
2012-11-07 13:14:25 +00:00
|
|
|
|
keypath = self.keypath.get()
|
2013-04-05 16:44:48 +00:00
|
|
|
|
if not name:
|
|
|
|
|
self.status['text'] = u"Name not specified"
|
2012-11-07 13:14:25 +00:00
|
|
|
|
return
|
2013-04-05 16:44:48 +00:00
|
|
|
|
if not ccn:
|
|
|
|
|
self.status['text'] = u"Credit card number not specified"
|
2012-11-07 13:14:25 +00:00
|
|
|
|
return
|
2013-04-05 16:44:48 +00:00
|
|
|
|
if not keypath:
|
|
|
|
|
self.status['text'] = u"Output keyfile path not specified"
|
2012-11-07 13:14:25 +00:00
|
|
|
|
return
|
2013-04-05 16:44:48 +00:00
|
|
|
|
self.status['text'] = u"Generating..."
|
2012-11-07 13:14:25 +00:00
|
|
|
|
try:
|
2013-04-05 16:44:48 +00:00
|
|
|
|
userkey = generate_key(name, ccn)
|
2012-11-07 13:14:25 +00:00
|
|
|
|
except Exception, e:
|
2013-04-05 16:44:48 +00:00
|
|
|
|
self.status['text'] = u"Error: (0}".format(e.args[0])
|
2012-11-07 13:14:25 +00:00
|
|
|
|
return
|
2013-04-05 16:44:48 +00:00
|
|
|
|
open(keypath,'wb').write(userkey)
|
|
|
|
|
self.status['text'] = u"Keyfile successfully generated"
|
2012-11-07 13:14:25 +00:00
|
|
|
|
|
2009-12-22 10:54:19 +00:00
|
|
|
|
root = Tkinter.Tk()
|
2013-04-05 16:44:48 +00:00
|
|
|
|
if AES is None:
|
|
|
|
|
root.withdraw()
|
|
|
|
|
tkMessageBox.showerror(
|
|
|
|
|
"Ignoble EPUB Keyfile Generator",
|
|
|
|
|
"This script requires OpenSSL or PyCrypto, which must be installed "
|
|
|
|
|
"separately. Read the top-of-script comment for details.")
|
|
|
|
|
return 1
|
|
|
|
|
root.title(u"Barnes & Noble ePub Keyfile Generator v.{0}".format(__version__))
|
2009-12-22 10:54:19 +00:00
|
|
|
|
root.resizable(True, False)
|
|
|
|
|
root.minsize(300, 0)
|
|
|
|
|
DecryptionDialog(root).pack(fill=Tkconstants.X, expand=1)
|
|
|
|
|
root.mainloop()
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
sys.exit(cli_main())
|
|
|
|
|
sys.exit(gui_main())
|