I made a random password generator for a project and it works fine, but when it generates passwords, it always adds " { " to a password or two. Why does it add this and how do I fix it? - import os
-
import random
-
from Tkinter import *
-
from tkMessageBox import *
-
-
class Application(Frame):
-
def __init__(self, master=None):
-
Frame.__init__(self, master)
-
self.grid()
-
self._passwordLength = StringVar()
-
self._passwordLength.set("")
-
self._passwordAmount = StringVar() # create a String control variable
-
self._passwordAmount.set("")
-
self._fileName = StringVar()
-
self._fileName.set("")
-
top = self.winfo_toplevel() # find top-level window
-
top.title("Random Password Generator")
-
self._createWidgets()
-
-
def _createWidgets(self):
-
siteLabel = Label(self, text="Password Lengths Wanted:", anchor = E, width = 20)
-
siteLabel.grid(row = 0, column = 0)
-
siteLabel = Label(self, text="Password Amount Wanted:", anchor = E, width = 20)
-
siteLabel.grid(row = 1, column = 0)
-
siteLabel = Label(self, text="File Name:", anchor = E, width = 20)
-
siteLabel.grid(row = 2, column = 0)
-
lengthEntry = Entry(self, takefocus = 1,
-
textvariable = self._passwordLength)
-
lengthEntry.grid(row = 0, column = 1, sticky=E+W)
-
amountEntry = Entry(self, takefocus = 1,
-
textvariable = self._passwordAmount)
-
amountEntry.grid(row = 1, column = 1, sticky=E+W)
-
fileEntry = Entry(self, takefocus = 1,
-
textvariable = self._fileName)
-
fileEntry.grid(row = 2, column = 1, sticky=E+W)
-
self._button = Button(self, text = "Generate", command = self._getData, width = 20)
-
self._button.grid(row = 3, column = 0)
-
self._button = Button(self, text = "Quit", command = self._quitProgram, width = 20)
-
self._button.grid(row = 3, column = 1)
-
-
def _getData(self):
-
try:
-
passwordLength = int(self._passwordLength.get())
-
passwordAmount = int(self._passwordAmount.get())
-
except:
-
showwarning("Error","Please check you input.")
-
self._generatePasswords(passwordLength, passwordAmount)
-
-
def _generatePasswords(self, passwordLength, passwordAmount):
-
fileName = str(self._fileName.get())
-
if ".txt" not in fileName:
-
fileName += ".txt"
-
if os.path.isfile(fileName):
-
if askyesno("Verify", "This file already exists, would you like to overwrite it?"):
-
fileToWrite = open(fileName, "w")
-
else:
-
fileToWrite = open(fileName, "w")
-
try:
-
randomize(passwordLength, passwordAmount, fileToWrite)
-
if True:
-
showwarning("Random Password Generator", "Passwords Created! Check your folder!")
-
except:
-
showwarning("Error"," Please check your input.")
-
-
def _quitProgram(self):
-
if askyesno('Verify', 'Are you sure you would like to quit the program?'):
-
root.destroy()
-
-
# end class Application
-
-
def main():
-
Application().mainloop()
-
-
def randomize(length, amount, fileToWrite):
-
for num in xrange(amount):
-
newPassword = ""
-
for count in xrange(length):
-
randomInt = random.randint(0,26)
-
newPassword += chr(randomInt+97)
-
count += 1
-
fileToWrite.write(newPassword+"\n")
-
fileToWrite.close()
-
-
root = Tk()
-
main()
-
In ASCII, the { character follows immediately after lowercase z. When you're generating a random number, you're asking for a value between 0 and 26 inclusive, which gives you 27 possibilities total.
Try changing line 78 to - randomInt = random.randint(0, 25)
and see where that gets you.
7 2141
In ASCII, the { character follows immediately after lowercase z. When you're generating a random number, you're asking for a value between 0 and 26 inclusive, which gives you 27 possibilities total.
Try changing line 78 to - randomInt = random.randint(0, 25)
and see where that gets you.
Worked like a charm. Thank you! :]
Also, if it isn't too much to ask. How would I format this: -
try:
-
randomize(passwordLength, passwordAmount, fileToWrite)
-
if True:
-
showwarning("Random Password Generator", "Passwords Created! Check your folder!")
-
So it would say "Passwords Created! Check your folder for (file name)!"
bvdet 2,851
Expert Mod 2GB - showwarning("Random Password Generator", "Passwords Created! Check your folder for %s!" % fileToWrite)
Randomly generated passwords are often made from a combination of digits, upper case letters and lower case letters. - >>> import string
-
>>> import random
-
>>> passwordLen = 8
-
>>> seq = string.digits+string.ascii_letters
-
>>> "".join([random.choice(seq) for i in range(passwordLen)])
-
'v5JuKQoT'
-
>>>
You can use a loop to create the 3 labels if you want. - for ctr, lit in enumerate(["Password Lengths Wanted:", "Password Amount Wanted:", "File Name:"]):
-
siteLabel = Label(self, text=lit, anchor = E, width = 20)
-
siteLabel.grid(row = ctr, column = 0)
Also, you have 2 separate varaibles named "count". The second one does nothing here and can be deleted. - def randomize(length, amount, fileToWrite):
-
for num in xrange(amount):
-
newPassword = ""
-
for count in xrange(length):
-
randomInt = random.randint(0,26)
-
newPassword += chr(randomInt+97)
-
## this does nothing so it can be deleted
-
count += 1
Thanks bvdet! Your answers are always short, sweet and get right to the point!
How would I implement your code to making the password more random?
bvdet 2,851
Expert Mod 2GB
Sorry it took so long to respond.
I would rewite function randomize as follows: - import string, random
-
-
def randomize(length, amount, fileToWrite):
-
seq = string.digits+string.ascii_letters
-
fileToWrite.write("\n".join(["".join([random.choice(seq) \
-
for i in range(length)]) \
-
for i in xrange(amount)]))
-
fileToWrite.close()
Note that writing to the file once is much faster than writing to the file for each password. The same thing holds true for print.
Thanks again for the help bvdet! It worked out great!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lobang Trader |
last post by:
Hi all,
I am trying to create a username and a password class.
I would like to know what are the RECOMMENDED minimum and maximum length
for both fields?
These fields will be something like...
|
by: Nitin |
last post by:
I have created a console based application using C# which asks for the user identification to work using that system. Now, the problem is that i'm not able to hide or encrypt the password on the...
|
by: Skeleton Man |
last post by:
Hi,
I came across the basic algorithmfor decrypting WS_FTP Pro 6 passwords as
follows, and I'm trying to reverse it to make an encryption function:
function ws_dec()
{
var str =...
|
by: julianmoors |
last post by:
Hey,
Currently I'm writing a VB.NET/1.1 app and I need to mask the input for
the password field. Does anyone know how to do this in VB? I've seen a
C# example, but wouldn't know how to convert...
|
by: UJ |
last post by:
Does anybody have lying around a Regex for a strong password (or a routine
to check it). I'd like the normal First character has to be a character,
total of 8 characters and has to have at least...
|
by: JIM.H. |
last post by:
I have a password field in my login page (ASP.NET 1.1), how can I validate
password based on at least 6 characters, one number, and one capital letter.
|
by: Gilbert |
last post by:
I am trying to install DB2 connect V 8.2 on my new Windows XP PC, a
Dell Optiplex GX620 with 4GB of RAM. I have installed this version of
DB2 connect on other Windows XP PC's without any problem....
|
by: rajbala.3399 |
last post by:
Hi ,
I want to download sql in my linux system...........
# rpm -ivh
MySQL-server-5.0.24a-0.glibc23.i386.rpm MySQL-cl
ient-5.0.24a-0.glibc23.i386.rpm
Preparing... ...
|
by: ssetz |
last post by:
Hello,
For work, I need to write a password filter. The problem is that my C+
+ experience is only some practice in school, 10 years ago. I now
develop in C# which is completely different to me....
|
by: Thana1995 |
last post by:
Write a program that asks the user to give a two-character password(defined by constants within the program). the program should then test the validity of the password. if it is incorrect after three...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |