Title Description:
(1) Simulate the login screen, discriminate the user name and password, give appropriate prompts, if more than three times, lock the input. Use instead of password; or the latest input is displayed and the previous becomes; security measures.
(2) Also add the enhanced verification (BOE) as shown below.
(3) The vacant position appears again when the scroll bar is touched.
Expansion:
- Add a registration page for users to sign up new users
- Adding digital authentication codes to differentiate between humans and machines
Solutions / Algorithm Analysis / Problems and Solutions
Slider validation is to bind the movement of the slider and the movement of the picture block, trigger the corresponding check function when the slider is released, in order to have a certain degree of fault tolerance, set the position of the slider and the position of the set within 3 pixels is considered to be a successful validation.
- The positions were randomly generated each time, while one of the 14 pre-prepared images was then selected as the background image.
- The image nugget and the background image are placed in two different QLabel containers.
- Each time you refresh the corresponding position of the background image will be keyed out and placed in the picture block, and the corresponding part of the background image will be set to white to show the difference.
- Expansion:
- The New Registration Page section is where you click a button to bring up the registration page dialog box, where you can enter new user information and save it to the user information dictionary, and then use the newly registered user to log in.
- The numeric CAPTCHA consists of 4 digits 0-9 and letters a-z and A-Z. 4 characters are randomly selected from the feasible character set to form the 4-digit CAPTCHA, and then the CAPTCHA is automatically generated to be presented to the user in the form of an image.
Experimental code
Log in to the main screen:
Login Authentication:
def check_login_func(self): if not self.validate_line.text(): (self, "Warning.", "The verification code has not been entered.") return validate_slide = Validate_page() r = validate_slide.exec_() if not r: return if () == self.validate_line.text().upper(): if USER_PWD.get(self.user_line.text()) == self.pwd_line.text(): (self, 'Information', 'Log in Successfully!') = 0 () # () # () else: (self, 'Wrong', 'Wrong Username or Password!') = + 1 if == 3: (self, "wrong", "The number of wrong passwords has reached three!") () self.user_line.clear() self.pwd_line.clear() self.validate_line.clear() else: (self, "wrong", "Captcha entered incorrectly!") () self.validate_line.clear()
Automatically generate CAPTCHA images:
def get_chars(): """Generate a string of the given length, returning a list format""" return (chars, length) def create_lines(): """Plotting interference lines""" line_num = (*n_line) # of interference lines for i in range(line_num): # Starting point begin = ((0, size[0]), (0, size[1])) # End point end = ((0, size[0]), (0, size[1])) ([begin, end], fill=(0, 0, 0)) def create_points(): """Mapping of interference points""" chance = min(100, max(0, int(point_chance))) # Size limited to [0, 100] for w in range(width): for h in range(height): tmp = (0, 100) if tmp > 100 - chance: ((w, h), fill=(0, 0, 0)) def create_strs(): """Drawing CAPTCHA characters.""" c_chars = get_chars() strs = ' %s ' % ' '.join(c_chars) # Separate each character with a space font = (font_type, font_size) font_width, font_height = (strs) (((width - font_width) / 3, (height - font_height) / 3), strs, font=font, fill=fg_color) return ''.join(c_chars) if draw_lines: create_lines() if draw_points: create_points() strs = create_strs() # Graphics distortion parameters params = [1 - float((1, 2)) / 100, 0, 0, 0, 1 - float((1, 10)) / 100, float((1, 2)) / 500, 0.001, float((1, 2)) / 500 ] img = (size, , params) # Create distortions img = (ImageFilter.EDGE_ENHANCE_MORE) # Filter, boundary enhancement (larger threshold) mstream = () (mstream, img_type) if save_img: ("", img_type) return mstream, strs
Registration interface section:
Page initialization:
def __init__(self): super(SigninPage, self).__init__() self.signin_user_label = QLabel('Username:', self) self.signin_pwd_label = QLabel('Password:', self) self.signin_pwd2_label = QLabel('Password:', self) self.signin_user_line = QLineEdit(self) self.signin_pwd_line = QLineEdit(self) self.signin_pwd2_line = QLineEdit(self) self.signin_button = QPushButton('Sign in', self) self.user_h_layout = QHBoxLayout() self.pwd_h_layout = QHBoxLayout() self.pwd2_h_layout = QHBoxLayout() self.all_v_layout = QVBoxLayout() self.lineedit_init() self.pushbutton_init() self.layout_init() def layout_init(self): self.user_h_layout.addWidget(self.signin_user_label) self.user_h_layout.addWidget(self.signin_user_line) self.pwd_h_layout.addWidget(self.signin_pwd_label) self.pwd_h_layout.addWidget(self.signin_pwd_line) self.pwd2_h_layout.addWidget(self.signin_pwd2_label) self.pwd2_h_layout.addWidget(self.signin_pwd2_line) self.all_v_layout.addLayout(self.user_h_layout) self.all_v_layout.addLayout(self.pwd_h_layout) self.all_v_layout.addLayout(self.pwd2_h_layout) self.all_v_layout.addWidget(self.signin_button) (self.all_v_layout) def lineedit_init(self): self.signin_user_line.setPlaceholderText("username") self.signin_pwd_line.setPlaceholderText("password") self.signin_pwd2_line.setPlaceholderText("password again") self.signin_pwd_line.setEchoMode() self.signin_pwd2_line.setEchoMode() self.signin_user_line.(self.check_input_func) self.signin_pwd_line.(self.check_input_func) self.signin_pwd2_line.(self.check_input_func)
Registration inspection section:
def check_signin_func(self): if self.signin_pwd_line.text() != self.signin_pwd2_line.text(): (self, 'Wrong', 'Two Passwords Typed Are Not Same!') elif self.signin_user_line.text() not in USER_PWD: USER_PWD[self.signin_user_line.text()] = self.signin_pwd_line.text() (self, 'Information', 'Register Successfully') () else: (self, 'Wrong', 'This Username Has Been Registered!') self.signin_user_line.clear() self.signin_pwd_line.clear() self.signin_pwd2_line.clear()
Slider validation section:
class Validate_page(QDialog): def __init__(self): super(Validate_page, self).__init__() (600, 500) ("Drag the slider to verify.") () self.Widget_init() () # () def initUI(self): # Initialize the location of the relevant control self.lb_bg = QLabel(self) self.lb_sl = QLabel(self) self.lb_bg.setStyleSheet("border: 2px solid red") # self.lb_sl.setStyleSheet("border: 2px solid blue") self.lb_bg.setGeometry(100, 100, 400, 200) self.lb_wz = QLabel(self) # self.lb_wz.setStyleSheet("border: 2px solid green") () = QPushButton("Cancel.", self) (400, 430, 100, 50) = QSlider(, self) (100, 350, 400, 50) def Widget_init(self): # Initialize signal connections to related components (0) (360) () () () def slide(self, value): self.lb_sl.move(100 + value, self.answer_y) # Bind the slider to the image nuggets # print(value) def refresh(self): import random # Randomly generated validation regions self.answer_x = (100, 460) self.answer_y = (100, 260) pic = (0, 14) self.lb_sl.setGeometry(100, self.answer_y, 40, 40) self.lb_wz.setGeometry(self.answer_x, self.answer_y, 40, 40) img = (f"pic/{pic}.jpeg") # Read the background image img = (img, (400, 200)) x = self.answer_x - 100 y = self.answer_y - 100 img1 = (img[y:y + 40, x:x + 40]) # Snap a 40*40 picture cube img[y:y + 40, x:x + 40] = 255 # White out the docked area img = (img, cv2.COLOR_BGR2RGB) img1 = (img1, cv2.COLOR_BGR2RGB) qt_img = QImage(, 400, 200, QImage.Format_RGB888) qt_img1 = QImage(, 40, 40, QImage.Format_RGB888) self.lb_bg.setPixmap((qt_img)) self.lb_sl.setPixmap((qt_img1)) def check(self): # Verify that the sliding validation was successful # Get the coordinates of the set area x = self.lb_sl.pos().x() y = self.lb_sl.pos().y() print(f"({x},{y})") if abs(x - self.answer_x) <= 3: # Fault tolerance of 3px (self, "Congratulations.", "Verification successful.") (1) else: (self, "Error.", "Authentication failed.") (0) () def cancel(self): (0) # Click to cancel slide verification
running result
All background images:
Registration screen:
Full Code
to this article on python password authentication (slider authentication) of the article is introduced to this, more related python password authentication (slider authentication) content, please search my previous posts or continue to browse the following related articles I hope that you will support me in the future more!