1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Python code for text editor

Discussion in 'Black Hat SEO' started by sfidirectory, Jun 10, 2010.

  1. sfidirectory

    sfidirectory Senior Member

    Joined:
    Mar 29, 2010
    Messages:
    904
    Likes Received:
    487
    Occupation:
    Web developer/BTC enthusiast
    Location:
    php artisan make:migration
    Home Page:
    Hi everyone,

    Here is some Python code I have been working on as part of my Degree. It basically is a text editor that can open/save files, encrypt/decrypt files. I have yet to compile it but I thought that if there are any Python gurus here on BHW that can do something with this code (like add even more features etc), or if anyone wants to muck around with it.

    Only thing is, it seems to have errors when I run it with IDLE, but if I open it from a shell it works fine. I had a postgrad student (who now works in the Comp Sci dept at my university) to confirm that it does work by running it from a shell (or bash terminal on a mac).

    Enough with the blabbering, heres the code:

    Code:
    import Tkinter
    import ScrolledText
    import tkFileDialog
    import tkSimpleDialog
    import random
    
    class EncryptTextEdit:
    
        def __init__(self, parent):
            self.parent = parent
            self.textWidget = ScrolledText.ScrolledText(parent, width=80, height=50)
            self.textWidget.pack()
    
            self.menuBar = Tkinter.Menu(parent, tearoff=0)
            self.fileMenu = Tkinter.Menu(self.menuBar, tearoff = 0)
            self.fileMenu.add_command(label="Open", command=self.open_command)
            self.fileMenu.add_command(label="Save", command=self.save_command)
            self.fileMenu.add_separator()
            self.fileMenu.add_command(label = "Quit", command=self.exit_program)
            self.menuBar.add_cascade(label = "File", menu=self.fileMenu)
            self.encryptMenu = Tkinter.Menu(self.menuBar, tearoff=0)
            self.encryptMenu.add_command(label = "Encrypt", command = self.encrypt_command)
            self.encryptMenu.add_command(label = "Decrypt", command = self.decrypt_command)
            self.menuBar.add_cascade(label = "Encrypt", menu=self.encryptMenu)
            parent.config(menu=self.menuBar)
    
        def open_command(self):
            file = tkFileDialog.askopenfile()
            if file != None:
                contents = file.read()
                self.textWidget.delete("1.0", ScrolledText.END)[:-1]
                file.write(contents)
                file.close()
    
        def save_command(self):
            file = tkFileDialog.asksaveasfile()
            if file != None:
                contents = self.textWidget.get("1.0", ScrolledText.END)[:-1]
                file.write(contents)
                file.close()
    
        def exit_program(self):
            self.parent.destroy()
    
        def caesarEncrypt(self, char, rotateBy):
            """Encrypt a single character"""
            return chr((ord(char)+rotateBy)%126+1)
    
        def caesarDecrypt(self, char, rotateBy):
            """Decrypt a single character"""
            return chr((ord(char)-rotateBy-1)%126)
    
        def encrypt(self, inString, password):
            """Encrypt a string"""
            outString = ""
            index = 0
            for ch in inString:
                ch = self.caesarEncrypt(ch, ord(password[index]))
                outString = outString + ch
                index = (index + 1) % len(password)
            return outString
    
        def decrypt(self, inString, password):
            """Decrypt a string"""
            outString = ""
            index = 0
            for ch in inString:
                ch = self.caesarDecrypt(ch, ord(password[index]))
                outString = outString + ch
                index = (index +1) % len(password)
            return outString
    
        def encrypt_command(self):
            password = tkSimpleDialog.askstring("Password", "Please enter your password")
            if password != None:
                contents = self.textWidget.get("1.0",ScrolledText.END)[:-1]
                encrypted = self.encrypt(contents, password)
                self.textWidget.delete("1.0", ScrolledText.END)
                self.textWidget.insert(ScrolledText.END, encrypted)
    
        def decrypt_command(self):
            password = tkSimpleDialog.askstring("Password", "Please enter your password")
            if password != None:
                contents = self.textWidget.get("1.0", ScrolledText.END)[:-1]
                encrypted = self.decrypt(contents, password)
                self.textWidget.delete("1.0", ScrolledText.END)
                self.textWidget.insert(ScrolledText.END, encrypted)
    
    if __name__ == "__main__":
        root = Tkinter.Tk()
        textApp = EncryptTextEdit(root)
        root.mainloop()
    
    Hope this is a good share? The more skills I get the more I will be able to put up on here, so keep an eye out :).

    Cheers and long live BHW!