首頁 > 軟體

python 密碼驗證(滾軸驗證)

2022-03-01 10:00:14

題目描述:

(1)模擬登陸介面,判別使用者名稱和密碼,給出合適的提示,如果超過三次,鎖定輸入。用代替密碼;或者最新輸入顯示,前面的變成;安全性措施。
(2)同時新增如下圖的加強驗證(京東)。
(3)在觸動卷軸時再出現空缺位置。

拓展:

  • 增加註冊頁面,可供使用者註冊新使用者
  • 增加數位驗證碼區別人機

解題思路/演演算法分析/問題及解決

滾軸驗證就是將滾軸的移動和圖片小塊的移動相繫結,在滾軸鬆開時觸發相對應的檢查函數,為了有一定的容錯率,設定滾軸的位置與設定的位置相差3個畫素以內即算驗證成功。

  • 每次的位置隨機生成,同時隨即從14張事先準備好的圖片中選取一張作為背景圖。
  • 圖片小塊和背景圖片分別放在兩個不同的QLabel容器中。
  • 每次重新整理時將背景圖片的相應位置摳出放於圖片小塊中,並將背景圖片的相應部分置為白色以示區別。
  • 拓展:
  • 新增註冊頁部分就是點選按鈕彈出註冊頁的對話方塊,可在裡面輸入新使用者資訊並儲存到使用者資訊字典中,隨後可用新註冊的使用者登入。
  • 數位驗證碼由4位元數位0-9和字母a-z及A-Z組成,隨機在可行字元集中選取4個字元組成四位驗證碼,然後自動生成驗證碼以圖片的方式呈現給使用者。

實驗程式碼

登入主介面:
登入驗證:

def check_login_func(self):
        if not self.validate_line.text():
            QMessageBox.warning(self, "警告", "還未輸入驗證碼")
            return
        validate_slide = Validate_page()
        r = validate_slide.exec_()
        if not r:
            return
        if self.validate.upper() == self.validate_line.text().upper():
            if USER_PWD.get(self.user_line.text()) == self.pwd_line.text():
                QMessageBox.information(self, 'Information', 'Log in Successfully!')
                self.nums = 0
                self.refresh()
                # self.close()
                # self.STU.exec()
            else:
                QMessageBox.critical(self, 'Wrong', 'Wrong Username or Password!')
                self.nums = self.nums + 1
                if self.nums == 3:
                    QMessageBox.critical(self, "wrong", "密碼輸錯次數已達三次!")
                    self.close()

            self.user_line.clear()
            self.pwd_line.clear()
            self.validate_line.clear()
        else:
            QMessageBox.critical(self, "wrong", "驗證碼輸入有誤!")
            self.refresh()
            self.validate_line.clear()

自動生成驗證碼圖片:

