def main(argv=sys.argv):
    args = argv[1:]
    if len(args) != 3:
        return -1
    infile = args[0]
    outdir = args[1]
    rscpath = args[2]
    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)
            try:
                rv = ineptpdf.decryptBook(keypath, infile, outfile)
                if rv == 0:
                    break
            except Exception, e:
                errlog += str(e)
                rv = 1
                pass
Exemple #2
0
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 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, e:
                errlog += traceback.format_exc()
                errlog += str(e)
                rv = 1
    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:
                print(
                    u"{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(
                u"{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(
            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 = []

        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, u"adobekey.py")
                defaultkeys = WineGetKeys(scriptpath, u".der",
                                          dedrmprefs['adobewineprefix'])

            self.default_key = defaultkeys[0]
        except:
            print(
                u"{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 = u""

        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 = ineptpdf.decryptBook(userkey, path_to_ebook,
                                                      of.name)
                    except:
                        print(
                            u"{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(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()
                            print(
                                u"{0} v{1}: Saved a new default key after {2:.1f} seconds"
                                .format(PLUGIN_NAME, PLUGIN_VERSION,
                                        time.time() - self.starttime))
                        except:
                            print(
                                u"{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(
                        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