import Tkinter
import ScrolledText
import tkFileDialog
import tkSimpleDialog

def open_command():
    global textWidget
    file = tkFileDialog.askopenfile()
    if file != None:
        contents = file.read()
        textWidget.delete("1.0", ScrolledText.END)
        textWidget.insert(ScrolledText.END, contents)

def save_command():
    global textWidget
    file = tkFileDialog.asksaveasfile()
    if file != None:
        contents = textWidget.get("1.0", ScrolledText.END)
        file.write(contents)
        file.close()
    
def exit_program():
    global root
    root.destroy()

def encrypt_xor_char(inString, passchar):
    outString = ""
    for ch in inString:
        ch = chr(ord(ch)^ord(passchar[0]))
        outString = outString + ch
    return outString

def encrypt_xor(inString, password):
    outString = ""
    index = 0
    for ch in inString:
        if ch.isalnum():
            ch = chr((ord(ch)^ord(password[index]))+1)
        outString = outString + ch
        index = (index+1)%len(password)
    return outString

def decrypt_xor(inString, password):
    outString = ""
    index = 0
    for ch in inString:
        try: 
            oldch = ch
            ch = chr((ord(ch)^ord(password[index]))-1)
        except ValueError:
            print ord(oldch)
            print ord(password[index])
            raise(SystemExit)
        outString = outString + ch
        index = (index+1)%len(password)
    return outString

def encrypt_command():
    global textWidget
    password = tkSimpleDialog.askstring("Password", "Please Enter your Password")
    if password != None:
        contents = textWidget.get("1.0", ScrolledText.END)
        encrypted = encrypt_xor(contents, password)
        textWidget.delete("1.0", ScrolledText.END)
        textWidget.insert(ScrolledText.END, encrypted)

def decrypt_command():
    global textWidget
    password = tkSimpleDialog.askstring("Password", "Please Enter your Password")
    if password != None:
        contents = textWidget.get("1.0", ScrolledText.END)
        encrypted = decrypt_xor(contents, password)
        textWidget.delete("1.0", ScrolledText.END)
        textWidget.insert(ScrolledText.END, encrypted)

if __name__ == "__main__":
    root = Tkinter.Tk()
    textWidget = ScrolledText.ScrolledText(root, width=80, height=50)
    textWidget.pack()

    menuBar = Tkinter.Menu(root, tearoff=0)
    fileMenu = Tkinter.Menu(menuBar, tearoff=0)
    fileMenu.add_command(label="Open", command=open_command)
    fileMenu.add_command(label="Save", command=save_command)
    fileMenu.add_separator()
    fileMenu.add_command(label="Quit", command=exit_program)
    menuBar.add_cascade(label="File", menu=fileMenu)
    encryptMenu = Tkinter.Menu(menuBar, tearoff=0)
    encryptMenu.add_command(label="Encrypt", command=encrypt_command)
    encryptMenu.add_command(label="Decrypt", command=decrypt_command)
    menuBar.add_cascade(label="Encrypt", menu=encryptMenu)

    root.config(menu=menuBar)
    
    root.mainloop()
