Python - base64 - 比较用户输入与存储的 PIN

Python - base64 - comparing user input vs stored PIN

我在检查输入的引脚 (check_pin) == 正确的引脚 (pin) 时遇到问题

check_pin 和 pin 的测试输出是相同的,但它不认为它们是等效的(第 4 行)

代码:

pin_check = input("Please input your 4 digit PIN number: ")
print("g"+str(base64.b64encode(bytes(pin_check, "utf-8"))))
print(pin)
    if "g"+str(base64.b64encode(bytes(pin_check, "utf-8"))) == pin:
        y = 0
    else:
        v = v + 1
        y = int(y) - 1
        print("Incorrect PIN,",y,"attempts remaining.\n")

输出:

Please input your 4 digit PIN number: 1234 [user input, correct pin[1234]]
gb'MTIzNA=='    [stored pin]
gb'MTIzNA=='    [user input pin]

Incorrect PIN, 2 attempts remaining.  [it should set y = 0, not print this line]

存储的pin:Pin.txt由几行组成:gb'MTIzNA=='

    import linecache
    fo = open("Pin.txt", "r")
    pin = linecache.getline('Pin.txt', int(card_no))
    print(pin)
    fo.close()

鉴于您从文件中读取的方式,请注意 linecache.getline 还将读取行尾的换行符。所以 pin 的内容实际上是 "gb'MTIzNA=='\n" 并且不等于 pin_check.

的 base64 编码值

在您的示例输出中,您在第三行和第五行之间有一个额外的换行符这一事实表明了这一点(因为从代码来看,第二行实际上是编码的用户输入 pin)。

您可能想要做的是

pin = linecache.getline('Pin.txt', int(card_no)).strip()

也就是去掉刚才读的那一行