重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
凯撒密码是一种非常古老的加密方法,相传当年凯撒大地行军打仗时为了保证自己的命令不被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。他的原理很简单,说到底就是字母于字母之间的替换。下面让我们看一个简单的例子:“baidu”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。
创新互联建站主营三原网站建设的网络公司,主营网站建设方案,重庆APP软件开发,三原h5微信小程序开发搭建,三原网站营销推广欢迎三原等地区企业咨询
#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);
}
#includestdio.h
#includestring.h
void main ()
{
char str[100];
char str1[100];
printf("输入字符串:");
scanf("%s",str);
int len;
len=strlen(str);
for(int i=0;i<len;i++)
{
str1[i]=(str[i]-97+3)%26+97;
}
str1[len]='\0';
printf ("密文为:%s\n",str1);
}
#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;ilen;i++)
{
buf[i] = enc[buf[i] - 'a'];
}
break;
case 'd':
case 'D':
for (i=0;ilen;i++)
{
buf[i] = nor[i];
}
break;
default:
printf("wrong input!\n");
}
printf("%s\n",buf);
return 0;
}
#include stdio.h
#define isletter( c ) ( ((c)='a'(c)='z') || ((c)='A'(c)='Z') )
void Enc( const char *str, char *out, int key )
{
int i = 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;
}
void Denc( const char *str, char *out, int key )
{
int i=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;
}
int main()
{
char out[100], out2[100];
Enc( "THE QUICK BROWn fox jumps over THE LAZY DOG", out, 3 );
printf( "%s\n", out );
Denc( out, out2, 3 );
printf( "%s\n", out2 );
}