当前位置:首页 » 编程语言 » c语言凯撒加密

c语言凯撒加密

发布时间: 2022-06-13 07:56:25

A. 凯撒密码,c语言,求救!

#include <stdio.h>
#include <string.h>

int main()
{
int i = 0;
int len = 0;
char ch;
char buf[256] = {0};
char nor[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char enc[26] = {'s','u','w','y','a','c','e','g','i','k','m','o','q','r','t','v','x','z','b','d','f','h','j','l','n','p'};

printf("Encode or Decode: ");
scanf("%c",&ch);
printf("please input your string: ");
fflush(stdin);
gets(buf);
len = strlen(buf);

switch (ch)
{
case 'e':
case 'E':
for (i=0;i<len;i++)
{
buf[i] = enc[buf[i] - 'a'];
}
break;
case 'd':
case 'D':
for (i=0;i<len;i++)
{
buf[i] = nor[i];
}
break;
default:
printf("wrong input!\n");
}

printf("<%s>\n",buf);

return 0;
}

B. 用C语言实现凯撒密码加密解密,急!

忘了说解密了,解密过程完全可以使用上面的代码,只要把key的值改成26-key就行了。比如,加密的密钥是3,那么解密密钥就是23,这样就可以使用同一段代码。至于为什么+23和-3的效果一样,还请翻阅数论有关剩余类环的知识。

C. C语言的凯撒加密

/*
和楼上的相比,或许 看上去很烦
ch[i] +=5;
if (ch[i] > 'Z')
{
ch[i] -= 26;
}

可以改成和 楼上的 方法
等价于 ch[i] = 'A' + (ch[i] - 'A' + 5) % 26;

*/

# include <stdio.h>
# include <stdlib.h> //用到了system(); 不写 ,可以用 getchar();

#define strwidth 117 //定义长度

int main(void)
{

char ch[strwidth];
int i ;

printf("请输入密码:");
gets(ch); //输入数据,用gets(); 保留了空格

for (i = 0; i < strwidth ;i++ )
{
if (ch[i] >= 'a' && ch[i] <= 'z' ) //判断是否小写字母
{
ch[i] +=5;
if (ch[i] > 'z') //不解释,我想这样,理解可能会方便点吧
{
ch[i] -= 26;
}
}
else if ( ch[i] >= 'A' && ch[i] <= 'Z') //判断是否大写字母
{
ch[i] +=5;
if (ch[i] > 'Z')
{
ch[i] -= 26;
}
}
}

printf("加密后为:%s\n" , ch); //输出数据

system("pause");
return 0;
}

/*

或者 这样

*/

# include <stdio.h>
# include <stdlib.h> //用到了system(); 不写 ,可以用 getchar();

#define strwidth 117 //定义长度

int main(void)
{

char ch[strwidth];
int i ;

printf("请输入密码:");
gets(ch); //输入数据,用gets(); 保留了空格

for (i = 0; i < strwidth ;i++ )
{
if (ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z' ) //判断是否是字母
{
ch[i] +=5;

if ( ch[i]>'Z' && ch[i] <= 'Z' + 5 || ch[i] > 'z' )
{
ch[i] -= 26;
}
}
}

printf("加密后为:%s\n" , ch); //输出数据

system("pause");
return 0;
}

D. 请问一下这道C语言编程题怎么做

按照题目要求编写的用凯撒密码加密的C语言程序如下

#include<stdio.h>

int main(){

char s[80];

int offset,i;

fgets(s,80,stdin);

scanf("%d",&offset);

for(i=0;s[i]!='';i++){

if('A'<=s[i] && s[i]<='Z')

if(offset>=0)

s[i]='A'+(s[i]-'A'+offset)%26;

else

s[i]='A'+(s[i]-'A'+26+offset%26)%26;

else if('a'<=s[i] && s[i]<='z')

if(offset>=0)

s[i]='a'+(s[i]-'a'+offset)%26;

else

s[i]='a'+(s[i]-'a'+26+offset%26)%26;

}

printf("%s ", s);

return 0;

}

E. c语言里的凯撒密码

#include<stdio.h>
#include<string.h>
intmain()
{
inti;intnumber;
chara[100];
scanf("%s",a);
number=strlen(a);
for(i=0;i<number;i++){
a[i]=a[i]+4;
}
for(i=0;i<number;i++){
printf("%c",a[i]);
}

return0;
}

F. 求凯撒加密法(C语言)

#include<stdio.h>
#include<conio.h> char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/
{
while(ch>=A&&ch<=Z)
{
return (A+(ch-A+n)%26);
}
while(ch>=a&&ch<=z)
{
return (a+(ch-a+n)%26);
}
return ch;
}void menu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf("\n===============================================================================");
printf("\n1.Encrypt the file");
printf("\n2.Decrypt the file");
printf("\n3.Force decrypt file");
printf("\n4.Quit\n");
printf("===============================================================================\n");
printf("Please select a item:");
return;
}void logo()/*显示版权信息*/
{
printf("\nZhensoft Encryption [Version:1.0.0]");
printf("\nCopyright (C) 2004 Zhensoft Corp.\n");
printf("\n http://www.zhensoft.com\n");
return;
}
main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();logo();
sleep(3);/*等待3秒*/menu();
ch0=getch();while(ch0!=4)
{
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);
} if(ch0==3)
{
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 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);
} for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按Q或者q退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("===============================================================================\n");
printf("The outfile is:\n");
printf("===============================================================================\n");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf("\n===============================================================================\n");
printf("The current key is: %d \n",i);/*显示当前破解所用密码*/
printf("Press Q to quit and other key to continue......\n");
printf("===============================================================================\n");
ch1=getch();
if(ch1==q||ch1==Q)/*按Q或者q时退出*/
{
clrscr();
logo();
printf("\nGood Bye!\n");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
} printf("\nForce decrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
logo();
printf("\nGood Bye!\n");
sleep(3);
}

