Encrypt the password

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cnivas
    New Member
    • Feb 2009
    • 8

    Encrypt the password

    Hai All,

    Good Evening,

    I'm doing a small application using python and MySQL in APPLE MACINTOSH OPERATING SYSTEM. I want to to store the password in encrypted form in the database. While retriving the password it should check the encrypted text with the plain text and it should navigate to the other form.

    I have done the encrypted form, but while checking the parameters it should not check the plain text with the encrypted text. It only checks the encrypted text in the database.

    This is the code I have given it. Please Check the code and if any modifications please correct it and send to me.
    Code:
    #! /usr/bin/env python
    from Tkinter import *
    import tkMessageBox
    import Tkinter
    import time
    
    
    
    curtime = ''
    clock = Tkinter.Label(bg="tan")
    clock.grid(row=22,column=0,columnspan=3,sticky=E)
    
    def tick():
        global curtime
        newtime = time.strftime("TIME:"'%d/%m/%d %H:%M:%S')
        if newtime != curtime:
            curtime = newtime
            clock.config(text=curtime)
        clock.after(200, tick)
    
    tick()
    
    class GUIFramework(Frame):
        """This is the GUI"""
        
        def __init__(self,master=None):
            """Initialize yourself"""
            
            """Initialise the base class"""
            Frame.__init__(self,master)
            
            """Set the Window Title"""
            self.master.title("LOGIN FORM")
            self.master.config(bg="wheat")
            """Display the main window"
            with a little bit of padding"""
            self.grid(padx=10,pady=10)
            self.CreateWidgets()
           
        def CreateWidgets(self):
            """Create all the widgets that we need"""
            self.config(bg="wheat")
            self.lbText = Label(self,text="LOGIN DETAILS",bg="tan",relief="solid")
            self.lbText.grid(row=0, column=0,sticky=NSEW,columnspan=50,pady=10)
    
            """Create the Text"""
            self.lbText1 = Label(self,text="User Name:",bg="wheat",relief="groove",width=20)
            self.lbText1.grid(row=1, column=0,sticky=W,columnspan=1)
            
            """Create the Entry, set it to be a bit wider"""
            self.enText1 = Entry(self, width=25)
            self.enText1.grid(row=1, column=1, columnspan=3,sticky=W)
    
            self.lbText2 = Label(self, text="Password:",bg="wheat",relief="groove",width=20)
            self.lbText2.grid(row=2, column=0,sticky=W)
    
            self.enText2 = Entry(self,width=25,show="*")
            self.enText2.grid(row=2, column=1, columnspan=3,sticky=W)
    
            self.lbText3 = Label(self, text="Confirm Password:",bg="wheat",relief="groove",width=20)
            self.lbText3.grid(row=3, column=0,sticky=W)
    
            self.enText3 = Entry(self,width=25,show="*")
            self.enText3.grid(row=3, column=1, columnspan=3,sticky=W)
    
            self.btnsubmit = Button(self, text="Login",command=self.login, bg='wheat',relief="groove",width=8)
            self.btnsubmit.grid(row=4, column=0,sticky=E,pady=10)
    
        def login(self):
            uname=self.enText1.get()
            pwd=self.enText2.get()
            cpwd = self.enText3.get()
            try:
                import os
                import MySQLdb
                import _mysql_exceptions as DB_EXC
                from hashlib import md5
                cxn = MySQLdb.connect(user ='root')
                cur = cxn.cursor()
                no = cur.execute("insert into abc.users values (%s,%s)", (uname,pwd))  # here abc is the database and users is the table name
                new = md5
                st = cur.execute("update abc.users set pwd=MD5(pwd) where uname=%s", (uname))
                cur.close()
                cxn.commit()
                cxn.close()
                if (no != 0):
                    tkMessageBox.showinfo("Text", "succesfully logged." )
                else:
                    tkMessageBox.showinfo("Text", "Invalid User Name and password.")
            except ImportError , e:
                 return None
    
    if __name__ == "__main__":
        guiFrame = GUIFramework()
        guiFrame.mainloop()
    Please Check the code and if there are any corrections made it and send to me. It is very urgent.
    Please help me.
    Thank You in advance.
    Warm Regards
    Srinivas
    Last edited by bvdet; Apr 19 '09, 09:34 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please use code tags. See "How to ask a question" in Posting Guidelines.

    Are you having a problem with your code? Do you have a question?

    -BV
    Moderator

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      You can compare the hash value of the plain text to the stored hash value. Here's a simplified example using a dictionary:[code=Python]import md5

      dd = {'chiller': md5.new('abcdef 123').digest(),
      'hotpants': md5.new('zyx987 ').digest()}

      def login(self):
      uname=self.enTe xt1.get()
      pwd=self.enText 2.get()
      cpwd = self.enText3.ge t()
      if pwd == cpwd and dd[uname] == md5.new(pwd).di gest():
      tkMessageBox.sh owinfo("Text", "Succesfull y logged.")
      self.master.des troy()
      else:
      tkMessageBox.sh owinfo("Text", "Invalid User Name and password.")[/code]

      Comment

      • cnivas
        New Member
        • Feb 2009
        • 8

        #4
        How to encrtrypt and decrypt the password.

        Hai,
        Good Evening,
        I'm doing a small application using python and MySQL. I want to encrypt the password to store in the database and also to decrypt the password while retriving the same password and also to redirect the next form.

        While it stores in the database it should be in encrypted form and while accessing the password it should be decrypt it and checks the plain text with the encrypted text and it redirect to the next form.

        This is the sample code:
        Code:
        #! /usr/bin/env python
        from Tkinter import *
        import tkMessageBox
        import Tkinter
        import time
        
        
        curtime = ''
        clock = Tkinter.Label(bg="tan")
        clock.grid(row=22,column=0,columnspan=3,sticky=E)
        
        def tick():
            global curtime
            newtime = time.strftime("TIME:"'%d/%m/%d %H:%M:%S')
            if newtime != curtime:
                curtime = newtime
                clock.config(text=curtime)
            clock.after(200, tick)
        
        tick()
        
        class GUIFramework(Frame):
            """This is the GUI"""
            
            def __init__(self,master=None):
                """Initialize yourself"""
                
                """Initialise the base class"""
                Frame.__init__(self,master)
                
                """Set the Window Title"""
                self.master.title("LOGIN FORM")
                self.master.config(bg="wheat")
                """Display the main window"
                with a little bit of padding"""
                self.grid(padx=10,pady=10)
                self.CreateWidgets()
               
            def CreateWidgets(self):
                """Create all the widgets that we need"""
                self.config(bg="wheat")
                self.lbText = Label(self,text="LOGIN DETAILS",bg="tan",relief="solid")
                self.lbText.grid(row=0, column=0,sticky=NSEW,columnspan=50,pady=10)
        
                """Create the Text"""
                self.lbText1 = Label(self,text="User Name:",bg="wheat",relief="groove",width=20)
                self.lbText1.grid(row=1, column=0,sticky=W,columnspan=1)
                
                """Create the Entry, set it to be a bit wider"""
                self.enText1 = Entry(self, width=25)
                self.enText1.grid(row=1, column=1, columnspan=3,sticky=W)
        
                self.lbText2 = Label(self, text="Password:",bg="wheat",relief="groove",width=20)
                self.lbText2.grid(row=2, column=0,sticky=W)
        
                self.enText2 = Entry(self,width=25,show="*")
                self.enText2.grid(row=2, column=1, columnspan=3,sticky=W)
        
                self.btnsubmit = Button(self, text="Login",command=self.login, bg='wheat',relief="groove",width=8)
                self.btnsubmit.grid(row=3, column=0,sticky=E,pady=10)
        
            def login(self):
                uname=self.enText1.get()
                pwd=self.enText2.get()
                try:
                    import os
                    import MySQLdb
                    from hashlib import md5
                    import _mysql_exceptions as DB_EXC
                    cxn = MySQLdb.connect(user ='root')
                    cur = cxn.cursor()
                    pwd= md5(pwd) 
                    no = cur.execute("select * from venlabs.users where uname=%s and pwd=%s",(uname,pwd))
                    cur.close()
                    cxn.commit()
                    cxn.close()
                    if (no != 0):
                        tkMessageBox.showinfo("Text", "succesfully logged." )
                        self.master.forward("/invoice1.py")
                    else:
                        tkMessageBox.showinfo("Text", "Invalid User Name and password.")
                except ImportError , e:
                     return None
        
        if __name__ == "__main__":
            guiFrame = GUIFramework()
            guiFrame.mainloop()
        Please help me in this case. I'm struggling from last one month.

        Thanks in advance for helping me.

        Warm Regards,
        Srinivas.

        *** Moderator Note ***
        This post was moved into an existing thread because it was a double post.
        *** Moderator Note ***
        Last edited by bvdet; Apr 20 '09, 01:28 PM. Reason: Add code tags

        Comment

        • cnivas
          New Member
          • Feb 2009
          • 8

          #5
          how to encrypt and decrypt the password.

          Good Evening,

          I'm doing a small application using python and mysql. I have encrypted the password using md5. but it is one way process. but I want to encrypt the password before storing into the database and while retriving the values it should be checked the plain text with the encrypted text.
          Please help me.
          Thanks in advance.
          Warm Regards,
          Srinivas

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by cnivas
            Good Evening,

            I'm doing a small application using python and mysql. I have encrypted the password using md5. but it is one way process. but I want to encrypt the password before storing into the database and while retriving the values it should be checked the plain text with the encrypted text.
            Please help me.
            Thanks in advance.
            Warm Regards,
            Srinivas
            Please see post #3 of this thread. Does this information help at all?

            -BV

            Comment

            • micmast
              New Member
              • Mar 2008
              • 144

              #7
              from a security point of view I would suggest SHA1 to hash passwords.
              If you want to decrypt the password I would suggest using triple DES, but as you want to decrypt the password you need to store a key somewhere, which everybody could find. Again from a security point of view this is not the best thing to do :)

              Comment

              Working...