Ⅰ C++密码编程
#include<iostream>
//#include <afxcoll.h>
//#include<algorithm>
#include <windows.h>
#include<string>
using namespace std;
void main()
{
int i=0,key[7]={4,9,6,2,8,7,3};
char ch;
string pass,text;
cout<<"请输入明文:"<<endl;
//----------------------加密过程
for(ch;((ch=cin.get())!='\n');)
{
ch=ch+key[i++];
if(i>6||ch=='\n')
i=0;
if(ch>122||ch<32)
ch=ch%122+32;
pass=pass+ch;
}
cout<<"密文是:\n"<<pass<<endl;
//----------------------解密过程
char *strs=new char[pass.length()];//将字符串转换为字符数组
strs[pass.length()]='\0';
pass.(strs,pass.length(),0);
i=0;
for(int j=0;j<pass.length();j++)
{
ch=strs[j];
ch-=key[i++];
if(i>6||ch=='\n')
i=0;
if(ch<32) // 由于你的加密算法并不是一一对应,即不一定可逆,需要扩展
{ ch=ch-32;
ch=ch+122;
}
text=text+ch;
}
cout<<"解密后是:\n"<<text<<endl;
delete[]strs;
}
Ⅱ 如何用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();
}
Ⅲ c语言程序设计 密码设置程序怎么编写
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main(int argc,char *argv[])
{
const char user[]="wangpin";/*用户名自己可改动*/
const char password[]="wangpin@126";/*密码自己可改动*/
if(argc == 1)
{
printf("Input error! Usage:filename username password\n");
getch();
exit(1);
}
else if(argc == 3)
{
if (strcmp(argv[1],user) != 0 || strcmp(argv[2],password) != 0)
{
printf("Input error: Invalid username or password\n");
getch();
exit(1);
}
}
printf("Authentication Pass..\n");
sound(500);/*最简单的音乐声*/
delay(50000);
nosound();
getch();
return 0;
}
先运行这个程序得到一个exe类型的可执行文件,然后可以复制到c盘根目录下,用桌面左下的图标进入:开始-程序-附件-命令提示符
然后键入 cd \
到c盘根目录下输入
exe文件名 wangpin wangpin@126
就是运行这个程序
------------------------------------------------------------------
------------------------------------------------------------------
下面是一个简单的音乐程序,你可以把它加到上面代替sound()到nosound()那一部分发出<<东方红>>音乐歌曲(小心!声音可能很大)
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
int main(void)
{
int i,j;
int fr[]={392,392,440,294,262,262,220,294,392,392,
440,532,440,392,262,262,220,294,392,294,
262,247,220,196,392,294,330,294,262,262,
220,294,330,294,262,294,262,247,220,196};
int tim[]={4,2,2,8,4,2,2,8,4,4,2,2,2,2,4,2,2,8,4,
4,4,2,2,4,4,4,2,2,4,2,2,2,2,2,2,2,2,2,2,12};
for(i=0;i<40;i++)
{
sound(fr[i]);
delay(tim[i]*100000000);
nosound();
}
system("pause");
return 0;
}
Ⅳ 如何编程密码口令
#include "conio.h"
#include "stdio.h"
int main()
{
char c[10];
int i=0;
c[0]=getch();
while(c[i]!='\r')
{
printf("*");
c[++i]=getch(); /*先不对字符个数进行检测,假设输入字符不超过数组范围*/
}
c[++i]='\0';
return 0;
}
Ⅳ (c++) 如何编程实现:密码的输入
可以参考下面的代码:
#include <cstring>
#include <cstdio>
cout<<"Please enter password: ";
gets(user);
if(strcmp(user,"password"/* 随便输入一个初始密码*/))cout<<"error";
else {……}
(5)密码怎样编程的扩展阅读:
C++参考函数
int isupper(int ch) 若ch是大写字母('A'-'Z')返回非0值,否则返回0
int isxdigit(int ch) 若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值,否则返回0
int tolower(int ch) 若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')
int toupper(int ch) 若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')
Ⅵ 密码锁c语言编程代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
intmain()
{
charpassword[10],password2[10];
memset(password,0,sizeof(password));
memset(password2,0,sizeof(password2));
printf("请设置8位数以内密码: ");
scanf("%s",password);
printf("请设置校验密码: ");
scanf("%s",password2);
if(atoi(password2)==atoi(password))
{
printf("密码输入正确!: ");
}
else
{
printf("密码输入错误!: ");
}
return0;
}