def get_chars():
        """生成給定長度的字串,返回列表格式"""

        return random.sample(chars, length)

    def create_lines():
        """繪製干擾線"""

        line_num = random.randint(*n_line)  # 干擾線條數

        for i in range(line_num):
            # 起始點
            begin = (random.randint(0, size[0]), random.randint(0, size[1]))
            # 結束點
            end = (random.randint(0, size[0]), random.randint(0, size[1]))
            draw.line([begin, end], fill=(0, 0, 0))

    def create_points():
        """繪製干擾點"""

        chance = min(100, max(0, int(point_chance)))  # 大小限制在[0, 100]

        for w in range(width):
            for h in range(height):
                tmp = random.randint(0, 100)
                if tmp > 100 - chance:
                    draw.point((w, h), fill=(0, 0, 0))

    def create_strs():
        """繪製驗證碼字元"""

        c_chars = get_chars()
        strs = ' %s ' % ' '.join(c_chars)  # 每個字元前後以空格隔開

        font = ImageFont.truetype(font_type, font_size)
        font_width, font_height = font.getsize(strs)

        draw.text(((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()

    # 圖形扭曲引數
    params = [1 - float(random.randint(1, 2)) / 100,
              0,
              0,
              0,
              1 - float(random.randint(1, 10)) / 100,
              float(random.randint(1, 2)) / 500,
              0.001,
              float(random.randint(1, 2)) / 500
              ]
    img = img.transform(size, Image.PERSPECTIVE, params)  # 建立扭曲

    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)  # 濾鏡,邊界加強(閾值更大)

    mstream = io.BytesIO()
    img.save(mstream, img_type)

    if save_img:
        img.save("validate.jpg", img_type)

    return mstream, strs

註冊介面部分:
頁面初始化:

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.setLayout(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(QLineEdit.Password)
        self.signin_pwd2_line.setEchoMode(QLineEdit.Password)

        self.signin_user_line.textChanged.connect(self.check_input_func)
        self.signin_pwd_line.textChanged.connect(self.check_input_func)
        self.signin_pwd2_line.textChanged.connect(self.check_input_func)

註冊檢驗部分:

def check_signin_func(self):
        if self.signin_pwd_line.text() != self.signin_pwd2_line.text():
            QMessageBox.critical(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()
            QMessageBox.information(self, 'Information', 'Register Successfully')
            self.close()
        else:
            QMessageBox.critical(self, 'Wrong', 'This Username Has Been Registered!')

        self.signin_user_line.clear()
        self.signin_pwd_line.clear()
        self.signin_pwd2_line.clear()

滾軸驗證部分:

class Validate_page(QDialog):
    def __init__(self):
        super(Validate_page, self).__init__()
        self.resize(600, 500)
        self.setWindowTitle("拖動滾軸驗證")
        self.initUI()
        self.Widget_init()
        self.refresh()
        # self.show()

    def initUI(self):
        # 初始化相關控制元件位置
        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")

        self.refresh()

        self.button = QPushButton("取消", self)
        self.button.setGeometry(400, 430, 100, 50)

        self.slider = QSlider(Qt.Horizontal, self)
        self.slider.setGeometry(100, 350, 400, 50)

    def Widget_init(self):
        # 初始化相關元件的訊號連線
        self.slider.setMinimum(0)
        self.slider.setMaximum(360)
        self.slider.valueChanged.connect(self.slide)
        self.slider.sliderReleased.connect(self.check)
        self.button.clicked.connect(self.cancel)

    def slide(self, value):
        self.lb_sl.move(100 + value, self.answer_y)  # 將滾軸與圖片小塊繫結
        # print(value)

    def refresh(self):
        import random
        # 隨機生成驗證區域
        self.answer_x = random.randint(100, 460)
        self.answer_y = random.randint(100, 260)
        pic = random.randint(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 = cv2.imread(f"pic/{pic}.jpeg")  # 讀取背景圖片
        img = cv2.resize(img, (400, 200))
        x = self.answer_x - 100
        y = self.answer_y - 100
        img1 = copy.copy(img[y:y + 40, x:x + 40])  # 扣40*40的圖片小塊
        img[y:y + 40, x:x + 40] = 255  # 將被扣區域置為白色
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)

        qt_img = QImage(img.data, 400, 200, QImage.Format_RGB888)
        qt_img1 = QImage(img1.data, 40, 40, QImage.Format_RGB888)
        self.lb_bg.setPixmap(QPixmap.fromImage(qt_img))
        self.lb_sl.setPixmap(QPixmap.fromImage(qt_img1))

    def check(self):  # 檢驗滑動驗證是否成功
        # 獲取設定區域座標
        x = self.lb_sl.pos().x()
        y = self.lb_sl.pos().y()
        print(f"({x},{y})")
        if abs(x - self.answer_x) <= 3:  # 容錯為3px
            QMessageBox.information(self, "恭喜", "驗證成功")
            self.done(1)
        else:
            QMessageBox.critical(self, "錯誤", "驗證失敗")
            self.slider.setValue(0)
            self.refresh()

    def cancel(self):
        self.done(0)  # 點選取消滑動驗證

執行結果

全部背景圖片:

註冊介面:

完整程式碼

到此這篇關於python 密碼驗證(滾軸驗證)的文章就介紹到這了,更多相關python 密碼驗證(滾軸驗證)內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com