Connecting Tech Pros Worldwide Help | Site Map

Encrypt the password

Newbie
 
Join Date: Feb 2009
Posts: 8
#1: Apr 18 '09
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.
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python
  2. from Tkinter import *
  3. import tkMessageBox
  4. import Tkinter
  5. import time
  6.  
  7.  
  8.  
  9. curtime = ''
  10. clock = Tkinter.Label(bg="tan")
  11. clock.grid(row=22,column=0,columnspan=3,sticky=E)
  12.  
  13. def tick():
  14.     global curtime
  15.     newtime = time.strftime("TIME:"'%d/%m/%d %H:%M:%S')
  16.     if newtime != curtime:
  17.         curtime = newtime
  18.         clock.config(text=curtime)
  19.     clock.after(200, tick)
  20.  
  21. tick()
  22.  
  23. class GUIFramework(Frame):
  24.     """This is the GUI"""
  25.  
  26.     def __init__(self,master=None):
  27.         """Initialize yourself"""
  28.  
  29.         """Initialise the base class"""
  30.         Frame.__init__(self,master)
  31.  
  32.         """Set the Window Title"""
  33.         self.master.title("LOGIN FORM")
  34.         self.master.config(bg="wheat")
  35.         """Display the main window"
  36.         with a little bit of padding"""
  37.         self.grid(padx=10,pady=10)
  38.         self.CreateWidgets()
  39.  
  40.     def CreateWidgets(self):
  41.         """Create all the widgets that we need"""
  42.         self.config(bg="wheat")
  43.         self.lbText = Label(self,text="LOGIN DETAILS",bg="tan",relief="solid")
  44.         self.lbText.grid(row=0, column=0,sticky=NSEW,columnspan=50,pady=10)
  45.  
  46.         """Create the Text"""
  47.         self.lbText1 = Label(self,text="User Name:",bg="wheat",relief="groove",width=20)
  48.         self.lbText1.grid(row=1, column=0,sticky=W,columnspan=1)
  49.  
  50.         """Create the Entry, set it to be a bit wider"""
  51.         self.enText1 = Entry(self, width=25)
  52.         self.enText1.grid(row=1, column=1, columnspan=3,sticky=W)
  53.  
  54.         self.lbText2 = Label(self, text="Password:",bg="wheat",relief="groove",width=20)
  55.         self.lbText2.grid(row=2, column=0,sticky=W)
  56.  
  57.         self.enText2 = Entry(self,width=25,show="*")
  58.         self.enText2.grid(row=2, column=1, columnspan=3,sticky=W)
  59.  
  60.         self.lbText3 = Label(self, text="Confirm Password:",bg="wheat",relief="groove",width=20)
  61.         self.lbText3.grid(row=3, column=0,sticky=W)
  62.  
  63.         self.enText3 = Entry(self,width=25,show="*")
  64.         self.enText3.grid(row=3, column=1, columnspan=3,sticky=W)
  65.  
  66.         self.btnsubmit = Button(self, text="Login",command=self.login, bg='wheat',relief="groove",width=8)
  67.         self.btnsubmit.grid(row=4, column=0,sticky=E,pady=10)
  68.  
  69.     def login(self):
  70.         uname=self.enText1.get()
  71.         pwd=self.enText2.get()
  72.         cpwd = self.enText3.get()
  73.         try:
  74.             import os
  75.             import MySQLdb
  76.             import _mysql_exceptions as DB_EXC
  77.             from hashlib import md5
  78.             cxn = MySQLdb.connect(user ='root')
  79.             cur = cxn.cursor()
  80.             no = cur.execute("insert into abc.users values (%s,%s)", (uname,pwd))  # here abc is the database and users is the table name
  81.             new = md5
  82.             st = cur.execute("update abc.users set pwd=MD5(pwd) where uname=%s", (uname))
  83.             cur.close()
  84.             cxn.commit()
  85.             cxn.close()
  86.             if (no != 0):
  87.                 tkMessageBox.showinfo("Text", "succesfully logged." )
  88.             else:
  89.                 tkMessageBox.showinfo("Text", "Invalid User Name and password.")
  90.         except ImportError , e:
  91.              return None
  92.  
  93. if __name__ == "__main__":
  94.     guiFrame = GUIFramework()
  95.     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
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#2: Apr 19 '09

re: Encrypt the password


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
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#3: Apr 20 '09

re: Encrypt the password


