导航:首页 > 源码编译 > c语言编译密码怎么用

c语言编译密码怎么用

发布时间:2022-06-10 01:10:39

1. c语言如何输入密码

在C语言中不像VC与VB等可视化的编译系统,他好像没有提供这种函数)
有一种方法可以大概的实现它。用for 语句和getch( );putchar (‘*’);来实现的,而getch 不分区另ENTER和BACKSPACE等特殊键,不好控制它的结束。因此只有避过问题强行规定密码必须是8位的,但在输入密码时仍然不允许用户输入ENTER和BACKSPACE等特殊键。
或者自己写个函数,使用getch读取键盘输入,只接收那些密码允许的字符。没接受到一个合法的字符就再屏幕上输出一个“*”

2. 请问,用C语言如何实现密码输入

c语言中可采用getch()函数来实现输入密码字符时,不显示字符到终端上,这时,只需要显示出一个相应的*就可以达到效果了。参考代码及运行效果如下图:

3. 如何用C语言编译密码

异或操作啊
最简单的加密
第一次异或得到加密结果
在用同样的数组做一次异或结果还原

4. 用c语言编译一个密码程序,错误可再输入(最多三次)

main()
{
int i;
char *pwd = "123456";
char pwd2[7];
for (i=0; i<3; i++)
{
printf("请输入密码:");
scanf("%s", pwd2);
if (strcmp(pwd, pwd2)==0)
{
break;
}
else
{
printf("您已错误%d次(共3次)", i+1);
}
}

if (i<3) printf("登录成功!\n");
else printf("密码错误3次,系统锁定!\n");
}

5. 请大神帮我用C语言编译一有关于密码的程序。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<windows.h>

typedefstruct
{
intx;
inty;
}PT_STRUCT;

typedefstruct
{
charuser[20];
charpw[20];
}USER_STRUCT;

PT_STRUCTpt;
boolLoginSuccess=false;
charCurrentUser[20]={''};
intUserCount=0;
intLoginErrCount=0;
USER_STRUCT*pUser=NULL;
charWorkDir[100]={''};
charDataFile[100]={''};

