ineptepub 4.1

This commit is contained in:
Anonymous 2010-02-18 18:09:43 +00:00 committed by Apprentice Alf
parent f0d920c158
commit 63219d6054

View File

@ -1,6 +1,6 @@
#! /usr/bin/python #! /usr/bin/python
# ineptepub.pyw, version 4 # ineptepub.pyw, version 4.1
# To run this program install Python 2.6 from http://www.python.org/download/ # To run this program install Python 2.6 from http://www.python.org/download/
# and PyCrypto from http://www.voidspace.org.uk/python/modules.shtml#pycrypto # and PyCrypto from http://www.voidspace.org.uk/python/modules.shtml#pycrypto
@ -11,7 +11,8 @@
# 1 - Initial release # 1 - Initial release
# 2 - Rename to INEPT, fix exit code # 2 - Rename to INEPT, fix exit code
# 3 - Add cmd or gui choosing # 3 - Add cmd or gui choosing
# 4 - changed to adeptkey4.der format for 1.7.2 support (anon) # 4 - support for 1.7.2 support (anon)
# 4.1 - backward compatibility for 1.7.1 and old adeptkey.der
""" """
Decrypt Adobe ADEPT-encrypted EPUB books. Decrypt Adobe ADEPT-encrypted EPUB books.
@ -32,7 +33,6 @@ import Tkinter
import Tkconstants import Tkconstants
import tkFileDialog import tkFileDialog
import tkMessageBox import tkMessageBox
import pickle
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
@ -196,52 +196,35 @@ def cli_main(argv=sys.argv):
print "usage: %s KEYFILE INBOOK OUTBOOK" % (progname,) print "usage: %s KEYFILE INBOOK OUTBOOK" % (progname,)
return 1 return 1
keypath, inpath, outpath = argv[1:] keypath, inpath, outpath = argv[1:]
with open(keypath, 'r') as f: with open(keypath, 'rb') as f:
keyderlist = pickle.load(f) keyder = f.read()
keynotfound = 1 key = ASN1Parser([ord(x) for x in keyder])
for keyder in keyderlist: key = [bytesToNumber(key.getChild(x).value) for x in xrange(1, 4)]
key = ASN1Parser([ord(x) for x in keyder]) rsa = RSA.construct(key)
key = [bytesToNumber(key.getChild(x).value) for x in xrange(1, 4)] with closing(ZipFile(open(inpath, 'rb'))) as inf:
try: namelist = set(inf.namelist())
rsa = RSA.construct(key) if 'META-INF/rights.xml' not in namelist or \
except Exception: 'META-INF/encryption.xml' not in namelist:
keynotfound = 1 raise ADEPTError('%s: not an ADEPT EPUB' % (inpath,))
continue for name in META_NAMES:
with closing(ZipFile(open(inpath, 'rb'))) as inf: namelist.remove(name)
namelist = set(inf.namelist()) rights = etree.fromstring(inf.read('META-INF/rights.xml'))
if 'META-INF/rights.xml' not in namelist or \ adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
'META-INF/encryption.xml' not in namelist: expr = './/%s' % (adept('encryptedKey'),)
raise ADEPTError('%s: not an ADEPT EPUB' % (inpath,)) bookkey = ''.join(rights.findtext(expr))
for name in META_NAMES: bookkey = rsa.decrypt(bookkey.decode('base64'))
namelist.remove(name) # Padded as per RSAES-PKCS1-v1_5
rights = etree.fromstring(inf.read('META-INF/rights.xml')) if bookkey[-17] != '\x00':
adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag) raise ADEPTError('problem decrypting session key')
expr = './/%s' % (adept('encryptedKey'),) encryption = inf.read('META-INF/encryption.xml')
bookkey = ''.join(rights.findtext(expr)) decryptor = Decryptor(bookkey[-16:], encryption)
try: kwds = dict(compression=ZIP_DEFLATED, allowZip64=False)
bookkey = rsa.decrypt(bookkey.decode('base64')) with closing(ZipFile(open(outpath, 'wb'), 'w', **kwds)) as outf:
except Exception: zi = ZipInfo('mimetype', compress_type=ZIP_STORED)
keynotfound = 1 outf.writestr(zi, inf.read('mimetype'))
continue for path in namelist:
# Padded as per RSAES-PKCS1-v1_5 data = inf.read(path)
if bookkey[-17] != '\x00': outf.writestr(path, decryptor.decrypt(path, data))
keynotfound = 1
inf.close()
continue
else:
keynotfound = 0
encryption = inf.read('META-INF/encryption.xml')
decryptor = Decryptor(bookkey[-16:], encryption)
kwds = dict(compression=ZIP_DEFLATED, allowZip64=False)
with closing(ZipFile(open(outpath, 'wb'), 'w', **kwds)) as outf:
zi = ZipInfo('mimetype', compress_type=ZIP_STORED)
outf.writestr(zi, inf.read('mimetype'))
for path in namelist:
data = inf.read(path)
outf.writestr(path, decryptor.decrypt(path, data))
break
if keynotfound == 1:
raise ADEPTError('problem decrypting session key')
return 0 return 0
@ -257,8 +240,8 @@ class DecryptionDialog(Tkinter.Frame):
Tkinter.Label(body, text='Key file').grid(row=0) Tkinter.Label(body, text='Key file').grid(row=0)
self.keypath = Tkinter.Entry(body, width=30) self.keypath = Tkinter.Entry(body, width=30)
self.keypath.grid(row=0, column=1, sticky=sticky) self.keypath.grid(row=0, column=1, sticky=sticky)
if os.path.exists('adeptkey4.der'): if os.path.exists('adeptkey.der'):
self.keypath.insert(0, 'adeptkey4.der') self.keypath.insert(0, 'adeptkey.der')
button = Tkinter.Button(body, text="...", command=self.get_keypath) button = Tkinter.Button(body, text="...", command=self.get_keypath)
button.grid(row=0, column=2) button.grid(row=0, column=2)
Tkinter.Label(body, text='Input file').grid(row=1) Tkinter.Label(body, text='Input file').grid(row=1)