#define UC (1U<<1) // upper case
#define LC (1U<<2) // lower case
#define NUM (1U<<3) // 0-9
#define ALL (UC|LC|NUM)
int check(const char pass1[], const char pass2[])
{
const char *p = &pass1[0]
unsigned int flag = 0
if (strlen(pass1) <6 || strlen(pass1) >8)
{
printf("password length is 6 to 8.\n")
return 1
}
if (strcmp(pass1, pass2))
{
printf("the tow passwords are diffrence.\n")
return 2
}
while (*p)
{
if (*p >= 'a' &&*p <= 'z') flag |= LC
else if (*p >= 'A' &&*p <= 'Z') flag |= UC
else if (*p >= '0' &&*p <= '9') flag |= NUM
else
{
printf("in valid charactor: %c.\n", *p)
return 3
}
++p
}
if (flag == ALL) return 0
if ((flag &UC) == 0)
{
printf("lack of uppercase.\n")
}
if ((flag &LC) == 0)
{
printf("lack of lowercase.\n")
}
if ((flag &NUM) == 0)
{
printf("lack of number.\n")
}
return -1
}
int main(int argc, char *argv[])
{
char pass1[100]
char pass2[100]
do {
printf("input password:")
scanf("%s", pass1)
printf("repeat password:")
scanf("%s", pass2)
} while (check(pass1, pass2) != 0)
return 0
}
// playFair 加密 你参考下 ...#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define x 50
char MiYao[x],PassWord[x],AddPass[x],Table[5][5],Map[25]
bool Visit[27]={false}
char English[27]="abcdefghijklmnopqrstuvwxyz"
void Input()
{
printf("请输入密钥:\t") scanf("%s",MiYao)
printf("请输入待加密密码:\t") scanf("%s",PassWord)
}
void Fun_5x5()
{
int count = 0,V =0
/*标记密钥内字符为: true*/
for(int i=0MiYao[i]!='\0'i++)
if(strchr(English,MiYao[i])!=NULL)
Visit[strchr(English,MiYao[i])-English] = true
/*执行密钥矩阵 *** 作 并标记已使用字符:true*/
for(int i=0i<5i++)
for(int j=0j<5j++)
{
if(count<strlen(MiYao))
Table[i][j] = MiYao[count++]
else
{
while(Visit[V] != false) V++
Table[i][j] = English[V]
Visit[V++] = true
}
}
puts("∞∞∞密钥矩阵为∞∞∞")
for(int i=0i<5i++)
{ for(int j=0j<5j++)
printf("%3c",Table[i][j])
puts("")
}
puts("∞∞∞∞∞∞∞∞∞∞∞")
}
int IsVisited(char ch)
{
return Visit[strchr(English,ch)-English] //false 未出现过
}
void TabletoMap()
{ int count=0
for(int i=0i<5i++)
for(int j=0j<5j++)
Map[count++]=Table[i][j]
Map[count]='\0'
}
void Judge()
{
int len = strlen(PassWord),i,j,k
memset(AddPass,0,sizeof(char))
/*一对对去字母,剩下单个字母,则不变化,直接放入加密串中.*/
if(len%2){
AddPass[len-1] = PassWord[len-1]
len -=1
}
/*一对中 密钥矩阵中 存在矩阵 eg.ab 先输出a同行顶点在输出b同行顶点*/
int row1,low1,row2,low2,a1,a2
for(i=0i<leni+=2)
{
char c1,c2
c1 = PassWord[i]
c2 = PassWord[i+1]
/*一对中 两字母相同 无变化*/
/*一对中 有字母不在密钥矩阵中 无变化*/
if(c1 == c2 || ( !IsVisited(c1)||!IsVisited(c2)))
{ AddPass[i] = c1
AddPass[i+1]=c2
}else{
a1 = strchr(Map,c1)-Map
row1 = a1/5 low1 = a1%5
a2 = strchr(Map,c2)-Map
row2 = a2/5 low2 = a2%5
/*一对中 字符出现在同行或同列 简单swap字符*/
if(row1 == row2 || low1 == low2)
{
AddPass[i] = c2
AddPass[i+1] = c1
}else{
AddPass[i] = Table[row1][low2]
AddPass[i+1] = Table[row2][low1]
}
}
}AddPass[len+1]='\0'
puts("加密后字符串:")
puts(AddPass)
puts("原串是:")
puts(PassWord)
}
int main()
{
Input()
Fun_5x5()
TabletoMap()
Judge()
return 0
}
C语言设计一个简单的加密解密程序如下:加密程序代码:
#include<stdio.h>
main()
{
char
c,filename[20]
FILE
*fp1,*fp2
printf("请输入待加密的文件名:\n")
scanf("%s",filename)
fp1=fopen(filename,"r")
fp2=fopen("miwen.txt","w")
do
{
c=fgetc(fp1)
if(c>=32&&c<=126)
{
c=c-32
c=126-c
}
if(c!=-1)
fprintf(fp2,"%c",c)
}
while(c!=-1)
}
解密程序代码:
#include<stdio.h>
#include<string.h>
main()
{
char
c,filename[20]
char
yanzhengma[20]
FILE
*fp1,*fp2
printf("请输入待解密文件名:\n")
scanf("%s",filename)
printf("请输入验证码:\n")
scanf("%s",yanzhengma)
if(strcmp(yanzhengma,"shan")==0)
{
fp1=fopen(filename,"r")
fp2=fopen("yuanwen.txt","w")
do
{
c=fgetc(fp1)
if(c>=32&&c<=126)
{
c=126-c
c=32+c
}
if(c!=-1)
fprintf(fp2,"%c",c)
}
while(c!=-1)
}
else
{
printf("验证码错误!请重新输入:\n")
scanf("%s",filename)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)