voidsetxy(intx,inty)
{
COORDcoord={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

voidgetxy()
{
HANDLEhConsole=GetStdHandle(STD_OUTPUT_HANDLE);
COORDcoordScreen={0,0};//光标位置
CONSOLE_SCREEN_BUFFER_INFOcsbi;
if(GetConsoleScreenBufferInfo(hConsole,&csbi))
{
pt.x=csbi.dwCursorPosition.X;
pt.y=csbi.dwCursorPosition.Y;
}
}

voidinput(char*buf)
{
intlen=strlen(buf);
intc;
inti=0;
while(1)
{
c=getch();
if(c==8&&i==0)
{
continue;
}
if(c==10||c==13) break;
if(c==8&&i>0)
{
buf[i-1]='';
i--;
getxy();
setxy(pt.x-1,pt.y);
printf("");
setxy(pt.x-1,pt.y);
}
elseif(i>=0)
{
printf("*");
buf[i++]=c;
}
if(i>=buf)
{
return;
}
}
}

voidGetUserDataFromFile()
{
UserCount=GetPrivateProfileInt("USER","user_count",0,DataFile);
if(pUser!=NULL)
{
free(pUser);
}
pUser=(USER_STRUCT*)malloc(sizeof(USER_STRUCT)*(UserCount+1));
inti;
charch[5];
for(i=0;i<UserCount;i++)
{
memset(ch,'',5);
sprintf(ch,"%d",i+1);
GetPrivateProfileString(ch,"user","",pUser[i].user,20,DataFile);
GetPrivateProfileString(ch,"pw","",pUser[i].pw,20,DataFile);
}
}

voidWriteDataToFile()
{
inti;
chart[5]={""};
sprintf(t,"%d",UserCount);
WritePrivateProfileString("USER","user_count",t,DataFile);
for(i=0;i<UserCount;i++)
{
sprintf(t,"%d",i+1);
WritePrivateProfileString(t,"user",pUser[i].user,DataFile);
WritePrivateProfileString(t,"pw",pUser[i].pw,DataFile);
}
GetUserDataFromFile();
}

boollogin()
{
if(LoginSuccess==true)
{
printf("当前已有用户登录!重新登录吗?(Y/N)");
charsel;
fflush(stdin);
scanf("%c",&sel);
if(sel=='y'||sel=='Y')
{
LoginSuccess==false;
}
else
{
returnfalse;
}
}
charuser[20]={''};
charpw[20]={''};
printf(" InputUser:");
fflush(stdin);
scanf("%s",user);
printf(" InputPassword:");
fflush(stdin);
input(pw);
boolfind=false;
inti;
for(i=0;i<UserCount;i++)
{
if(strcmp(user,pUser[i].user)==0&&strcmp(pw,pUser[i].pw)==0)
{
find=true;
LoginSuccess=true;
strcpy(CurrentUser,pUser[i].user);
break;
}
}
if(find==true)
{
return1;
}
else
{
return0;
}
}

boolregist(char*ErrBuf)
{
charuser[20]={''};
charpw1[20]={''};
charpw2[20]={''};
printf(" InputUser:");
fflush(stdin);
scanf("%s",user);
inti;
for(i=0;i<UserCount;i++)
{
if(strcmp(user,pUser[i].user)==0)
{
strcpy(ErrBuf,"UserRepeat");
returnfalse;
}
}
printf(" InputPassword:");
fflush(stdin);
input(pw1);
printf(" ReInputPassword:");
fflush(stdin);
input(pw2);
if(strcmp(pw1,pw2)!=0)
{
strcpy(ErrBuf,"PasswordDiffer");
returnfalse;
}
strcpy(pUser[UserCount].user,user);
strcpy(pUser[UserCount].pw,pw1);
UserCount++;
returntrue;
}

intmain()
{
_getcwd(WorkDir,100);//取得当前程序的绝对路径
strcpy(DataFile,WorkDir);
strcat(DataFile,"\user.dat");
if(access(DataFile,0))
{
printf(" 用户数据文件不存在,按任意键将创建文件");
getchar();
FILE*fp;
fp=fopen(DataFile,"wb");
intre=fputs("[USER] user_count=0",fp);
if(re>=0)
{
printf(" 用户数据文件创建成功!");
}
else
{
printf(" 用户数据文件创建失败!");
}
getchar();
fclose(fp);
}
GetUserDataFromFile();
while(1)
{
system("cls");
printf("已注册用户%d",UserCount);
if(LoginSuccess==true)
{
printf("LoginedUser:%s",CurrentUser);
}
printf(" -------------------------------");
printf(" 登录---1");
printf(" 注册---2");
printf(" 退出---3");
printf(" -------------------------------");
printf(" PleaseInput1-3:");
intsel;
boolre;
charErrBuf[50]={''};
fflush(stdin);
scanf("%d",&sel);
if(sel<1||sel>3)
{
printf(" Inputerror!");
fflush(stdin);
getchar();
continue;
}
switch(sel)
{
case1:
re=login();
if(re==true)
{
printf(" LoginSuccess!CurrentUser:%s",CurrentUser);
fflush(stdin);
getchar();
}
else
{
printf(" LoginFail!");
LoginErrCount++;
if(LoginErrCount>=3)
{
printf(" LoginFail3,Programwillbeexit!");
}
fflush(stdin);
getchar();
if(LoginErrCount>=3)
{
exit(0);
}
}
break;
case2:
memset(ErrBuf,'',50);
re=regist(ErrBuf);
if(re!=true)
{
printf(" %sRegisterFail!",ErrBuf);
fflush(stdin);
getchar();
}
else
{
WriteDataToFile();
printf(" RegisterSuccess!");
fflush(stdin);
getchar();
}
break;
case3:
exit(0);
break;
default:

break;
}
}
return0;
}

6. 如何使用c语言编写一个密码程序

密码保存在文件中,从文件中读取密码,但是没做容错和异常处理,仅供参考

#include <stdio.h>
#include <string.h>

#define PSDLEN 6

void inputPsd(char *str) /*处理输入*/
{
int i;

for(i = 0; i < PSDLEN; i++)
{
while(1)
{
str[i] = getch();
if(str[i] == '\b') /*处理退格键*/
{
i--;
if(i < 0)
{
i = 0;
}
else
{
printf("\b \b");
}
continue;
}
else if(str[i] == '\r') /*处理回车键*/
{
continue;
}
else
{
printf("*");
break;
}
}
}
str[i] = '\0';
printf("\n");
}

int checkFirst() /*检测是否是第一次使用*/
{
FILE *fp;
if((fp = fopen("psd.dat", "rb")) == NULL)
{
return 1;
}
fclose(fp);
return 0;
}

void firstUse() /*第一次使用 需要输入密码*/
{
FILE *fp;
int i;
char passwd[PSDLEN + 1];
char checkPsd[PSDLEN + 1];

if((fp = fopen("psd.dat", "wb")) == NULL)
{
printf("Creat password error!\n");
exit(1);
}
while(1)
{
printf("Please input password:");
inputPsd(passwd);

printf("\nPlease input password again:");
inputPsd(checkPsd);

if(!strcmp(passwd, checkPsd))
{
break;
}
printf("\ncheck password error! \n");
}
fwrite(passwd, sizeof(char), PSDLEN, fp);
fclose(fp);
}

void login() /*核对密码,并登录*/
{
FILE *fp;
int i, num = 3;
char passwd[PSDLEN + 1];
char checkPsd[PSDLEN + 1];

if((fp = fopen("psd.dat", "rb")) == NULL)
{
puts("Open psd.dat error");
exit(1);
}
fread(passwd, sizeof(char), PSDLEN, fp);
fclose(fp);
passwd[PSDLEN] = '\0';

printf("Please input password to login");
while(num)
{
printf("you have %d chances to cry:\n", num);
inputPsd(checkPsd);
if(!strcmp(passwd, checkPsd))
{
break;
}
puts("\npassword error,Please input again");
num--;
}
if(!num)
{
puts("Press any key to exit...");
getch();
exit(0);
}
else
{
puts("\n--------\nWelcome!\n--------\n");
}
}

void main()
{
if(checkFirst())
{
firstUse();
}
else
login();

getch();
}

7. 如何编译一个C语言密码程序,判断密码的位数(如5位),如果输入的密码小于5位或者大于五位都有相应提示

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

intmain()
{
intlen=0;
charpassword[10]={0};

printf("请输入密码:");
scanf("%s",password);

len=strlen(password);

if(len<5){
printf("密码长度不足! ");
}
elseif(len>5){
printf("密码长度过长! ");
}
else{

//默认密码是admin
if(strcmp(password,"admin")==0){
printf("登录成功! ");
}
else{
printf("密码不正确! ");
}
}

system("pause");
return0;
}

8. 如何用c语言来编一个简单的密码程序

这个问题很难解释啊~如果最简单的说,你有一个固定的密码,比如123
那么直接就是
if(password==123){
//你要输出的正确信息,比如cout
?????????
}
else{
//错误信息
}
复杂来说,你的密码可能存放在一个数据库中,你就要先在数据库中查找对应的用户名,再核对密码
此外,密码一般是不能直接保存的,会被看到,所以你要有个加密的算法来保存你的密码,简单来说,你把正确的密码加密后保存,你等他输入一个密码,你用同样的算法加密,再和你的保存的加密过的比较就OK了

9. 请教高手如何用C语言译密码

专业加密,一般专门定义一个头文件,里面是一些数组,
数组里面都是对加密的初始化。
然后再通过一些方法调用初始化的加密。
这个比较复杂。
如果只是简单的添加一个密码,
那就把密码保存到一个字符串里面,然后让用户输入字符,
如果跟定好的字符符合,那么就通过,这个就非常简单了。

阅读全文

与c语言编译密码怎么用相关的资料

热点内容
仿真51单片机 浏览:864
密码器单片机 浏览:380
php订单处理 浏览:248
安庆程序员接私活哪里接 浏览:978
程序员那么可爱第9集预告片 浏览:668
手机解压缩工具在哪 浏览:757
如何启用阿里云服务器 浏览:737
python里有trim函数吗 浏览:690
pdf里面的文字怎么复制 浏览:901
ps切图压缩 浏览:299
linux删除db2 浏览:284
用prim算法求公路最优解程序 浏览:641
gpu编译android 浏览:604
miui刷机显示加密中 浏览:582
linuxqt图形界面 浏览:719
c语言常用的排序算法 浏览:762
php写本地文件 浏览:979
光影魔术手批量压缩图片 浏览:657
python菜鸟面向对象 浏览:708
考虫讲义PDF 浏览:551