二次的密码验证机制
又来发第二次帖了,这次遇到的问题是临时想到的我想在原有程式的基础下再添加一个二次输入验证的机制(就有点像在注册帐号时的"请再输入一次密码"的感觉)
但因为我在先前有写一段纪录按键延迟的阵列,想把延迟也当作验证的一部份
大致流程
1.照"节奏"第一次输入密码时:显示输入的数(矩阵记录下密码与间隔时间),按下回车跳至第二次的验证
2.第二次验证密码:第二次验证需重复第一次的密码与节奏(考虑到人的节奏感,希望在第一次的节奏基础上有+-的容错时间差)
3.如果符合则跳至 Keys pressed/ intervals between key presses那一段,如不符合需再重复输入(希望至多5次)
麻烦各位老师了!
程式碼如下:
程序代码:
#include <stdio.h> #include <windows.h> #define MAX_KEYS 50 #define MAX_INTERVALS (MAX_KEYS - 1) int main() { INPUT_RECORD input_record; HANDLE console = GetStdHandle(STD_INPUT_HANDLE); DWORD events_read; LARGE_INTEGER start_time, end_time, frequency; BOOL last_event_was_release = FALSE; LARGE_INTEGER last_release_time; int key_count = 0; char keys[MAX_KEYS] = {0}; char password[50]; char verify_password[50]; double intervals[MAX_INTERVALS - 1] = {0}; QueryPerformanceFrequency(&frequency); printf("請設定節拍密碼\n\n"); while (1) { if (ReadConsoleInput(console, &input_record, 1, &events_read)) { if (input_record.EventType == KEY_EVENT) { if (input_record.Event.KeyEvent.bKeyDown) { if (last_event_was_release && input_record.Event.KeyEvent.wVirtualKeyCode != VK_RETURN) { LARGE_INTEGER press_time; QueryPerformanceCounter(&press_time); double interval = (double)(press_time.QuadPart - last_release_time.QuadPart) / frequency.QuadPart * 1.0; intervals[key_count - 1] = interval; printf("Time since last release: %.3fs\n", (double)(press_time.QuadPart - last_release_time.QuadPart) / frequency.QuadPart * 1.0); } QueryPerformanceCounter(&start_time); last_event_was_release = FALSE; if (key_count < MAX_KEYS) { keys[key_count++] = input_record.Event.KeyEvent.uChar.AsciiChar; } } else { LARGE_INTEGER release_time; QueryPerformanceCounter(&release_time); last_release_time = release_time; last_event_was_release = TRUE; if (input_record.Event.KeyEvent.uChar.AsciiChar != '\0') { printf("'%c' key pressed for %.3f ms\n", input_record.Event.KeyEvent.uChar.AsciiChar, (double)(release_time.QuadPart - start_time.QuadPart) / frequency.QuadPart * 1.0); } else { printf("Key pressed for %.3f s\n", (release_time.QuadPart - start_time.QuadPart) / frequency.QuadPart * 1.0); } } } } if (GetAsyncKeyState(VK_RETURN) & 1) { printf("\n==============================================================================\n"); printf("\n Keys pressed "); for (int i = 0; i < key_count; i++) { printf("%c ", keys[i]); } printf("\n"); // Output the intervals between key presses printf(" intervals between key presses: "); for (int i = 0; i < key_count - 2; i++) { printf("%.3fs ", intervals[i]); } printf("\n"); break; } } return 0; }