G. C语言编程问题,凯撒密码

#include<stdio.h>
chars[]="(BTBU)isakeystate-,Sciences,Engineering,Law,Economics,History,PhilosophyandManagement.";
voidfun(char*s,intn)
{
inti=0;
while(s[i]!=0)
{
if(s[i]<='z'&&s[i]>='a')
{
s[i]+=n;
if(s[i]>'z')s[i]-=24;
}
elseif(s[i]<='Z'&&s[i]>='A')
{
s[i]+=n;
if(s[i]>'Z')s[i]-=24;
}
i++;
}
}
voidmain()
{
fun(s,3);
puts(s);
}

H. 凯撒密码的算法c语言的怎么实现啊

凯撒密码是一种非常古老的加密方法,相传当年凯撒大地行军打仗时为了保证自己的命令不被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。他的原理很简单,说到底就是字母于字母之间的替换。下面让我们看一个简单的例子:“”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。

#include <stdio.h>
main()
{
char M[100];
char C[100];
int K=3,i;
printf("请输入明文M(注意不要输入空白串)\n");
gets(M);

for(i=0;M[i]!='\0';i++)
C[i]=(M[i]-'a'+K)%26+'a';
C[i]='\0';

printf("结果是:\n%s\n",C);
}

I. 用C语言编程恺撒密码加密解密程序

#include<stdio.h>

#defineisletter(c)(((c)>='a'&&(c)<='z')||((c)>='A'&&(c)<='Z'))

voidEnc(constchar*str,char*out,intkey)
{
inti=0;
while(str[i])
{
if(isletter(str[i]))
{
out[i]=str[i]+key;
if(!isletter(out[i]))
out[i]-=26;
}
else
out[i]=str[i];
i++;
}
out[i]=0;
}
voidDenc(constchar*str,char*out,intkey)
{
inti=0;
while(str[i])
{
if(isletter(str[i]))
{
out[i]=str[i]-key;
if(!isletter(out[i]))
out[i]+=26;
}
else
out[i]=str[i];
i++;
}
out[i]=0;
}

intmain()
{
charout[100],out2[100];
Enc("",out,3);
printf("%s ",out);
Denc(out,out2,3);
printf("%s ",out2);
}

J. 关于C语言的凯撒加密

你需要添加路径: ../file.txt;
因为你的exe文件在debug文件夹下,在该文件夹下没有你输入的哪个文件,程序中是只读的形式打开文件的。所以,会提示存出错。

两种解决办法:
第一个:将exe拷贝到加密文件的路径下
第二个:将文件烤到exe文件路径下。
根本的解决办法就是: 给定文件的相对路径或者绝对路径,绝对不会出现问题的。

热点内容
创建存储空间 发布:2024-05-18 21:20:57 浏览:120
sql日期和时间 发布:2024-05-18 21:16:19 浏览:141
安卓网页怎么截取 发布:2024-05-18 20:53:56 浏览:970
在配置更新的时候没电关机怎么办 发布:2024-05-18 20:36:10 浏览:927
win7访问win2000 发布:2024-05-18 20:27:41 浏览:388
青岛人社局密码多少 发布:2024-05-18 20:19:10 浏览:734
无法存储呼叫转移 发布:2024-05-18 20:18:30 浏览:126
数据库的调优 发布:2024-05-18 20:18:29 浏览:346
sqlserver注册表清理 发布:2024-05-18 20:13:14 浏览:991
linux删除连接 发布:2024-05-18 20:06:56 浏览:822