導航:首頁 > 源碼編譯 > 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語言編譯密碼怎麼用相關的資料

熱點內容
phpjson合並 瀏覽:458
app伺服器程序放在哪裡 瀏覽:841
電商怎麼選擇雲伺服器 瀏覽:565
錘子視頻文件夾 瀏覽:16
演算法的兩要素是什麼和什麼 瀏覽:773
如何創建伺服器多用戶 瀏覽:654
javaonlinejudge編譯錯誤 瀏覽:65
命令與征服3泰伯利亞戰爭升級 瀏覽:690
投標工具需要加密鎖嗎 瀏覽:503
蘇州阿里雲伺服器服務電話 瀏覽:783
怎麼知道app專屬流量 瀏覽:62
單片機模擬動畫教程 瀏覽:735
linux解壓鏡像 瀏覽:164
c語言可以在哪編譯 瀏覽:127
如何對spl的密碼加密 瀏覽:73
oppoa59s如何添加應用加密 瀏覽:515
比特幣asic演算法 瀏覽:175
查看伺服器外網訪問地址 瀏覽:857
魔獸爭霸地圖最新加密 瀏覽:687
暢捷雲APP怎麼l發票 瀏覽:213