You can compare the hash value of the plain text to the stored hash value. Here's a simplified example using a dictionary:
Expand|Select|Wrap|Line Numbers
  1. import md5
  2.  
  3. dd = {'chiller': md5.new('abcdef123').digest(),
  4.       'hotpants': md5.new('zyx987').digest()}
  5.  
  6.     def login(self):
  7.         uname=self.enText1.get()
  8.         pwd=self.enText2.get()
  9.         cpwd = self.enText3.get()
  10.         if pwd == cpwd and dd[uname] == md5.new(pwd).digest():
  11.             tkMessageBox.showinfo("Text", "Succesfully logged.")
  12.             self.master.destroy()
  13.         else:
  14.             tkMessageBox.showinfo("Text", "Invalid User Name and password.")
Newbie
 
Join Date: Feb 2009
Posts: 8
#4: Apr 20 '09

re: Encrypt 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:
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python
  2. from Tkinter import *
  3. import tkMessageBox
  4. import Tkinter
  5. import time
  6.  
  7.  
  8. curtime = ''
  9. clock = Tkinter.Label(bg="tan")
  10. clock.grid(row=22,column=0,columnspan=3,sticky=E)
  11.  
  12. def tick():
  13.     global curtime
  14.     newtime = time.strftime("TIME:"'%d/%m/%d %H:%M:%S')
  15.     if newtime != curtime:
  16.         curtime = newtime
  17.         clock.config(text=curtime)
  18.     clock.after(200, tick)
  19.  
  20. tick()
  21.  
  22. class GUIFramework(Frame):
  23.     """This is the GUI"""
  24.  
  25.     def __init__(self,master=None):
  26.         """Initialize yourself"""
  27.  
  28.         """Initialise the base class"""
  29.         Frame.__init__(self,master)
  30.  
  31.         """Set the Window Title"""
  32.         self.master.title("LOGIN FORM")
  33.         self.master.config(bg="wheat")
  34.         """Display the main window"
  35.         with a little bit of padding"""
  36.         self.grid(padx=10,pady=10)
  37.         self.CreateWidgets()
  38.  
  39.     def CreateWidgets(self):
  40.         """Create all the widgets that we need"""
  41.         self.config(bg="wheat")
  42.         self.lbText = Label(self,text="LOGIN DETAILS",bg="tan",relief="solid")
  43.         self.lbText.grid(row=0, column=0,sticky=NSEW,columnspan=50,pady=10)
  44.  
  45.         """Create the Text"""
  46.         self.lbText1 = Label(self,text="User Name:",bg="wheat",relief="groove",width=20)
  47.         self.lbText1.grid(row=1, column=0,sticky=W,columnspan=1)
  48.  
  49.         """Create the Entry, set it to be a bit wider"""
  50.         self.enText1 = Entry(self, width=25)
  51.         self.enText1.grid(row=1, column=1, columnspan=3,sticky=W)
  52.  
  53.         self.lbText2 = Label(self, text="Password:",bg="wheat",relief="groove",width=20)
  54.         self.lbText2.grid(row=2, column=0,sticky=W)
  55.  
  56.         self.enText2 = Entry(self,width=25,show="*")
  57.         self.enText2.grid(row=2, column=1, columnspan=3,sticky=W)
  58.  
  59.         self.btnsubmit = Button(self, text="Login",command=self.login, bg='wheat',relief="groove",width=8)
  60.         self.btnsubmit.grid(row=3, column=0,sticky=E,pady=10)
  61.  
  62.     def login(self):
  63.         uname=self.enText1.get()
  64.         pwd=self.enText2.get()
  65.         try:
  66.             import os
  67.             import MySQLdb
  68.             from hashlib import md5
  69.             import _mysql_exceptions as DB_EXC
  70.             cxn = MySQLdb.connect(user ='root')
  71.             cur = cxn.cursor()
  72.             pwd= md5(pwd) 
  73.             no = cur.execute("select * from venlabs.users where uname=%s and pwd=%s",(uname,pwd))
  74.             cur.close()
  75.             cxn.commit()
  76.             cxn.close()
  77.             if (no != 0):
  78.                 tkMessageBox.showinfo("Text", "succesfully logged." )
  79.                 self.master.forward("/invoice1.py")
  80.             else:
  81.                 tkMessageBox.showinfo("Text", "Invalid User Name and password.")
  82.         except ImportError , e:
  83.              return None
  84.  
  85. if __name__ == "__main__":
  86.     guiFrame = GUIFramework()
  87.     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 ***
Newbie
 
Join Date: Feb 2009
Posts: 8
#5: Apr 21 '09

re: Encrypt 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
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,560
#6: Apr 21 '09

re: Encrypt the password


Quote:

Originally Posted by cnivas View Post

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
micmast's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Belgium
Posts: 136
#7: Apr 22 '09

re: Encrypt the password


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 :)
Reply