NewPasswordEntry.py 3.44 KB
Newer Older
Heiko Reese's avatar
Heiko Reese committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from OpenSSL import crypto, SSL
from os.path import basename, dirname, isfile, splitext
from sys import exit
from tkinter import Tk, filedialog, messagebox, simpledialog, Label, Entry, Button, N, E, W, S, NORMAL, DISABLED

class PasswordEntry:
    def __init__(self, master):
        self.master = master
        master.title("Enter Password")

        infilename = ""
        infile = ""

        self.label = Label(master, text = "Passwort")
        self.label.grid(row=0, sticky=W)

        self.passentry = Entry(master)
        self.passentry.grid(row=0, column=1, sticky=W)
        self.toggleShow()
        self.passentry.focus_set()

        self.toggle = Button(master, text="⚷", command=self.toggleShow)
        self.toggle.grid(row=0, column=2, sticky=W)

        self.okbutton = Button(master, text="OK", command=self.reencode)
        self.okbutton.grid(row=1, column=0, columnspan=3, sticky=W+E+N+S)
        self.okbutton.config()

        # ask for input file
        self.askInputFile()

    # check password while being typed in
    def reencode(self):
        try:
            p12 = crypto.load_pkcs12(self.infile, self.passentry.get().encode('utf-8'))
        except crypto.Error:
            messagebox.showerror("Ok", "Falsches Passwort")
            return

        # write output file
        outfile = filedialog.asksaveasfilename(title='PKCS12-Ausgabedatei',
                                               initialdir=dirname(self.infilename),
                                               initialfile=splitext(basename(self.infilename))[0] + '-repariert.p12',
                                               defaultextension='.p12',
                                               filetypes=(('PKCS12-Dateien', ('*.p12', '*.P12', '*.pfx', '*.PFX')),
                                                          ('alle Dateien', '*.*')))
        if not (outfile):
            messagebox.showinfo('Abbruch', 'Die Dateiauswahl wurde abgebrochen!')
            return

        try:
            with open(outfile, 'wb') as file:
                file.write(p12.export(self.passentry.get().encode('utf-8')))
        except:
            messagebox.showinfo('Fehler!', 'Kann PKCS12-Datei {} nicht schreiben!'.format(outfile))
        self.master.quit()

    # toggle password hiding
    def toggleShow(self):
        if self.passentry["show"] == "":
            self.passentry["show"] = "●"
        else:
            self.passentry["show"] = ""

    # get input file
    def askInputFile(self):
        # Read input file
        while True:
            self.infilename = filedialog.askopenfilename(title='P12-Eingabedatei',
                                                         filetypes=(('PKCS12-Dateien', ('*.p12', '*.P12', '*.pfx', '*.PFX')),
                                                           ('alle Dateien', '*.*')))
            if not (self.infilename):
                messagebox.showerror('Abbruch', 'Die Dateiauswahl wurde abgebrochen!')
                self.master.quit()

            # Verify file exists; if so, break
            if isfile(self.infilename):
                try:
                    with open(self.infilename, 'rb') as pkcs12file:
                        self.infile = pkcs12file.read()
                except Exception as e:
                    messagebox.showerror("Ok", e)
                    continue
                return


# Create and hide root window
root = Tk()
decodegui = PasswordEntry(root)
root.mainloop()