Guest User

Untitled

a guest
Apr 20th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 22.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from __future__ import with_statement
  5.  
  6. # adobekey.pyw, version 5.7
  7. # Copyright © 2009-2010 i♥cabbages
  8.  
  9. # Released under the terms of the GNU General Public Licence, version 3
  10. # <http://www.gnu.org/licenses/>
  11.  
  12. # Modified 2010–2013 by some_updates, DiapDealer and Apprentice Alf
  13.  
  14. # Windows users: Before running this program, you must first install Python.
  15. #   We recommend ActiveState Python 2.7.X for Windows (x86) from
  16. #   http://www.activestate.com/activepython/downloads.
  17. #   You must also install PyCrypto from
  18. #   http://www.voidspace.org.uk/python/modules.shtml#pycrypto
  19. #   (make certain to install the version for Python 2.7).
  20. #   Then save this script file as adobekey.pyw and double-click on it to run it.
  21. #   It will create a file named adobekey_1.der in in the same directory as the script.
  22. #   This is your Adobe Digital Editions user key.
  23. #
  24. # Mac OS X users: Save this script file as adobekey.pyw.  You can run this
  25. #   program from the command line (python adobekey.pyw) or by double-clicking
  26. #   it when it has been associated with PythonLauncher.  It will create a file
  27. #   named adobekey_1.der in the same directory as the script.
  28. #   This is your Adobe Digital Editions user key.
  29.  
  30. # Revision history:
  31. #   1 - Initial release, for Adobe Digital Editions 1.7
  32. #   2 - Better algorithm for finding pLK; improved error handling
  33. #   3 - Rename to INEPT
  34. #   4 - Series of changes by joblack (and others?) --
  35. #   4.1 - quick beta fix for ADE 1.7.2 (anon)
  36. #   4.2 - added old 1.7.1 processing
  37. #   4.3 - better key search
  38. #   4.4 - Make it working on 64-bit Python
  39. #   5  -  Clean up and improve 4.x changes;
  40. #         Clean up and merge OS X support by unknown
  41. #   5.1 - add support for using OpenSSL on Windows in place of PyCrypto
  42. #   5.2 - added support for output of key to a particular file
  43. #   5.3 - On Windows try PyCrypto first, OpenSSL next
  44. #   5.4 - Modify interface to allow use of import
  45. #   5.5 - Fix for potential problem with PyCrypto
  46. #   5.6 - Revised to allow use in Plugins to eliminate need for duplicate code
  47. #   5.7 - Unicode support added, renamed adobekey from ineptkey
  48. #   5.8 - Added getkey interface for Windows DeDRM application
  49. #   5.9 - moved unicode_argv call inside main for Windows DeDRM compatibility
  50. #   6.0 - Work if TkInter is missing
  51.  
  52. """
  53. Retrieve Adobe ADEPT user key.
  54. """
  55.  
  56. __license__ = 'GPL v3'
  57. __version__ = '6.0'
  58.  
  59. import sys, os, struct, getopt
  60.  
  61. # Wrap a stream so that output gets flushed immediately
  62. # and also make sure that any unicode strings get
  63. # encoded using "replace" before writing them.
  64. class SafeUnbuffered:
  65.     def __init__(self, stream):
  66.         self.stream = stream
  67.         self.encoding = stream.encoding
  68.         if self.encoding == None:
  69.             self.encoding = "utf-8"
  70.     def write(self, data):
  71.         if isinstance(data,unicode):
  72.             data = data.encode(self.encoding,"replace")
  73.         self.stream.write(data)
  74.         self.stream.flush()
  75.     def __getattr__(self, attr):
  76.         return getattr(self.stream, attr)
  77.  
  78. try:
  79.     from calibre.constants import iswindows, isosx
  80. except:
  81.     iswindows = sys.platform.startswith('win')
  82.     isosx = sys.platform.startswith('darwin')
  83.  
  84. def unicode_argv():
  85.     if iswindows:
  86.         # Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
  87.         # strings.
  88.  
  89.         # Versions 2.x of Python don't support Unicode in sys.argv on
  90.         # Windows, with the underlying Windows API instead replacing multi-byte
  91.         # characters with '?'.  So use shell32.GetCommandLineArgvW to get sys.argv
  92.         # as a list of Unicode strings and encode them as utf-8
  93.  
  94.         from ctypes import POINTER, byref, cdll, c_int, windll
  95.         from ctypes.wintypes import LPCWSTR, LPWSTR
  96.  
  97.         GetCommandLineW = cdll.kernel32.GetCommandLineW
  98.         GetCommandLineW.argtypes = []
  99.         GetCommandLineW.restype = LPCWSTR
  100.  
  101.         CommandLineToArgvW = windll.shell32.CommandLineToArgvW
  102.         CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
  103.         CommandLineToArgvW.restype = POINTER(LPWSTR)
  104.  
  105.         cmd = GetCommandLineW()
  106.         argc = c_int(0)
  107.         argv = CommandLineToArgvW(cmd, byref(argc))
  108.         if argc.value > 0:
  109.             # Remove Python executable and commands if present
  110.             start = argc.value - len(sys.argv)
  111.             return [argv[i] for i in
  112.                     xrange(start, argc.value)]
  113.         # if we don't have any arguments at all, just pass back script name
  114.         # this should never happen
  115.         return [u"adobekey.py"]
  116.     else:
  117.         argvencoding = sys.stdin.encoding
  118.         if argvencoding == None:
  119.             argvencoding = "utf-8"
  120.         return [arg if (type(arg) == unicode) else unicode(arg,argvencoding) for arg in sys.argv]
  121.  
  122. class ADEPTError(Exception):
  123.     pass
  124.  
  125. if iswindows:
  126.     from ctypes import windll, c_char_p, c_wchar_p, c_uint, POINTER, byref, \
  127.         create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \
  128.         string_at, Structure, c_void_p, cast, c_size_t, memmove, CDLL, c_int, \
  129.         c_long, c_ulong
  130.  
  131.     from ctypes.wintypes import LPVOID, DWORD, BOOL
  132.     import _winreg as winreg
  133.  
  134.     def _load_crypto_libcrypto():
  135.         from ctypes.util import find_library
  136.         libcrypto = find_library('libeay32')
  137.         if libcrypto is None:
  138.             raise ADEPTError('libcrypto not found')
  139.         libcrypto = CDLL(libcrypto)
  140.         AES_MAXNR = 14
  141.         c_char_pp = POINTER(c_char_p)
  142.         c_int_p = POINTER(c_int)
  143.         class AES_KEY(Structure):
  144.             _fields_ = [('rd_key', c_long * (4 * (AES_MAXNR + 1))),
  145.                         ('rounds', c_int)]
  146.         AES_KEY_p = POINTER(AES_KEY)
  147.  
  148.         def F(restype, name, argtypes):
  149.             func = getattr(libcrypto, name)
  150.             func.restype = restype
  151.             func.argtypes = argtypes
  152.             return func
  153.  
  154.         AES_set_decrypt_key = F(c_int, 'AES_set_decrypt_key',
  155.                                 [c_char_p, c_int, AES_KEY_p])
  156.         AES_cbc_encrypt = F(None, 'AES_cbc_encrypt',
  157.                             [c_char_p, c_char_p, c_ulong, AES_KEY_p, c_char_p,
  158.                              c_int])
  159.         class AES(object):
  160.             def __init__(self, userkey):
  161.                 self._blocksize = len(userkey)
  162.                 if (self._blocksize != 16) and (self._blocksize != 24) and (self._blocksize != 32) :
  163.                     raise ADEPTError('AES improper key used')
  164.                 key = self._key = AES_KEY()
  165.                 rv = AES_set_decrypt_key(userkey, len(userkey) * 8, key)
  166.                 if rv < 0:
  167.                     raise ADEPTError('Failed to initialize AES key')
  168.             def decrypt(self, data):
  169.                 out = create_string_buffer(len(data))
  170.                 iv = ("\x00" * self._blocksize)
  171.                 rv = AES_cbc_encrypt(data, out, len(data), self._key, iv, 0)
  172.                 if rv == 0:
  173.                     raise ADEPTError('AES decryption failed')
  174.                 return out.raw
  175.         return AES
  176.  
  177.     def _load_crypto_pycrypto():
  178.         from Crypto.Cipher import AES as _AES
  179.         class AES(object):
  180.             def __init__(self, key):
  181.                 self._aes = _AES.new(key, _AES.MODE_CBC, '\x00'*16)
  182.             def decrypt(self, data):
  183.                 return self._aes.decrypt(data)
  184.         return AES
  185.  
  186.     def _load_crypto():
  187.         AES = None
  188.         for loader in (_load_crypto_pycrypto, _load_crypto_libcrypto):
  189.             try:
  190.                 AES = loader()
  191.                 break
  192.             except (ImportError, ADEPTError):
  193.                 pass
  194.         return AES
  195.  
  196.     AES = _load_crypto()
  197.  
  198.  
  199.     DEVICE_KEY_PATH = r'Software\Adobe\Adept\Device'
  200.     PRIVATE_LICENCE_KEY_PATH = r'Software\Adobe\Adept\Activation'
  201.  
  202.     MAX_PATH = 255
  203.  
  204.     kernel32 = windll.kernel32
  205.     advapi32 = windll.advapi32
  206.     crypt32 = windll.crypt32
  207.  
  208.     def GetSystemDirectory():
  209.         GetSystemDirectoryW = kernel32.GetSystemDirectoryW
  210.         GetSystemDirectoryW.argtypes = [c_wchar_p, c_uint]
  211.         GetSystemDirectoryW.restype = c_uint
  212.         def GetSystemDirectory():
  213.             buffer = create_unicode_buffer(MAX_PATH + 1)
  214.             GetSystemDirectoryW(buffer, len(buffer))
  215.             return buffer.value
  216.         return GetSystemDirectory
  217.     GetSystemDirectory = GetSystemDirectory()
  218.  
  219.     def GetVolumeSerialNumber():
  220.         GetVolumeInformationW = kernel32.GetVolumeInformationW
  221.         GetVolumeInformationW.argtypes = [c_wchar_p, c_wchar_p, c_uint,
  222.                                           POINTER(c_uint), POINTER(c_uint),
  223.                                           POINTER(c_uint), c_wchar_p, c_uint]
  224.         GetVolumeInformationW.restype = c_uint
  225.         def GetVolumeSerialNumber(path):
  226.             vsn = c_uint(0)
  227.             GetVolumeInformationW(
  228.                 path, None, 0, byref(vsn), None, None, None, 0)
  229.             return vsn.value
  230.         return GetVolumeSerialNumber
  231.     GetVolumeSerialNumber = GetVolumeSerialNumber()
  232.  
  233.     def GetUserName():
  234.         GetUserNameW = advapi32.GetUserNameW
  235.         GetUserNameW.argtypes = [c_wchar_p, POINTER(c_uint)]
  236.         GetUserNameW.restype = c_uint
  237.         def GetUserName():
  238.             buffer = create_unicode_buffer(32)
  239.             size = c_uint(len(buffer))
  240.             while not GetUserNameW(buffer, byref(size)):
  241.                 buffer = create_unicode_buffer(len(buffer) * 2)
  242.                 size.value = len(buffer)
  243.             return buffer.value.encode('utf-16-le')[::2]
  244.         return GetUserName
  245.     GetUserName = GetUserName()
  246.  
  247.     PAGE_EXECUTE_READWRITE = 0x40
  248.     MEM_COMMIT  = 0x1000
  249.     MEM_RESERVE = 0x2000
  250.  
  251.     def VirtualAlloc():
  252.         _VirtualAlloc = kernel32.VirtualAlloc
  253.         _VirtualAlloc.argtypes = [LPVOID, c_size_t, DWORD, DWORD]
  254.         _VirtualAlloc.restype = LPVOID
  255.         def VirtualAlloc(addr, size, alloctype=(MEM_COMMIT | MEM_RESERVE),
  256.                          protect=PAGE_EXECUTE_READWRITE):
  257.             return _VirtualAlloc(addr, size, alloctype, protect)
  258.         return VirtualAlloc
  259.     VirtualAlloc = VirtualAlloc()
  260.  
  261.     MEM_RELEASE = 0x8000
  262.  
  263.     def VirtualFree():
  264.         _VirtualFree = kernel32.VirtualFree
  265.         _VirtualFree.argtypes = [LPVOID, c_size_t, DWORD]
  266.         _VirtualFree.restype = BOOL
  267.         def VirtualFree(addr, size=0, freetype=MEM_RELEASE):
  268.             return _VirtualFree(addr, size, freetype)
  269.         return VirtualFree
  270.     VirtualFree = VirtualFree()
  271.  
  272.     class NativeFunction(object):
  273.         def __init__(self, restype, argtypes, insns):
  274.             self._buf = buf = VirtualAlloc(None, len(insns))
  275.             memmove(buf, insns, len(insns))
  276.             ftype = CFUNCTYPE(restype, *argtypes)
  277.             self._native = ftype(buf)
  278.  
  279.         def __call__(self, *args):
  280.             return self._native(*args)
  281.  
  282.         def __del__(self):
  283.             if self._buf is not None:
  284.                 VirtualFree(self._buf)
  285.                 self._buf = None
  286.  
  287.     if struct.calcsize("P") == 4:
  288.         CPUID0_INSNS = (
  289.             "\x53"             # push   %ebx
  290.             "\x31\xc0"         # xor    %eax,%eax
  291.             "\x0f\xa2"         # cpuid
  292.             "\x8b\x44\x24\x08" # mov    0x8(%esp),%eax
  293.             "\x89\x18"         # mov    %ebx,0x0(%eax)
  294.             "\x89\x50\x04"     # mov    %edx,0x4(%eax)
  295.             "\x89\x48\x08"     # mov    %ecx,0x8(%eax)
  296.             "\x5b"             # pop    %ebx
  297.             "\xc3"             # ret
  298.         )
  299.         CPUID1_INSNS = (
  300.             "\x53"             # push   %ebx
  301.             "\x31\xc0"         # xor    %eax,%eax
  302.             "\x40"             # inc    %eax
  303.             "\x0f\xa2"         # cpuid
  304.             "\x5b"             # pop    %ebx
  305.             "\xc3"             # ret
  306.         )
  307.     else:
  308.         CPUID0_INSNS = (
  309.             "\x49\x89\xd8"     # mov    %rbx,%r8
  310.             "\x49\x89\xc9"     # mov    %rcx,%r9
  311.             "\x48\x31\xc0"     # xor    %rax,%rax
  312.             "\x0f\xa2"         # cpuid
  313.             "\x4c\x89\xc8"     # mov    %r9,%rax
  314.             "\x89\x18"         # mov    %ebx,0x0(%rax)
  315.             "\x89\x50\x04"     # mov    %edx,0x4(%rax)
  316.             "\x89\x48\x08"     # mov    %ecx,0x8(%rax)
  317.             "\x4c\x89\xc3"     # mov    %r8,%rbx
  318.             "\xc3"             # retq
  319.         )
  320.         CPUID1_INSNS = (
  321.             "\x53"             # push   %rbx
  322.             "\x48\x31\xc0"     # xor    %rax,%rax
  323.             "\x48\xff\xc0"     # inc    %rax
  324.             "\x0f\xa2"         # cpuid
  325.             "\x5b"             # pop    %rbx
  326.             "\xc3"             # retq
  327.         )
  328.  
  329.     def cpuid0():
  330.         _cpuid0 = NativeFunction(None, [c_char_p], CPUID0_INSNS)
  331.         buf = create_string_buffer(12)
  332.         def cpuid0():
  333.             _cpuid0(buf)
  334.             return buf.raw
  335.         return cpuid0
  336.     cpuid0 = cpuid0()
  337.  
  338.     cpuid1 = NativeFunction(c_uint, [], CPUID1_INSNS)
  339.  
  340.     class DataBlob(Structure):
  341.         _fields_ = [('cbData', c_uint),
  342.                     ('pbData', c_void_p)]
  343.     DataBlob_p = POINTER(DataBlob)
  344.  
  345.     def CryptUnprotectData():
  346.         _CryptUnprotectData = crypt32.CryptUnprotectData
  347.         _CryptUnprotectData.argtypes = [DataBlob_p, c_wchar_p, DataBlob_p,
  348.                                        c_void_p, c_void_p, c_uint, DataBlob_p]
  349.         _CryptUnprotectData.restype = c_uint
  350.         def CryptUnprotectData(indata, entropy):
  351.             indatab = create_string_buffer(indata)
  352.             indata = DataBlob(len(indata), cast(indatab, c_void_p))
  353.             entropyb = create_string_buffer(entropy)
  354.             entropy = DataBlob(len(entropy), cast(entropyb, c_void_p))
  355.             outdata = DataBlob()
  356.             if not _CryptUnprotectData(byref(indata), None, byref(entropy),
  357.                                        None, None, 0, byref(outdata)):
  358.                 raise ADEPTError("Failed to decrypt user key key (sic)")
  359.             return string_at(outdata.pbData, outdata.cbData)
  360.         return CryptUnprotectData
  361.     CryptUnprotectData = CryptUnprotectData()
  362.  
  363.     def adeptkeys():
  364.         if AES is None:
  365.             raise ADEPTError("PyCrypto or OpenSSL must be installed")
  366.         root = GetSystemDirectory().split('\\')[0] + '\\'
  367.         serial = GetVolumeSerialNumber(root)
  368.         vendor = cpuid0()
  369.         signature = struct.pack('>I', cpuid1())[1:]
  370.         user = GetUserName()
  371.         entropy = struct.pack('>I12s3s13s', serial, vendor, signature, user)
  372.         cuser = winreg.HKEY_CURRENT_USER
  373.         try:
  374.             regkey = winreg.OpenKey(cuser, DEVICE_KEY_PATH)
  375.             device = winreg.QueryValueEx(regkey, 'key')[0]
  376.         except WindowsError:
  377.             raise ADEPTError("Adobe Digital Editions not activated")
  378.         keykey = CryptUnprotectData(device, entropy)
  379.         userkey = None
  380.         keys = []
  381.         try:
  382.             plkroot = winreg.OpenKey(cuser, PRIVATE_LICENCE_KEY_PATH)
  383.         except WindowsError:
  384.             raise ADEPTError("Could not locate ADE activation")
  385.         for i in xrange(0, 16):
  386.             try:
  387.                 plkparent = winreg.OpenKey(plkroot, "%04d" % (i,))
  388.             except WindowsError:
  389.                 break
  390.             ktype = winreg.QueryValueEx(plkparent, None)[0]
  391.             if ktype != 'credentials':
  392.                 continue
  393.             for j in xrange(0, 16):
  394.                 try:
  395.                     plkkey = winreg.OpenKey(plkparent, "%04d" % (j,))
  396.                 except WindowsError:
  397.                     break
  398.                 ktype = winreg.QueryValueEx(plkkey, None)[0]
  399.                 if ktype != 'privateLicenseKey':
  400.                     continue
  401.                 userkey = winreg.QueryValueEx(plkkey, 'value')[0]
  402.                 userkey = userkey.decode('base64')
  403.                 aes = AES(keykey)
  404.                 userkey = aes.decrypt(userkey)
  405.                 userkey = userkey[26:-ord(userkey[-1])]
  406.                 #print "found key:",userkey.encode('hex')
  407.                 keys.append(userkey)
  408.         if len(keys) == 0:
  409.             raise ADEPTError('Could not locate privateLicenseKey')
  410.         print u"Found {0:d} keys".format(len(keys))
  411.         return keys
  412.  
  413.  
  414. elif isosx:
  415.     import xml.etree.ElementTree as etree
  416.     import subprocess
  417.  
  418.     NSMAP = {'adept': 'http://ns.adobe.com/adept',
  419.              'enc': 'http://www.w3.org/2001/04/xmlenc#'}
  420.  
  421.     def findActivationDat():
  422.         import warnings
  423.         warnings.filterwarnings('ignore', category=FutureWarning)
  424.  
  425.         home = os.getenv('HOME')
  426.         cmdline = 'find "' + home + '/Library/Application Support/Adobe/Digital Editions" -name "activation.dat"'
  427.         cmdline = cmdline.encode(sys.getfilesystemencoding())
  428.         p2 = subprocess.Popen(cmdline, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=False)
  429.         out1, out2 = p2.communicate()
  430.         reslst = out1.split('\n')
  431.         cnt = len(reslst)
  432.         ActDatPath = "activation.dat"
  433.         for j in xrange(cnt):
  434.             resline = reslst[j]
  435.             pp = resline.find('activation.dat')
  436.             if pp >= 0:
  437.                 ActDatPath = resline
  438.                 break
  439.         if os.path.exists(ActDatPath):
  440.             return ActDatPath
  441.         return None
  442.  
  443.     def adeptkeys():
  444.         actpath = findActivationDat()
  445.         if actpath is None:
  446.             raise ADEPTError("Could not find ADE activation.dat file.")
  447.         tree = etree.parse(actpath)
  448.         adept = lambda tag: '{%s}%s' % (NSMAP['adept'], tag)
  449.         expr = '//%s/%s' % (adept('credentials'), adept('privateLicenseKey'))
  450.         userkey = tree.findtext(expr)
  451.         userkey = userkey.decode('base64')
  452.         userkey = userkey[26:]
  453.         return [userkey]
  454.  
  455. else:
  456.     def adeptkeys():
  457.         raise ADEPTError("This script only supports Windows and Mac OS X.")
  458.         return []
  459.  
  460. # interface for Python DeDRM
  461. def getkey(outpath):
  462.     keys = adeptkeys()
  463.     if len(keys) > 0:
  464.         if not os.path.isdir(outpath):
  465.             outfile = outpath
  466.             with file(outfile, 'wb') as keyfileout:
  467.                 keyfileout.write(keys[0])
  468.             print u"Saved a key to {0}".format(outfile)
  469.         else:
  470.             keycount = 0
  471.             for key in keys:
  472.                 while True:
  473.                     keycount += 1
  474.                     outfile = os.path.join(outpath,u"adobekey_{0:d}.der".format(keycount))
  475.                     if not os.path.exists(outfile):
  476.                         break
  477.                 with file(outfile, 'wb') as keyfileout:
  478.                     keyfileout.write(key)
  479.                 print u"Saved a key to {0}".format(outfile)
  480.         return True
  481.     return False
  482.  
  483. def usage(progname):
  484.     print u"Finds, decrypts and saves the default Adobe Adept encryption key(s)."
  485.     print u"Keys are saved to the current directory, or a specified output directory."
  486.     print u"If a file name is passed instead of a directory, only the first key is saved, in that file."
  487.     print u"Usage:"
  488.     print u"    {0:s} [-h] [<outpath>]".format(progname)
  489.  
  490. def cli_main():
  491.     sys.stdout=SafeUnbuffered(sys.stdout)
  492.     sys.stderr=SafeUnbuffered(sys.stderr)
  493.     argv=unicode_argv()
  494.     progname = os.path.basename(argv[0])
  495.     print u"{0} v{1}\nCopyright © 2009-2013 i♥cabbages and Apprentice Alf".format(progname,__version__)
  496.  
  497.     try:
  498.         opts, args = getopt.getopt(argv[1:], "h")
  499.     except getopt.GetoptError, err:
  500.         print u"Error in options or arguments: {0}".format(err.args[0])
  501.         usage(progname)
  502.         sys.exit(2)
  503.  
  504.     for o, a in opts:
  505.         if o == "-h":
  506.             usage(progname)
  507.             sys.exit(0)
  508.  
  509.     if len(args) > 1:
  510.         usage(progname)
  511.         sys.exit(2)
  512.  
  513.     if len(args) == 1:
  514.         # save to the specified file or directory
  515.         outpath = args[0]
  516.         if not os.path.isabs(outpath):
  517.            outpath = os.path.abspath(outpath)
  518.     else:
  519.         # save to the same directory as the script
  520.         outpath = os.path.dirname(argv[0])
  521.  
  522.     # make sure the outpath is the
  523.     outpath = os.path.realpath(os.path.normpath(outpath))
  524.  
  525.     keys = adeptkeys()
  526.     if len(keys) > 0:
  527.         if not os.path.isdir(outpath):
  528.             outfile = outpath
  529.             with file(outfile, 'wb') as keyfileout:
  530.                 keyfileout.write(keys[0])
  531.             print u"Saved a key to {0}".format(outfile)
  532.         else:
  533.             keycount = 0
  534.             for key in keys:
  535.                 while True:
  536.                     keycount += 1
  537.                     outfile = os.path.join(outpath,u"adobekey_{0:d}.der".format(keycount))
  538.                     if not os.path.exists(outfile):
  539.                         break
  540.                 with file(outfile, 'wb') as keyfileout:
  541.                     keyfileout.write(key)
  542.                 print u"Saved a key to {0}".format(outfile)
  543.     else:
  544.         print u"Could not retrieve Adobe Adept key."
  545.     return 0
  546.  
  547.  
  548. def gui_main():
  549.     try:
  550.         import Tkinter
  551.         import Tkconstants
  552.         import tkMessageBox
  553.         import traceback
  554.     except:
  555.         return cli_main()
  556.  
  557.     class ExceptionDialog(Tkinter.Frame):
  558.         def __init__(self, root, text):
  559.             Tkinter.Frame.__init__(self, root, border=5)
  560.             label = Tkinter.Label(self, text=u"Unexpected error:",
  561.                                   anchor=Tkconstants.W, justify=Tkconstants.LEFT)
  562.             label.pack(fill=Tkconstants.X, expand=0)
  563.             self.text = Tkinter.Text(self)
  564.             self.text.pack(fill=Tkconstants.BOTH, expand=1)
  565.  
  566.             self.text.insert(Tkconstants.END, text)
  567.  
  568.  
  569.     argv=unicode_argv()
  570.     root = Tkinter.Tk()
  571.     root.withdraw()
  572.     progpath, progname = os.path.split(argv[0])
  573.     success = False
  574.     try:
  575.         keys = adeptkeys()
  576.         keycount = 0
  577.         for key in keys:
  578.             while True:
  579.                 keycount += 1
  580.                 outfile = os.path.join(progpath,u"adobekey_{0:d}.der".format(keycount))
  581.                 if not os.path.exists(outfile):
  582.                     break
  583.  
  584.             with file(outfile, 'wb') as keyfileout:
  585.                 keyfileout.write(key)
  586.             success = True
  587.             tkMessageBox.showinfo(progname, u"Key successfully retrieved to {0}".format(outfile))
  588.     except ADEPTError, e:
  589.         tkMessageBox.showerror(progname, u"Error: {0}".format(str(e)))
  590.     except Exception:
  591.         root.wm_state('normal')
  592.         root.title(progname)
  593.         text = traceback.format_exc()
  594.         ExceptionDialog(root, text).pack(fill=Tkconstants.BOTH, expand=1)
  595.         root.mainloop()
  596.     if not success:
  597.         return 1
  598.     return 0
  599.  
  600. if __name__ == '__main__':
  601.     if len(sys.argv) > 1:
  602.         sys.exit(cli_main())
  603.     sys.exit(gui_main())
Add Comment
Please, Sign In to add comment