c語言密碼
㈠ c語言 修改密碼初始密碼為123456,密碼包含數字和字母,數字必須大於2位
#include<stdio.h>
#include<string.h>
#include<ctype.h>
intmain()
{charpsw[21]="123456",t1[21],t2[21];
inti,n=0;
printf("請輸入初始密碼: ");
do
{gets(t1);
n++;
err=strcmp(t1,psw);
if(err)printf("初始密碼錯誤!請重新輸入! ");
}
while(err&&n<3);
if(strcmp(t1,psw))
{printf("密碼錯誤,不能登錄! ");
return0;
}
while(n<3)
{while(n<3)
{printf("請輸入新密碼: ");
gets(t1);
for(i=0;t1[i];i++)
if(isdigit(t1[i]))n++;
if(n<3)
{n=0;
printf("數字必須大於2位! ");
}
}
printf("請再次輸入新密碼: ");
gets(t2);
if(strcmp(t1,t2)==0)
printf("密碼修改成功! ");
else
{printf("二次密碼不一致! ");
n=0;
}
}
return0;
}
㈡ c語言密碼核對
加了一個,while句,錯了 可以繼續輸入~
#include <stdio.h>
#include <string.h>
int comp(char* code1, char* code2); //定義密碼比較函數
int main(void)
{
char code1[20]={0,};
char code2[20]={0,};
int rt;
while(1)
{
printf("輸入密碼: "); //pw1
scanf("%s",code1);
printf("密碼確認: "); //pw2
scanf("%s",code2);
rt = comp(code1,code2); // Call 比較函數
if(rt == 0)
{
printf("\n-恭喜!密碼一直!\n\n");
break;
}
else
printf("\n-很遺憾!密碼錯了!\n\n");
memset(code1,0x00,sizeof(code1)); //清楚內容
memset(code2,0x00,sizeof(code2));
}
return 0;
}
int comp(char* code1, char* code2) //比較函數 構造
{
return strcmp(code1,code2);
}
㈢ c語言 密碼輸入
因為你不清空數組,input超過密碼長度的值還保留在數組裡面。給你詳細舉例,
當你輸入超過密碼長度hehe1234,你的input裡面的數據就是{h,e,h,e,1,2,3,4...}這時候你再輸入正確密碼hehe123,前面的都被覆蓋,但是4還保留在數組裡面,你的input其實還是hehe1234,所以你不可能再得到正確密碼。
㈣ 幫忙用C語言寫一個密碼
#include <stdio.h>
#define MAX_LENGTH 128
#define FILE_NAME "pwd.dat"
#define INIT_PWD "123456"
char pwd[MAX_LENGTH+1];
void Init ( void )
{
FILE *fp;
fp = fopen ( FILE_NAME, "r" );
if ( fp == NULL )
{
strcpy ( pwd, INIT_PWD );
}
else
{
fgets ( pwd, MAX_LENGTH, fp );
fclose ( fp );
}
}
void Login ( void )
{
char ch;
char tmp[MAX_LENGTH+1];
int pass = 1;
while ( pass )
{
puts ( "==================================\nPlease input your password!" );
scanf ( "%s", tmp );
pass = strcmp ( tmp, pwd );
}
}
void Edit ( void )
{
puts ( "Please input a new password!" );
scanf ( "%s", pwd );
}
void End ( void )
{
FILE *fp;
fp = fopen ( FILE_NAME, "w" );
if ( fp == NULL )
{
printf ( "Cannot save your password!\n" );
system ( "pause" );
}
else
{
fputs ( pwd, fp );
fclose ( fp );
}
}
int main ( void )
{
Init();
Login();
Edit();
End();
}
㈤ c語言實現密碼加密
unsignedchar*encrypt(unsignedchar*psw,intenc){
intsum=0,i;
if(enc){
for(i=0;i<6;i++){
psw[i]-=15;
sum+=psw[i];
}
psw[6]=(unsignedchar)sum;
psw[7]=0;
}
else{
for(i=0;i<6;i++){
sum+=psw[i];
psw[i]+=15;
}
if((unsignedchar)sum!=psw[6]){
printf("Badpassword ");
psw[0]=0;
returnpsw;
}
else{
psw[6]=0;
}
}
for(i=0;i<3;i++){
unsignedchart=psw[i];
psw[i]=psw[5-i];
psw[5-i]=t;
}
returnpsw;
}
intmain()
{
unsignedcharpsw[128];
scanf("%s",psw);
printf("encodeto:%s ",encrypt(psw,1));
printf("decodeto:%s ",encrypt(psw,0));
return0;
}
㈥ C語言如何實現輸入密碼以星號顯示
C語言中可採用getch()函數來實現輸入密碼字元時,不顯示字元到終端上,這時,只需要顯示出一個相應的*就可以達到效果了。參考代碼及運行效果如下圖:
㈦ 如何用C語言編寫密碼程序
1、用一個字元數組來存密碼
再用一個字元數組接收你的輸入,然後用strcmp
來比較,如果返回0則密碼是正確的
2、常式:
#include"stdio.h"
#include"string.h"
intmain()
{
charmima[100]="YuanShi888";
charinput[100]={0};
printf("請輸入密碼:");
gets(input);
if(strcmp(mima,input)==0)
printf("恭喜你,密碼正確! ");
else
printf("對不起,密碼輸入錯誤! ");
}
㈧ 請問,用C語言如何實現密碼輸入
c語言中可採用getch()函數來實現輸入密碼字元時,不顯示字元到終端上,這時,只需要顯示出一個相應的*就可以達到效果了。參考代碼及運行效果如下圖:
㈨ c語言密碼
用什麼替換?
暫時用小寫加移位取?比如A加密成c,B加密成d,就是大寫變小寫後位置發生變化。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void EncodeString(char *str,int key)
{
int length,i;//length為傳入字元串長度,i用作循環計數器
length=strlen(str);
for(i=0;i<length;i++)//對字元串中的每個字元依次進行加密
{
if(isupper(str[i]))//對大寫字母加密
{
str[i]+=key%26;
if(str[i]>'Z')
{
str[i]-=26;
}
else if(str[i]<'A')
{
str[i]+=26;
}
}
else if(islower(str[i]))//對小寫字母加密
{
if(str[i]+key%26<128){
str[i]+=key%26;
if(str[i]>'z')
{
str[i]-=26;
}
else if(str[i]<'a')
{
str[i]+=26;
}
}
else{str[i]-=26;str[i]+=key%26;}
}
}
}
void main()
{
char arr[50],buffer;//arr[50]用來接收字元串信息,buffer用來接收緩沖區中的回車
int key;//key為加密秘鑰
printf("This program encodes messages using a cyclic cipher.
");
printf("To stop, enter 0 as the key.
");
while(1)//程序一直運行,直到輸入密鑰0為止
{
printf("Enter the key: ");
scanf("%d",&key);
scanf("%c",&buffer);
if(0==key)
{
break;//輸入密鑰為0,則退出程序
}
printf("Enter a message: ");
scanf("%s",arr);
scanf("%c",&buffer);
EncodeString(arr,key);
printf("Encoded message: %s
",arr);
}
}