def decryptpdf(infile, outdir, rscpath): errlog = '' rv = 1 # determine a good name for the output file name, ext = os.path.splitext(os.path.basename(infile)) outfile = os.path.join(outdir, name + '_nodrm.pdf') # try with any keyfiles (*.der) in the rscpath files = os.listdir(rscpath) filefilter = re.compile("\.der$", re.IGNORECASE) files = filter(filefilter.search, files) if files: for filename in files: keypath = os.path.join(rscpath, filename) userkey = open(keypath, 'rb').read() try: rv = ineptpdf.decryptBook(userkey, infile, outfile) if rv == 0: break except Exception as e: errlog += traceback.format_exc() errlog += str(e) rv = 1 if rv != 0: print(errlog) return rv
def PDFDecrypt(self, path_to_ebook): import calibre_plugins.dedrm.prefs as prefs import calibre_plugins.dedrm.ineptpdf dedrmprefs = prefs.DeDRM_Prefs() # Attempt to decrypt epub with each encryption key (generated or provided). print("{0} v{1}: {2} is a PDF ebook".format( PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook))) for keyname, userkeyhex in dedrmprefs['adeptkeys'].items(): userkey = codecs.decode(userkeyhex, 'hex') print("{0} v{1}: Trying Encryption key {2:s}".format( PLUGIN_NAME, PLUGIN_VERSION, keyname)) of = self.temporary_file(".pdf") # Give the user key, ebook and TemporaryPersistent file to the decryption function. try: result = ineptpdf.decryptBook(userkey, path_to_ebook, of.name) except: print( "{0} v{1}: Exception when decrypting after {2:.1f} seconds" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime)) traceback.print_exc() result = 1 of.close() if result == 0: # Decryption was successful. # Return the modified PersistentTemporary file to calibre. return of.name print( "{0} v{1}: Failed to decrypt with key {2:s} after {3:.1f} seconds" .format(PLUGIN_NAME, PLUGIN_VERSION, keyname, time.time() - self.starttime)) # perhaps we need to get a new default ADE key print( "{0} v{1}: Looking for new default Adobe Digital Editions Keys after {2:.1f} seconds" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime)) # get the default Adobe keys defaultkeys = [] try: if iswindows or isosx: from calibre_plugins.dedrm.adobekey import adeptkeys defaultkeys = adeptkeys() else: # linux from .wineutils import WineGetKeys scriptpath = os.path.join(self.alfdir, "adobekey.py") defaultkeys = WineGetKeys(scriptpath, ".der", dedrmprefs['adobewineprefix']) self.default_key = defaultkeys[0] except: print( "{0} v{1}: Exception when getting default Adobe Key after {2:.1f} seconds" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime)) traceback.print_exc() self.default_key = "" newkeys = [] for keyvalue in defaultkeys: if codecs.encode(keyvalue, 'hex') not in dedrmprefs['adeptkeys'].values(): newkeys.append(keyvalue) if len(newkeys) > 0: try: for i, userkey in enumerate(newkeys): print("{0} v{1}: Trying a new default key".format( PLUGIN_NAME, PLUGIN_VERSION)) of = self.temporary_file(".pdf") # Give the user key, ebook and TemporaryPersistent file to the decryption function. try: result = ineptpdf.decryptBook(userkey, path_to_ebook, of.name) except: print( "{0} v{1}: Exception when decrypting after {2:.1f} seconds" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime)) traceback.print_exc() result = 1 of.close() if result == 0: # Decryption was a success # Store the new successful key in the defaults print("{0} v{1}: Saving a new default key".format( PLUGIN_NAME, PLUGIN_VERSION)) try: dedrmprefs.addnamedvaluetoprefs( 'adeptkeys', 'default_key', codecs.encode(keyvalue, 'hex')) dedrmprefs.writeprefs() print( "{0} v{1}: Saved a new default key after {2:.1f} seconds" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime)) except: print( "{0} v{1}: Exception when saving a new default key after {2:.1f} seconds" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime)) traceback.print_exc() # Return the modified PersistentTemporary file to calibre. return of.name print( "{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime)) except Exception as e: pass # Something went wrong with decryption. print( "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime)) raise DeDRMError( "{0} v{1}: Ultimately failed to decrypt after {2:.1f} seconds. Read the FAQs at Harper's repository: https://github.com/apprenticeharper/DeDRM_tools/blob/master/FAQs.md" .format(PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime))
def PDFDecrypt(self, path_to_ebook): import calibre_plugins.dedrm.prefs as prefs import calibre_plugins.dedrm.ineptpdf dedrmprefs = prefs.DeDRM_Prefs() # Attempt to decrypt epub with each encryption key (generated or provided). print u"{0} v{1}: {2} is a PDF ebook".format( PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook)) for keyname, userkeyhex in dedrmprefs['adeptkeys'].items(): userkey = userkeyhex.decode('hex') print u"{0} v{1}: Trying Encryption key {2:s}".format( PLUGIN_NAME, PLUGIN_VERSION, keyname) of = self.temporary_file(u".pdf") # Give the user key, ebook and TemporaryPersistent file to the decryption function. try: result = ineptpdf.decryptBook(userkey, path_to_ebook, of.name) except: result = 1 of.close() if result == 0: # Decryption was successful. # Return the modified PersistentTemporary file to calibre. return of.name # perhaps we need to get a new default ADE key print u"{0} v{1}: Looking for new default Adobe Digital Editions Keys after {2:.1f} seconds".format( PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime) # get the default Adobe keys defaultkeys = [] if iswindows or isosx: import calibre_plugins.dedrm.adobekey as adobe try: defaultkeys = adobe.adeptkeys() except: pass else: # linux try: from wineutils import WineGetKeys scriptpath = os.join(self.alfdir, u"adobekey.py") defaultkeys = self.WineGetKeys( scriptpath, u".der", dedrmprefs['adobewineprefix']) except: pass newkeys = [] for keyvalue in defaultkeys: if keyvalue.encode( 'hex') not in dedrmprefs['adeptkeys'].values(): newkeys.append(keyvalue) if len(newkeys) > 0: try: for i, userkey in enumerate(newkeys): print u"{0} v{1}: Trying a new default key".format( PLUGIN_NAME, PLUGIN_VERSION) of = self.temporary_file(u".pdf") # Give the user key, ebook and TemporaryPersistent file to the decryption function. try: result = ineptepdf.decryptBook( userkey, inf.name, of.name) except: result = 1 of.close() if result == 0: # Decryption was a success # Store the new successful key in the defaults print u"{0} v{1}: Saving a new default key".format( PLUGIN_NAME, PLUGIN_VERSION) try: dedrmprefs.addnamedvaluetoprefs( 'adeptkeys', 'default_key', keyvalue.encode('hex')) dedrmprefs.writeprefs() except: traceback.print_exc() # Return the modified PersistentTemporary file to calibre. return of.name print u"{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds".format( PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime) except Exception, e: pass
def PDFDecrypt(self, path_to_ebook): import calibre_plugins.dedrm.prefs as prefs import calibre_plugins.dedrm.ineptpdf dedrmprefs = prefs.DeDRM_Prefs() # Attempt to decrypt epub with each encryption key (generated or provided). print u"{0} v{1}: {2} is a PDF ebook".format(PLUGIN_NAME, PLUGIN_VERSION, os.path.basename(path_to_ebook)) for keyname, userkeyhex in dedrmprefs["adeptkeys"].items(): userkey = userkeyhex.decode("hex") print u"{0} v{1}: Trying Encryption key {2:s}".format(PLUGIN_NAME, PLUGIN_VERSION, keyname) of = self.temporary_file(u".pdf") # Give the user key, ebook and TemporaryPersistent file to the decryption function. try: result = ineptpdf.decryptBook(userkey, path_to_ebook, of.name) except: result = 1 of.close() if result == 0: # Decryption was successful. # Return the modified PersistentTemporary file to calibre. return of.name # perhaps we need to get a new default ADE key print u"{0} v{1}: Looking for new default Adobe Digital Editions Keys after {2:.1f} seconds".format( PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime ) # get the default Adobe keys defaultkeys = [] if iswindows or isosx: import calibre_plugins.dedrm.adobekey as adobe try: defaultkeys = adobe.adeptkeys() except: pass else: # linux try: from wineutils import WineGetKeys scriptpath = os.path.join(self.alfdir, u"adobekey.py") defaultkeys = WineGetKeys(scriptpath, u".der", dedrmprefs["adobewineprefix"]) except: pass newkeys = [] for keyvalue in defaultkeys: if keyvalue.encode("hex") not in dedrmprefs["adeptkeys"].values(): newkeys.append(keyvalue) if len(newkeys) > 0: try: for i, userkey in enumerate(newkeys): print u"{0} v{1}: Trying a new default key".format(PLUGIN_NAME, PLUGIN_VERSION) of = self.temporary_file(u".pdf") # Give the user key, ebook and TemporaryPersistent file to the decryption function. try: result = ineptepdf.decryptBook(userkey, inf.name, of.name) except: result = 1 of.close() if result == 0: # Decryption was a success # Store the new successful key in the defaults print u"{0} v{1}: Saving a new default key".format(PLUGIN_NAME, PLUGIN_VERSION) try: dedrmprefs.addnamedvaluetoprefs("adeptkeys", "default_key", keyvalue.encode("hex")) dedrmprefs.writeprefs() except: traceback.print_exc() # Return the modified PersistentTemporary file to calibre. return of.name print u"{0} v{1}: Failed to decrypt with new default key after {2:.1f} seconds".format( PLUGIN_NAME, PLUGIN_VERSION, time.time() - self.starttime ) except Exception, e: pass