c语言如何隐藏登陆密码

Python014

c语言如何隐藏登陆密码,第1张

#include<conio.h> //这个头文件不是标准库函数的,一般编译器有的,但是unix和linux编译器是没有的

#define PASSWORD "123456"

int password()

{

char

p[20],i=0

system("cls")

printf("请输入密码 =>")

while(p[i]=getch())

{

if(p[i]==13)

break

if(p[i]!='\b')

{

printf("*")

i++

}

else

{

printf("\b

\b")

i--

}

}

p[i]='\0'

if(strcmp(p,PASSWORD)==0)

{

printf("验证通过")

press()

return 1

}

else

{

printf("密码错误")

press()

return 0

}

}

在linux中getch可以做到不回显,但是用法比windos中复杂一点点,以下是简单实现,密码设为6位,可以按退格修改,就只写了这些功能,望采纳

#include<stdio.h>

#include<curses.h>

int main()

{

int i,j=0

char ch

initscr()

cbreak()

noecho()

curs_set(0)

for(i=0i<6+2*ji++)

{

ch=getch()

addstr("*")

if(i)

{

if(ch==127)

{

j++

move(0,i+1-2*j)

clrtoeol()

}

}

}

getch()

endwin()

return 0

}

#include <stdio.h>

#define MAX_STR_LEN  32  

   

char passwd[MAX_STR_LEN] = {0}  

   

char *GetPasswd(void)  

{  

    char c  

    int i = 0  

    int len = MAX_STR_LEN-1  

       

    while ((c=getch()) != '\r')  

    {  

        passwd[i] = c  

        putchar('*')  

        i++  

        if (i >= len)  

        {  

            break  

        }  

    }  

       

    return passwd  

}  

   

int main(void)  

{  

    char *dispstr = NULL  

   

    dispstr = GetPasswd()  

    printf("\nthe password is : %s\n", dispstr)  

   

    return 0  

}