Semantic Error in Python

1

It happens that to make my program safer, I ask for a password at the beginning of the program, and this password is also customizable (using the ConfigParser library), to make it even more secure, the config.ini file is encrypted using the Pydes library , from this, the enter method is used, which first thing is to obtain the password that the user entered, the second is to decrypt the file config.ini to be able to read it, the password section is obtained, and then compare what that the user introduced with what is in the password section of config.ini, if the password is incorrect, a message is displayed, if it is correct, the program is accessed, however, when testing it, it happens that, despite typing the password correctly, I keep pulling that the password is incorrect

    #Python 3.5
#Office Database v2.0
#Etapa: Desarrollo
#Desarrollador: Cesar Eduardo Cuevas Garza

from tkinter import *
from pyDes import des
from tkinter import messagebox
import configparser
import time
#from dependencies import delete as d
global contraseña_encriptado



def encriptar(contraseña, fichero):
    f = open(fichero, "rb+")
    d = f.read()
    f.close()
    k = des(contraseña)
    d = k.encrypt(d, " ")
    f = open(fichero, "wb+")
    f.write(d)
    f.close()
    return True

def desencriptar(contraseña, fichero):
    f = open(fichero, "rb+")
    d = f.read()
    f.close()
    k = des(contraseña)
    d = k.decrypt(d, " ")
    f = open(fichero, "wb+")
    f.write(d)
    f.close()
    return True

try:
    config = configparser.ConfigParser()
    config.read("config.ini")
    config.get("Software", "contraseña")
    encriptar("#[email protected]", "config.ini")
except:
    desencriptar("#[email protected]", "config.ini")
    encriptar("#[email protected]", "config.ini")

def main():
    ventana_main = Tk()
    ventana.destroy()
    ventana_main.geometry("800x800+400+100")
    opcion = StringVar(ventana_main)
    bienvenido_label = Label(ventana_main, text = "Bienvenido, Selecciona Una Opcion:", font = ("Helvetica", 24)).place(x = 150, y = 40)
    opciones_spinbox = Spinbox(ventana_main, values = ("Añadir un usuario", "Hola"), textvariable = opcion, width = 60, font = ("Helvetica", 12, "bold"), state = "readonly").place(x = 150, y = 120)

def entrar(x):
    desencriptar("#[email protected]", "config.ini")
    config = configparser.ConfigParser()
    config.read("config.ini")
    final_pass = config.get("Software", "contraseña")
    if str(x) == str(final_pass):
        encriptar("#[email protected]", "config.ini")
        main()
    else:
        encriptar("#[email protected]", "config.ini")
        messagebox.showerror("Error", "Contraseña Incorrecta")

ventana = Tk()
ventana.title("Bienvenido")
ventana.geometry("300x300+600+250")
ventana.resizable(width=False, height=False)
fondo = "#2F4F4F"
letras = "#FFF"
ventana.configure(background = fondo)


try:
    archivo = open("datos.csv")
    config = open("config.ini")
except:
    messagebox.showwarning("Error", "Falta algun archivo!")
    x = messagebox.askquestion("Setup", "¿Crear Archivos?")
    if x == "yes":
        archivo = open("datos.csv", "a")
        config = open("config.ini", "a")
        archivo.close()
        config.close()
        archivo = open("datos.csv")
        config = open("config.ini")
        longitud = 0
        for z in archivo:
            longitud = longitud + len(z)
        archivo.close()
        if longitud==0:
            archivo = open("datos.csv", "a")
            archivo.write("ID,Nombre,Apellidos,Equipo de Trabajo,Puntos Avante\n")
            archivo.close()
        longitud = 0
        for z in config:
            longitud = longitud + len(z)
        if longitud == 0:
            config = open("config.ini", "a")
            config.write("[Software]\nusuario = Root\ncontraseña = password")
            config.close()
        Config = configparser.ConfigParser()
        Config.read("config.ini")
        messagebox.showinfo("Setup", "Archivos Creados Exitosamente")
        encriptar("#[email protected]" , "config.ini")
        encriptar("#[email protected]", "datos.csv")
    else:
        messagebox.showwarning("Cerrando Programa", "Se necesitan los archivos para avanzar")
        exit()



contraseña_entrar = StringVar()
pass_etiqueta = Label(ventana , text = "¡Introduce La Contraseña!", bg = fondo, fg = letras, font= ("Helvetica", 16)).place(x = 30, y = 10)
pass_entry = Entry(ventana, textvariable = contraseña_entrar).place(x = 60, y = 60)
pass_boton = Button(ventana, text= "Entrar!", command = lambda: entrar(contraseña_entrar)).place(x = 60, y = 90)


ventana.mainloop()
    
asked by Cesar Cuevas 05.12.2016 в 22:01
source

1 answer

2

The problem is that StringVar is a container and you should use the method get() to read the data contained in it. If the name of the instance is used what is returned is the content of the attribute 'for internal use' _name and not the stored value:

For example:

import tkinter as tk

root = tk.Tk()
var = tk.StringVar()
var.set('Hola Mundo')


print(var)
print(var._name)
print(root.globalgetvar('PY_VAR0'))
print(var.get())

Return us:

PY_VAR0
PY_VAR0
Hola Mundo
Hola Mundo

As we can see PY_VAR0 is only the default internal name assigned to the instance, not the value it stores. This leads to confusion really. In fact it can be changed using the parameter name of the constructor:

var = tk.StringVar(name = 'MiVariable')

The complete constructor is:

def __init__(self, master=None, value=None, name=None)

In short, change:

pass_boton = Button(ventana, text= "Entrar!", command = lambda: entrar(contraseña_entrar)).place(x = 60, y = 90)

by:

pass_boton = Button(ventana, text= "Entrar!", command = lambda: entrar(contraseña_entrar.get())).place(x = 60, y = 90)
    
answered by 05.12.2016 в 22:44