凯撒密码破解

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
**基于凯撒密码的加密解密小程序
*/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<windows.h>

#defineDECIPHER_SIZE26
#defineNAME_SIZE20//文件名的长度
#defineHEAD"======================================="
#defineLINE"---------------------------------------"
#defineEND"======================================="

voidmenu(void);//菜单函数

charencrypt(charconstch,intkey);//加密函数
chardecipher(charconstch,intkey);//解密函数

FILE*open_file(charconst*name,charconst*style);//打开文件的函数
voidclose_file(FILE*fname);//关闭文件的函数

intmain(void)
{
intselect,key;

charyes;//保存用户的选择(y/n);
char*name;
char*store;
charch;//变量ch保存每次从文件中读出来的一个字符

FILE*fp,*input;//分别用来打开源文件和目的文件的文件指针变量

name=malloc(NAME_SIZE);
store=malloc(NAME_SIZE);
menu();//打印菜单

while(1)
{
printf("请选择:");
scanf("%d",&select);//用户输入他选择的操作

if(1==select)
{
printf("输入需要加密的文件的文件名:");
scanf("%s",name);

fp=open_file(name,"r");//打开要进行加密的文件

printf("请输入加密密钥:");
scanf("%d",&key);
printf("输入保存加密后文件的文件名:");
scanf("%s",store);

input=open_file(store,"w");//打开加密后文件所要保存的目的文件

while((ch=fgetc(fp))!=EOF)
{
fputc(encrypt(ch,key),input);
}

//关闭文件
close_file(fp);
close_file(input);

}

/*解密操作*/
elseif(2==select)
{
printf("输入需要解密的文件的文件名:");
scanf("%s",name);


printf("请输入解密后要存入的文件名:");
scanf("%s",store);
input=open_file(store,"a");//打开保存解密后文本的文件
key=1;
while(key<DECIPHER_SIZE)
{
fp=open_file(name,"r");

while((ch=fgetc(fp))!=EOF)
{
fputc(decipher(ch,key),input);//打开需要解密的文件
}
close_file(fp);

fputc('\n',input);
++key;
}
close_file(input);
}

//用户选择退出系统是的操作
elseif(3==select)
{
printf("真的要退出系统?(y/n)\n");
scanf("%s",&yes);

if('n'==yes||'N'==yes)
continue;

if('y'==yes||'Y'==yes)
exit(0);
}

else
{
printf("错误的输入,请重新输入!\n");
continue;
}

/*询问用户是否继续*/
printf("是否继续?(y/n):");
scanf("%s",&yes);

if('n'==yes||'N'==yes)
exit(-1);

if('y'==yes||'Y'==yes)
system("cls");
menu();
}

/*释放内存*/
free(name);
free(store);

returnEXIT_SUCCESS;//成功返回
}

/*函数用来打印菜单*/
voidmenu(void)
{
printf("****凯撒密码加密解密系统****\n");
printf(HEAD"\n");
printf("\t☆☆☆MENU☆☆☆\n");
printf(LINE"\n");
printf("Option:\n");
printf("\t1.对文件加密\n");
printf("\t2.对文件解密\n");
printf("\t3.退出系统\n");
printf(LINE"\n");
printf("\t版本:V1.0\n");
printf(END"\n");

}

/*函数用来打开文件*/
FILE*open_file(charconst*name,charconst*style)
{
FILE*fp;
fp=fopen(name,style);
if(NULL==fp)
{
perror("打开文件失败!");
exit(EXIT_FAILURE);
}
returnfp;
}

/*函数用来关闭文件*/
voidclose_file(FILE*fname)
{
if(fclose(fname)!=0)
{
perror("关闭文件失败!");
exit(EXIT_FAILURE);
}
}


/*对字符进行加密操作的函数*/
charencrypt(charconstch,intkey)
{

if(ch>=0&&ch<=128)
return(ch+key)%128;
}


/*对字符进行解密操作的函数*/
chardecipher(charconstch,intkey)
{
key=26-key;

if(ch>='a'&&ch<='z')
return'a'+(ch-'a'+key)%DECIPHER_SIZE;

elseif(ch>='A'&&ch<='Z')
return'A'+(ch-'A'+key)%DECIPHER_SIZE;

else
returnch;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
凯撒密码加解密程序(C语言) 2009年09月30日 星期三 13:21 1、程序结构化,用函数分别实现 2、对文件的加密,解密输出到文件 #include #include void menu()/*菜单,1.加密 2.解密 3.退出*/ { clrscr(); printf("\n==============================================================================="); printf("\n1.Encrypt the file"); printf("\n2.Decrypt the file"); printf("\n3.Quit\n"); printf("===============================================================================\n"); printf("Please select a item:"); return; } char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/ { while(ch>='A'&&ch='a'&&ch<='z') { return ('a'+(ch-'a'+n)%26); } return ch; } main() { int i,n; char ch0,ch1; FILE *in,*out; char infile[10],outfile[10]; textbackground(RED); textcolor(LIGHTGREEN); clrscr(); menu(); ch0=getch(); while(ch0!='3') { if(ch0=='1') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要加密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the key:"); scanf("%d",&n);/*输入加密密码*/ printf("Please input the outfile:"); scanf("%s",outfile);/*输入加密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } while(!feof(in))/*加密*/ { fputc(encrypt(fgetc(in),n),out); } printf("\nEncrypt is over!\n"); fclose(in); fclose(out); sleep(1); } if(ch0=='2') { clrscr(); printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要解密的文件名*/ if((in=fopen(infile,"r"))==NULL) { printf("Can not open the infile!\n"); printf("Press any key to exit!\n"); getch(); exit(0); } printf("Please input the key:"); scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/ n=26-n; printf("Please input the outfile:"); scanf("%s",outfile);/*输入解密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL) { printf("Can not open the outfile!\n"); printf("Press any key to exit!\n"); fclose(in); getch(); exit(0); } while(!feof(in)) { fputc(encrypt(fgetc(in),n),out); } printf("\nDecrypt is over!\n"); fclose(in); fclose(out); sleep(1); } clrscr(); printf("\nGood Bye!\n"); sleep(3); getch(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值