Skip to content

Commit 1620686

Browse files
authored
Add files via upload
1 parent 827ba94 commit 1620686

File tree

2 files changed

+113
-105
lines changed

2 files changed

+113
-105
lines changed

passwdmanager.exe

-38.7 KB
Binary file not shown.

passwdmanager.py

Lines changed: 113 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mysql.connector
2+
import getpass
23
import urllib
34
import sqlite3
45
import functools
@@ -14,50 +15,85 @@
1415
from cryptography.hazmat.primitives import hashes
1516
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
1617

17-
#connect to database
18-
mydb = mysql.connector.connect(
19-
host="localhost",
20-
user="root"
21-
)
22-
23-
#set cursor
24-
mycursor = mydb.cursor(buffered=True)
25-
d = mydb.cursor(buffered=True)
26-
i = mydb.cursor(buffered=True)
27-
28-
#detect and create database
29-
mycursor.execute('CREATE DATABASE IF NOT EXISTS db_password')
30-
mycursor.execute('CREATE TABLE IF NOT EXISTS db_password.tb_nap (id INT NOT NULL AUTO_INCREMENT,name VARCHAR(255) NOT NULL,password VARCHAR(255) NOT NULL,PRIMARY KEY (id))')
31-
32-
#interfaces
33-
print("\n\nWelcome to password manager python! what you want to do?(v to view all your password,i to insert,d to delete")
34-
cmd = input(">")
35-
36-
#view query
37-
if cmd == 'v' or cmd == 'V':
38-
mycursor.execute("SELECT id, name FROM db_password.tb_nap") #select id,name from database
39-
myresult = mycursor.fetchall()
40-
41-
if len(myresult)==0: #detect blank input
42-
print("Notthing here\n")
43-
else:
44-
print("What you wanna see?")
45-
for x in myresult :
46-
print(x)
47-
48-
icmd = input("Enter ID:")
49-
if icmd=='':
50-
print("\033[1;31;40m Error id. \033[1;37;40m\n")
18+
#enter user and password for database and master password
19+
user = input("Enter database username: ")
20+
passwd = getpass.getpass("Enter database password: ")
21+
k = getpass.getpass("Enter master password: ")
22+
23+
if user=='' or passwd=='' or k=='': #detect blank input
24+
print("\033[1;31;40m Error username or password.Plese try again\033[1;37;40m\n")
25+
else:
26+
#connect to database
27+
mydb = mysql.connector.connect(
28+
host='localhost', #or your hostname/ip-address
29+
user=(user),
30+
password=(passwd)
31+
)
32+
33+
#set cursor
34+
mycursor = mydb.cursor(buffered=True)
35+
d = mydb.cursor(buffered=True)
36+
i = mydb.cursor(buffered=True)
37+
38+
#detect and create database
39+
mycursor.execute('CREATE DATABASE IF NOT EXISTS db_password')
40+
mycursor.execute('CREATE TABLE IF NOT EXISTS db_password.tb_nap (id INT NOT NULL AUTO_INCREMENT,name VARCHAR(255) NOT NULL,password VARCHAR(255) NOT NULL,PRIMARY KEY (id))')
41+
42+
#interfaces
43+
print("\n\nWelcome to password manager python! what you want to do?(v to view all your password,i to insert,d to delete")
44+
cmd = input(">")
45+
46+
#view query
47+
if cmd == 'v' or cmd == 'V':
48+
mycursor.execute("SELECT id, name FROM db_password.tb_nap") #select id,name from database
49+
myresult = mycursor.fetchall()
50+
51+
if len(myresult)==0: #detect blank input
52+
print("Nothing here\n")
53+
else:
54+
print("What you wanna see?")
55+
for x in myresult :
56+
print(x)
57+
58+
icmd = input("Enter ID:")
59+
if icmd=='':
60+
print("\033[1;31;40m Error id. \033[1;37;40m\n")
61+
else:
62+
d.execute("SELECT id,name FROM db_password.tb_nap WHERE id= %s",(icmd,)) #select id,name from id input
63+
i.execute("SELECT password FROM db_password.tb_nap WHERE id= %s",(icmd,)) #select password from id input
64+
p = d.fetchall()
65+
i = i.fetchall()
66+
password = " , ".join( map(str, i) ) #transition list to string
67+
68+
k_encode = k.encode() #encode key to byte
69+
p_encode = password.encode() #encode password to byte
70+
salt = b'`R\xf7\xc0\xf3+@\xdd~\xa4K1Ty\x83\x9a'
71+
kdf = PBKDF2HMAC(
72+
algorithm=hashes.SHA256(),
73+
length=32,
74+
salt=salt,
75+
iterations=100000,
76+
backend=default_backend()
77+
)
78+
key = base64.urlsafe_b64encode(kdf.derive(k_encode))
79+
f = Fernet(key) #ready to decrypt
80+
decrypted = decrypted = f.decrypt(p_encode) #decrypted
81+
ogpasswd = decrypted.decode() #decode from byte to string
82+
83+
print("Password for",p,"is",ogpasswd,"\n\n") #show id,name,password
84+
85+
86+
#insert
87+
elif cmd == 'i' or cmd == 'I':
88+
print("Insert name and password")
89+
n = input("name>")
90+
p = getpass.getpass("password>")
91+
92+
if n=='' or p=='': #detect blank input
93+
print("\033[1;31;40m Can't insert into database.Plese input all of data. \033[1;37;40m\n")
5194
else:
52-
d.execute("SELECT id,name FROM db_password.tb_nap WHERE id= %s",(icmd,)) #select id,name from id input
53-
i.execute("SELECT password FROM db_password.tb_nap WHERE id= %s",(icmd,)) #select password from id input
54-
p = d.fetchall()
55-
i = i.fetchall()
56-
password = " , ".join( map(str, i) ) #transition list to string
57-
58-
k = ("key") #set key
5995
k_encode = k.encode() #encode key to byte
60-
p_encode = password.encode() #encode password to byte
96+
p_encode = p.encode() #encode password input to byte
6197
salt = b'`R\xf7\xc0\xf3+@\xdd~\xa4K1Ty\x83\x9a'
6298
kdf = PBKDF2HMAC(
6399
algorithm=hashes.SHA256(),
@@ -67,66 +103,38 @@
67103
backend=default_backend()
68104
)
69105
key = base64.urlsafe_b64encode(kdf.derive(k_encode))
70-
f = Fernet(key) #ready to decrypt
71-
decrypted = decrypted = f.decrypt(p_encode) #decrypted
72-
ogpasswd = decrypted.decode() #decode from byte to string
73-
74-
print("Password for",p,"is",ogpasswd,"\n\n") #show id,name,password
75-
76-
77-
#insert
78-
elif cmd == 'i' or cmd == 'I':
79-
print("Insert name and password")
80-
n = input("name>")
81-
p = input("password>")
82-
83-
if n=='' or p=='': #detect blank input
84-
print("\033[1;31;40m Can't insert into database.Plese input all of data. \033[1;37;40m\n")
85-
else:
86-
k = ("key") #set key
87-
k_encode = k.encode() #encode key to byte
88-
p_encode = p.encode() #encode password input to byte
89-
salt = b'`R\xf7\xc0\xf3+@\xdd~\xa4K1Ty\x83\x9a'
90-
kdf = PBKDF2HMAC(
91-
algorithm=hashes.SHA256(),
92-
length=32,
93-
salt=salt,
94-
iterations=100000,
95-
backend=default_backend()
96-
)
97-
key = base64.urlsafe_b64encode(kdf.derive(k_encode))
98-
99-
f = Fernet(key) #ready to encrypt
100-
encrypted = f.encrypt(p_encode) #encrpyted
101-
102-
sql = "INSERT INTO db_password.tb_nap (name, password) VALUES (%s, %s)" #insert to table query
103-
val = (n, encrypted)
104-
mycursor.execute(sql, val)
105-
mydb.commit() #confirm operation to database
106-
107-
print(mycursor.rowcount, "password inserted\n\n") #show number(s) of query that have inserted
108-
109-
#delete
110-
elif cmd == 'd' or cmd == 'D':
111-
mycursor.execute("SELECT id,name FROM db_password.tb_nap") #select id,name from db
112-
myresult = mycursor.fetchall()
113-
for x in myresult: #show id,name query in database
114-
print("What you want to delete?")
115-
print(x)
116-
117-
i = input("Enter id:") #enter query id
118-
119-
if i=='': #detect blank input
120-
print("\033[1;31;40m Error id. \033[1;37;40m")
121-
else:
122-
sql = "DELETE FROM db_password.tb_nap WHERE id = %s" #delete from query id
123-
mycursor.execute(sql, (i,))
124-
mydb.commit() #confirm operation to database
125-
126-
print(mycursor.rowcount, "name and password deleted\n\n") #show number(s) of query that have deleted
127-
128-
#error
129-
else :
130-
print("\033[1;31;40m Error,Can't define command...plese try again \033[1;37;40m\n")
131-
132-
os.system('pause')
106+
107+
f = Fernet(key) #ready to encrypt
108+
encrypted = f.encrypt(p_encode) #encrpyted
109+
110+
sql = "INSERT INTO db_password.tb_nap (name, password) VALUES (%s, %s)" #insert to table query
111+
val = (n, encrypted)
112+
mycursor.execute(sql, val)
113+
mydb.commit() #confirm operation to database
114+
115+
print(mycursor.rowcount, "password inserted\n\n") #show number(s) of query that have inserted
116+
117+
#delete
118+
elif cmd == 'd' or cmd == 'D':
119+
mycursor.execute("SELECT id,name FROM db_password.tb_nap") #select id,name from db
120+
myresult = mycursor.fetchall()
121+
for x in myresult: #show id,name query in database
122+
print("What you want to delete?")
123+
print(x)
124+
125+
i = input("Enter id:") #enter query id
126+
127+
if i=='': #detect blank input
128+
print("\033[1;31;40m Error id. \033[1;37;40m")
129+
else:
130+
sql = "DELETE FROM db_password.tb_nap WHERE id = %s" #delete from query id
131+
mycursor.execute(sql, (i,))
132+
mydb.commit() #confirm operation to database
133+
134+
print(mycursor.rowcount, "name and password deleted\n\n") #show number(s) of query that have deleted
135+
136+
#error
137+
else :
138+
print("\033[1;31;40m Error,Can't define command...plese try again \033[1;37;40m\n")
139+
140+
os.system('pause')

0 commit comments

Comments
 (0)