im kinda new to python and am programming a password generator. As of now, i think i am at a plateau where i need explanation.
At the end, where i want to generate a password with the user input given above, i get a type error
(TypeError: choice() takes 2 positional arguments but 3 were given)
What am I missing, so that the random.choice function is not working
import random
Uppercaseletters = open("Upper.txt").read()
Lowercaseletters = open("Lower.txt").read()
Numbers = open("Zahlen.txt").read()
Symbols = open("Symbole.txt").read()
Upperbool = True
Lowerbool = True
Numbersbool = True
Symbolsbool = True
whole = ""
if Upperbool:
whole += Uppercaseletters
if Lowerbool:
whole += Lowercaseletters
if Numbersbool:
whole += Numbers
if Symbolsbool:
whole += Symbols
print("Hello and welcome to the simple password generator.")
a = 1
b = 1
if b <= 10:
amount = int(input("How many passwords do you want to generate? "))
else:
print("You are exceeding the limit of a maximum of 10 Passwords")
# length auswählen lassen (maximal 20 Zeichen lang (Fehler prevention))
if a <= 20:
length = int(input("How long do you want your password to be? "))
else:
print("That password will be too long, try a number below 20")
for x in range(amount):
password = "".join(random.choice(whole, length))
print(password)
I believe you are looking for something like this:
import random
Uppercaseletters = open("Upper.txt").read()
Lowercaseletters = open("Lower.txt").read()
Numbers = open("Zahlen.txt").read()
Symbols = open("Symbole.txt").read()
Upperbool = True
Lowerbool = True
Numbersbool = True
Symbolsbool = True
whole = ""
if Upperbool:
whole += Uppercaseletters
if Lowerbool:
whole += Lowercaseletters
if Numbersbool:
whole += Numbers
if Symbolsbool:
whole += Symbols
print("Hello and welcome to the BMD's simple password generator.")
amount = 100
length = 100
while amount>10:
amount = int(input("How many passwords do you want to generate? "))
if amount>10:
print("You are exceeding the limit of a maximum of 10 Passwords")
while length>20:
length = int(input("How long do you want your password to be? "))
if length>20:
print("That password will be too long, try a number below 20")
for passwords in range(amount):
password = ""
for character in range(length):
password = password + random.choice(list(whole))
print(password)
I modified it so that it does not allow amounts above 10 and lengths above 20.
Related
I am writing a program to encrypt / decrypt an inputted message using a key generated from a pool
The pool is created by appending unicode characters to an array
When selecting option 2 (encrypt) and reading the genkey.txt file (after generating the key with option 1), the program shows an index error as shown:
Exception has occurred: IndexError
list index out of range, line 58, in b = keyarray[a]
However, when removing the unicode script and replacing the pool with plain text (e.g [a, b, c...]) the message is encrypted without any issue.
Any way to fix this?
Code:
import random
programrun = 0
encryptedarray = []
decryptedarray = []
keyarray = []
while programrun < 1:
pool = []
for i in range(32,123): #Unicode characters
pool.append(chr(i))
print("Encryption / Decryption")
print("")
print("1. Generate Key")
print("2. Encrypt")
print("3. Decrypt")
print("4. Quit")
print("NOTE: Key must be generated before selecting Encryption / Decryption")
print("")
option = int(input("Enter the number corresponding to the option: "))
if option == 1:
a = 20
while a > 0:
b = random.randint(0,57)
c = pool[b]
keyarray.append(c)
a = a - 1
keygen = ("".join(keyarray))
print("Your generated key is:", keygen)
print("")
print("(Make sure you have generated a key before typing 'yes')")
writebool = input("Do you want to save the file to your computer? ")
if writebool == "yes":
keyfile = open("genkey.txt", "x")
keyfile.write(keygen)
keyfile.close()
wbval = 1
print("File saved to genkey.txt successfully")
print("")
quitval = 1
elif writebool == "no":
print("ok")
else:
print("Type yes or no")
elif option == 2:
encryptvalid = 0
while encryptvalid < 1:
msg = str(input("Enter the message to be encrypted: "))
genkeyf = open("genkey.txt", "r")
genkeydata = genkeyf.read()
if genkeydata == keygen:
print("File VALID")
encryptvalid = 1
for i in msg:
a = pool.index(i)
b = keyarray[a]
encryptedarray.append(b)
p = ("".join(encryptedarray))
print("")
print("Your encrypted string is: ")
print("".join(encryptedarray))
else:
print("File INVALID")
print("Please check if the genkey.txt file matches the current generated key.")
a = pool.index(i) looks up the index of a character of the message in the pool, then that index is used to lookup a value in the keyarray with b = keyarray[a].
pool is 91 bytes long, but keyarray is 20 bytes long, so many of the indexes are too high. You probably want b = keyarray[a % len(keyarray)]. This uses the modulus operator to constrain the lookup from 0-19.
I have created a random password generator and it works but it displays "Here is your randomly generated password" for every character that it outputs. I would like it to put the full password in 1 string and display it to the user. Any help would be much appreciated.
import random
def password_generator():
length = int(input("Input length of password: "))
for n in range(length):
symbol_number = random.randint(33, 58)
character = random.randint(65, 123)
password = chr(symbol_number or character)
print(f"Here is your randomly generated password \nPassword: {password}")
password_generator()
One method you could use is to collect each item from the for loop inside a list and then join the list items together as a string before you display them to use user.
import random
def password_generator():
length = int(input("Input length of password: "))
password = []
for n in range(length):
symbol_number = random.randint(33, 58)
character = random.randint(65, 123)
password.append(chr(symbol_number or character))
password = "".join(password)
print(f"Here is your randomly generated password \nPassword: {password}")
password_generator()
What is the intended purpose of the or operator? I don't think it is going to do what you expect it to.
it displays "Here is your randomly generated password" for every character that it outputs.
That's because you have incorrectly indented your last line. It should be outside the for loop, not inside. You can fix it like so:
def password_generator():
length = int(input("Input length of password: "))
password = ""
for n in range(length):
symbol_number = random.randint(33, 58)
character = random.randint(65, 123)
password = password + chr(symbol_number or character)
print(f"Here is your randomly generated password \nPassword: {password}")
I am creating a password generator the takes the length of the desired password, number of letters, as well as the number of numbers. The password needs to contain uppercase letters as well as numbers and special characters. I am having trouble figuring out how to specify the number of letters and numbers in the password. This is what I have so far:
import random
charslet ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
charsnum = "1234567890"
charssym = "!##$%^&*"
def password():
pwd = ""
passlen = int(input("How long do you want the password to be?: "))
passlet = int(input("How many letters do you want in your password?: "))
passnum = int(input("How many numbers do you want in your password?: "))
passsym = int(passlen - (passlet + passnum))
chars = ""
for let in range(passlet):
chars += random.choice(charslet)
for num in range(passnum):
chars += random.choice(charsnum)
for sym in range(passsym):
chars += random.choice(charssym)
for p in range(passlen):
pwd += random.choice(chars)
print(pwd)
password()
I think the last part is what is confusing you. You are building the chars variable with the correct amount of specific chars, but you then choose between them again at the end.
You could just change:
for p in range(passlen):
password += random.choice(chars)
With
# option 1 - works better if working with lists
list_chars = list(chars)
random.shuffle(chars)
password = "".join(list_chars)
# option 2 - simpler solution for strings
password = "".join(random.sample(char, len(char)))
You could also use shuffle to select the chars before without the for loops, something like:
# for this to work your `charslet` must be a list
random.shuffle(charslet)
chars += "".join(charslet[:passlet])
This is the corrected code:
import random
charslet ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
charsnum = "1234567890"
charssym = "!##$%^&*"
def password():
pwd = ""
passlen = int(input("How long do you want the password to be?: "))
passlet = int(input("How many letters do you want in your password?: "))
passnum = int(input("How many numbers do you want in your password?: "))
passsym = int(passlen - (passlet + passnum))
chars = ""
for let in range(passlet):
chars += random.choice(charslet)
for num in range(passnum):
chars += random.choice(charsnum)
for sym in range(passsym):
chars += random.choice(charssym)
list_chars = list(chars)
random.shuffle(list_chars)
pwd = "".join(list_chars)
print(pwd)
password()
I replaced:
for p in range(passlen):
password += random.choice(chars)
with
list_chars = list(chars)
random.shuffle(list_chars)
pwd = "".join(list_chars)
Putting the altered chars variable in a list allowed me to shuffle it, randomizing it and allowing me to assign it to pwd
I am needing a bit of help on my encryption program. Instead of having the program just move the letters by two (c would become a or r would become p) I'd like to be able to have it reference 2 lists, the first one going from a-z normally and the other with letters in different order to act as the encrypt/decrypt side. Hopefully that makes sense. Here's what i have so far.
result = ''
choice = ''
message = ''
while choice != 0:
choice = input("\n Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
if choice == '1':
message = input('\nEnter message for encryption: ')
for i in range(0, len(message)):
result = result + chr(ord(message[i]) - 2)
print(result + '\n\n')
result = ''
if choice == '2':
message = input('\nEnter message to decrypt: ')
for i in range(0, len(message)):
result = result + chr(ord(message[i]) + 2)
print(result + '\n\n')
result = ''
elif choice != '0':
print('You have entered an invalid input, please try again. \n\n')
This works fine and dandy but i'd like to have the lists. Lets say list 1 is A,B,C,D,E and list 2 would be W,N,U,D,P. just for ease of use purposes.
Here is a solution, for small letters only. It can easily be modified to handle also capital letters, by adding them to the text strings.
As can be seen, the space character is at the same position in both lists. This is not necessary, as any character can be translated to any other. However if the decrypted or encrypted is not containing unique characters only, the program will break down.
decrypted = b"abcdefghijklmnopqrstuvwxyz "
encrypted = b"qwertyuiopasdfghjklzxcvbnm "
encrypt_table = bytes.maketrans(decrypted, encrypted)
decrypt_table = bytes.maketrans(encrypted, decrypted)
result = ''
choice = ''
message = ''
while choice != '0':
choice = input("\n Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
if choice == '1':
message = input('\nEnter message for encryption: ')
result = message.translate(encrypt_table)
print(result + '\n\n')
elif choice == '2':
message = input('\nEnter message to decrypt: ')
result = message.translate(decrypt_table)
print(result + '\n\n')
elif choice != '0':
print('You have entered an invalid input, please try again. \n\n')
Ok, so a few things here...
First I'll give you exactly what you were looking for and explain what I used and some of the changes that needed to be made to your original code. Then I'll explain some inherent issues what what you're trying to do and suggest some areas to read up on/some ways you might want to improve what you've got.
Here's the code you're looking for (while retaining the same flow as what you submitted put above):
import random
result = ''
choice = ''
message = ''
characters_in_order = [chr(x) for x in range(32,127)]
while choice != 0:
choice = input("\n Do you want to encrypt or decrypt the message?\n 1 to encrypt, 2 to decrypt or 0 to exit program. ")
if str(choice) == '1':
message = input('\nEnter message for encryption: ')
r_seed = input('Enter an integer to use as a seed: ')
random.seed(r_seed)
shuffled_list = [chr(x) for x in range(32,127)]
random.shuffle(shuffled_list)
for i in range(0, len(message)):
result += shuffled_list[characters_in_order.index(message[i])]
print(result + '\n\n')
result = ''
elif str(choice) == '2':
message = input('\nEnter message to decrypt: ')
r_seed = input('Enter an integer to use as a seed (should be the same one used to encrypt): ')
random.seed(r_seed)
shuffled_list = [chr(x) for x in range(32,127)]
random.shuffle(shuffled_list)
for i in range(0, len(message)):
result += characters_in_order[shuffled_list.index(message[i])]
print(result + '\n\n')
result = ''
elif str(choice) != '0':
print('You have entered an invalid input, please try again. \n\n')
You'll notice that I set a global 'characters in order' list, which is just every ASCII character (32-126) in order. I also imported the 'random' module and used this to shuffle the characters in order according to a seed that the user inputs. As long as this seed is the same on the encryption and decryption end, it will produce the same shuffled list and it should work to encrypt or decipher the same string. Also notice the str() around your input choices. Without that, the user had to input '1', rather than 1 to submit a choice without an error.
All of that said...
Notice that the way the new function works is by looking at a character's index in one list and pulling out the character at that index in another. The method you were using, of incrementing or decrementing a character's ASCII code is basic (though not much more basic than this), but it also has a pretty critical flaw, which is that characters on one end or another of the ASCII set wouldn't return ASCII characters. If you were encrypting it at a bit-level, which would be preferred, this wouldn't matter/would be irrelevant, but here you're not going to get the kind of string back that you want if you were to, for example, enter a [space] (ASCII 32) into your plaintext to be encrypted.
If you're interested, you might want to read up on symmetric key encryption/DES for some ideas on how encryption is really done, though props on the start/interest and this can certainly be a fun way to create some sort of cryptogram puzzle or something along those lines. I won't pretend to be any kind of expert, but I can at least point you in the write direction. (https://en.wikipedia.org/wiki/Data_Encryption_Standard https://en.wikipedia.org/wiki/Symmetric-key_algorithm)
Consider having your code read in a .txt file and print out to a .txt file, rather than using user input for the message.
Again, I'm not an expert by any means and there are definitely some fun uses of the kind of program you're aiming for, just trying to point you in the right direction if this is something that you're interested in. Hope all of that is helpful!
Here is my solution. It uses a randomizer to encrypt the file by assigning a ASCII value to the plain text and randomly shifts it around.
from random import randint
import sys
def menu():
input1=int(input(""" please select what you want to do:
1.Encrypt
2.Decrypt
3.Extended Encryption
4.exit
"""))#menu to choose what you want to do
if input1==1:
encrypt() #takes you to the encrypt function
elif input1==2:
decrypt()#takes you to the decrypt function
elif input1==3:
enxtended()#takes you to the extended encryption function
elif input1==4:
sys.exit #exits the program
else:
print("invalid entry try again")
menu()
def encrypt():
file_name=str(input("please enter the name of the file that you want to open\n"))
try:
text_file=open(file_name + ".txt","r")#puts the text file into read
text_file=text_file.read()#reads the text file
print(text_file)#prints the strings in the document
except:
print("error try again")
encrypt()
random(text_file)
def random(text_file):
list1=("")#creates blank string
for x in range (0,8):
num=(randint(33,126))#generates a random number between33 and 126
ascii1=chr(num) #converts it into an ascii character
list1=list1+ascii1#adds the ascii character to the blank string list1
print (f"your 8 key code is {list1}") #prints 8 character code
offset(list1,text_file)
def offset(list1,text_file):
total=0
for x in range (8,):
total=total+ord(list1[x]) #turns each character into an ascii value
total=total/8 #divides it by
total=round(total,0)#rounds it to 0 decimel places
print(total)
total=total-32#minuses 32 from total
print(f"your offset factor is {total}")
encrypting(total,text_file)
def encrypting(total,text_file):
length=len(text_file)
string1=("")
for x in range (length,):
numascii=ord(text_file[x])#turns the characters into its ascii value
numascii=int(numascii)#makes sure they are integers
if numascii==32:
letter=chr(32)#converts spaces back into spaces
string1=string1+letter#adds space to thestring
else:
numascii1=numascii+total#adds the character value to the offset factor
numascii1=int(numascii1)#makes sure it is an integer
if numascii1>126:# if the ascii value is great then 126
numascii1=numascii1-94#minus 94 from it
letter=chr(numascii1)#turn it into a character
string1=string1+letter#add it to the string
else:
letter=chr(numascii1)#turn the ascii value into a character
string1=string1+letter#add it to the string
print(f"your encrypted file is {string1}")
savefile(string1)
menu()
I have written separate programs for encryption and decryption. Both of these use file manipulation techniques. Use the username 'eggs' and password 'chicks' so that not anyone can see my secret code. I have used hashlib for more security. Just change the User 'Soumajit' to your respective Username to make it work. The first one is encryption and the next one is for decryption.
#ENCRYPTION
from time import sleep
import subprocess
import hashlib
def copy2clip(txt):
cmd='echo '+txt.strip()+'|clip'
return subprocess.check_call(cmd, shell=True)
def en():
alphabet = "abcdefghijklmnopqsrtuwvxyzABCDEFGHIJKLMNOPQSRTUVWXYZ,./?:;!##$%_&* ()`-+=1234567890"
encrypt = ""
decrypt = ""
print
print "Type y for yes and anything else for no"
start = raw_input("Do you want to import file from desktop? ")
if start == "y":
Open = raw_input("Enter the .txt file you want to open in desktop: ")
a = open("C://Users//Soumajit//Desktop//" + Open + ".txt", "r")
print
x = (a.read())
copy2clip(x)
a.close()
print "Right click and select paste below to encrypt"
print
message = raw_input()
for i in message:
x = alphabet.find(i)
new = (x - 5) % 74
encrypt += alphabet[new]
e2 = encrypt[::-1]
else:
print "Type your message below"
message = raw_input("")
for i in message:
x = alphabet.find(i)
new = (x - 5) % 74
encrypt += alphabet[new]
e2 = encrypt[::-1]
print
a = raw_input("By what name do you want to save it?: ")
file = open(a + ".txt", 'wb')
file.write(e2)
file.close()
copy = raw_input("Do you want to copy your file? ")
if copy == 'y':
copy2clip(e2)
print 'Your encrypted file has been copied to the clipboard'
else:
print "Your encrypted file has been saved with the name " + str(a) + " in desktop"
print "To decrypt it, use my other program"
sleep(3)
u = 'e415bf03b4d860dccba57cea46371f831d772ba1deca47f28fa7d1f7'
p = 'c35f7f79dc34a678beb2b4106c84c9963561e7c64bc170e50c429b9a'
ur = raw_input('Enter your username: ')
ur1 = hashlib.sha224(ur).hexdigest()
pr = raw_input('Enter your password: ')
pr1 = hashlib.sha224(pr).hexdigest()
if ur1 == u and pr1 == p:
print 'Access granted'
sleep(1)
en()
else:
print "Incorrect username or password"
sleep(1)
#DECRYPTION
from time import sleep
import subprocess
import hashlib
def copy2clip(txt):
cmd='echo '+txt.strip()+'|clip'
return subprocess.check_call(cmd, shell=True)
def de():
print "Type y for yes and anything else for no"
start = raw_input("Do you want to import file from desktop? ")
if start == "y":
Open = raw_input("Enter the .txt file you want to open from folder: ")
a = open("C://Users//Soumajit//Desktop//" + Open + ".txt", "r")
x = (a.read())
#print x
copy2clip(x)
print "Right click and select paste below to decrypt"
print
message = raw_input()
a.close()
alphabet = "abcdefghijklmnopqsrtuwvxyzABCDEFGHIJKLMNOPQSRTUVWXYZ,./?:;!##$%_&*()`-+=1234567890"
decrypt = ''
for i in message:
x = alphabet.find(i)
new = (x + 5) % 74
decrypt += alphabet[new]
d2 = decrypt[::-1]
d3 = d2.replace("`", " ")
final = d3.replace("2", " ")
print
print final
else:
print "Type or paste your encrypted text below"
print
message = raw_input()
alphabet = "abcdefghijklmnopqsrtuwvxyzABCDEFGHIJKLMNOPQSRTUVWXYZ,./?:;!##$%_&*()`-+=1234567890"
decrypt = ''
for i in message:
x = alphabet.find(i)
new = (x + 5) % 74
decrypt += alphabet[new]
d2 = decrypt[::-1]
d3 = d2.replace("`", " ")
final = d3.replace("2", " ")
print
print final
u = 'e415bf03b4d860dccba57cea46371f831d772ba1deca47f28fa7d1f7'
p = 'c35f7f79dc34a678beb2b4106c84c9963561e7c64bc170e50c429b9a'
ur = raw_input('Enter your username: ')
ur1 = hashlib.sha224(ur).hexdigest()
pr = raw_input('Enter your password: ')
pr1 = hashlib.sha224(pr).hexdigest()
if ur1 == u and pr1 == p:
print 'Access granted'
sleep(1)
de()
print
end = raw_input('press q to quit: ')
while end != 'q':
print 'You did not type q'
end = raw_input('press q to quit: ')
if end == 'q':
quit()
else:
print 'Incorrect username or password'
sleep(1)
quit()
mainmenu = input("Welcome to my ISBN calculator, please select an option\n\
1. Load ISBN Calculator\n\
2. Exit Program\n\
")
(mainmenu)
if mainmenu == ("2"):
print ("The ISBN Calculator will now close, thank you for using!")
time.sleep(1.5)
exit()
elif mainmenu == ("1"):
ISBN = input(" Please enter the 10 digit number exactly\n\
")
Digit1 = int(ISBN[0])*11
Digit2 = int(ISBN[1])*10
Digit3 = int(ISBN[2])*9
Digit4 = int(ISBN[3])*8
Digit5 = int(ISBN[4])*7
Digit6 = int(ISBN[5])*6
Digit7 = int(ISBN[6])*5
Digit8 = int(ISBN[7])*4
Digit9 = int(ISBN[8])*3
Digit10 = int(ISBN[9])*2
sum=(Digit1+Digit2+Digit3+Digit4+Digit5+Digit6+Digit7+Digit8+Digit9+Digit10)
num=sum%11
Digit11=11-num
if Digit11==10:
Digit11='X'
ISBNNumber=str(ISBN)+str(Digit11)
print('The ISBN number is --> ' + ISBNNumber)
This is my code and it always comes up with the error of Digit 1 is not defined whenever I try enter my 10 digit number, any help?
Why the line:
(mainmenu)
?
In your if statements remove the ():
if mainmenu == "1":
...
elif mainmenu == "2":
...
else:
print("Invalid menu option")
exit()
It will be work. Indent is important.
mainmenu = input("Welcome to my ISBN calculator, please select an option\n\
1. Load ISBN Calculator\n\
2. Exit Program\n\
")
if mainmenu == "2":
print ("The ISBN Calculator will now close, thank you for using!")
time.sleep(1.5)
exit()
elif mainmenu == "1":
ISBN = input(" Please enter the 10 digit number exactly\n")
Digit1 = int(ISBN[0])*11
Digit2 = int(ISBN[1])*10
Digit3 = int(ISBN[2])*9
Digit4 = int(ISBN[3])*8
Digit5 = int(ISBN[4])*7
Digit6 = int(ISBN[5])*6
Digit7 = int(ISBN[6])*5
Digit8 = int(ISBN[7])*4
Digit9 = int(ISBN[8])*3
Digit10 = int(ISBN[9])*2
sum=(Digit1+Digit2+Digit3+Digit4+Digit5+Digit6+Digit7+Digit8+Digit9+Digit10)
num=sum%11
Digit11=11-num
if Digit11==10:
Digit11='X'
ISBNNumber=str(ISBN)+str(Digit11)
print('The ISBN number is --> ' + ISBNNumber)
Note. This code is just work code, not a good implementation.
The problem occurs when you execute that code with Python 2. Use Python 3 instead.
In Python 2, input evaluates the input you provide, so if you enter 1, then mainmenu is 1 (the number) and not "1" (the string), thus both of your if-checks fail and your code arrives at the sum=... part without any ISBN number being inputted.
As commented above, your "ISBN" is quite different from the standard, which has either 10 or 13 digits including check digit.
A clean implementation for ISBN-10 calculation would be:
from string import digits
checkTemplate = digits + "X"
def isbn(isbnBody):
"""append check digit to a isbn given as string without check digit"""
assert len(isbnBody) == 9
s = sum([int(isbnChar)*multiplier for isbnChar, multiplier in zip(isbnBody, range(1,10))])
checkDigit = checkTemplate[s % 11]
return isbnBody + checkDigit