导航:首页 > 文档加密 > 凯撒法加密的程序

凯撒法加密的程序

发布时间:2022-08-20 15:58:53

A. 凯撒加密法,求代码

不好意思! 这个 真的 不擅长! 帮不了您!

B. C语言编写凯撒加密(简单版的),网上搜到的那个大哥200多行代码用不着……

#include<ctype.h>

int jiami(char str[],int len)
{
for(int i=0;i<len;i++)
{
if(!isalpha(str[i]))// 判断是否为字符
{
str[i]=str[i]+3;
if(str[i]>'Z'&&str[i]<'a')str[i]=str[i]-'Z'+'A'-1;//控制X,Y,Z回到A,B,C
if(str[i]>'z')str[i]=str[i]-'z'+'a'-1;//控制x,y,z回到a,b,c
}
}
return 1;
}

这是我写的,已经可以用了,你试试

C. 用C语言实现任意字符串的加密,其中,字母用凯撒加密方法加密,非字母不变

我尽量用注释阐述了思路,希望可以帮到你!!

#include<stdio.h>
#include<string.h>
#define N 80 //可加密字符串最大长度

char plaintext[N]={0}; //明文,输入时输入字符,参与运算时强制转换成整数
int ciphertext[N]={0}; //密文,保存成整数,输出时强制转换成字符
int k; //后(右)移位数,相当于密钥

void getPlainText() //获得明文字符串
{
printf("请输入明文:");
scanf("%s",plaintext);
printf("\n");
}

void getLength() //获取后(右)移位数(密钥)
{
printf("请输入后移的位数:");
scanf("%d",&k);
k%=26; //因为字母只有26个,所以超过26相当于重复
}

void Caesar_cipher() //凯撒加密,本程序采用的是字母循环后(右)移
{
unsigned int i;

for(i=0;i<strlen(plaintext);i++)
{
//两个bool类型的变量是为了判断字符是否是字母(包括大写和小写)
bool flag1=plaintext[i]>='a'&&plaintext[i]<='z';
bool flag2=plaintext[i]>='A'&&plaintext[i]<='Z';

if(flag1||flag2){ //如果是字母,加密
ciphertext[i]=(int)plaintext[i]+k; //字母在字母表中后(右)移K位
if(ciphertext[i]>(int)'z'){ //保证是循环后(右)移
ciphertext[i]-=26;
}
}
else //非字母字符,不做处理,原样保存
ciphertext[i]=(int)plaintext[i];

}

}

void printCipherText() //输出加密后的密文
{
unsigned int i;
printf("\n加密后的密文是:");
for(i=0;i<strlen(plaintext);i++) //把参与计算后是整数强制转换成对应的字符
printf("%c",(char)ciphertext[i]);
printf("\n");

}

void main()
{
getPlainText(); //明文
getLength(); //后(右)移位数
Caesar_cipher(); //凯撒加密
printCipherText(); //密文

}

D. 什么是凯撒加密法

简单的说,就是位移加密。
比如你的密码是ABCDE
然后设置凯撒密码的偏移量为3的话
那加密之后的密码就是DEFGH

E. 求凯撒加密法(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);
}

F. 凯撒加密法

凯撒加密法的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左或向右移动一个固定数目的位置。例如,当偏移量是左移3的时候(解密时的密钥就是3):
明文字母表:ABCDEFGHIJKLMNOPQRSTUVWXYZ
密文字母表:DEFGHIJKLMNOPQRSTUVWXYZABC
使用时,加密者查找明文字母表中需要加密的消息中的每一个字母所在位置,并且写下密文字母表中对应的字母。需要解密的人则根据事先已知的密钥反过来操作,得到原来的明文。例如:
明文:THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
密文:WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ
凯撒加密法的加密、解密方法还能够通过同余的数学方法进行计算。首先将字母用数字代替,A=0,B=1,...,Z=25。此时偏移量为n的加密方法即为:
En(x)=(x+n)mod26{\displaystyle E_{n}(x)=(x+n)\mod 26}
解密就是:
Dn(x)=(x−n)mod26{\displaystyle D_{n}(x)=(x-n)\mod 26}

G. 3、 凯撒加密算法加解密程序:

明文以实现加密,输出密文。

H. C++编程 凯撒加密

#include<iostream>
#include<string>
usingnamespacestd;
voidencrypt(string&s)//加密
{

inti=s.length();
for(intj=0;j<i;++j)
{
if('A'<=s[j]&&s[j]<='Z')
{
if('A'<=s[j]&&s[j]<='W')
s[j]+=3;
else
s[j]-=23;
//cout<<s[j];
s[j]+=32;
//cout<<s[j]<<endl;
}
elseif('a'<=s[j]&&s[j]<='z')
{
if('a'<=s[j]&&s[j]<='w')
s[j]+=3;
else
s[j]-=23;
//cout<<s[j];
s[j]-=32;
//cout<<s[j]<<endl;

}

}
}

intmain()//测试
{
stringxy;
cout<<"输入字符串"<<endl;
cin>>xy;
encrypt(xy);
cout<<xy<<endl;
return0;

}

听说回答的够长才能够自动采纳

I. 用python2.7.10编写凯撒密码加密和解密程序

s=raw_input('[开始加密]pleaseinputyourstr:')
s=list(s)
n=0
forswins:
s[n]=chr(ord(sw)+3)
n=n+1
sout=''
forsw2ins:
sout=sout+sw2
print'[加密结果]:',sout

解密的类似,主要用到ord、chr函数。

J. 将凯撒密码X的加密、解密过程用C语言编程实现

1、在密码学中,恺撒密码(或称恺撒加密、恺撒变换、变换加密)是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。恺撒密码通常被作为其他更复杂的加密方法中的一个步骤,例如维吉尼尔密码。恺撒密码还在现代的ROT13系统中被应用。但是和所有的利用字母表进行替换的加密技术一样,恺撒密码非常容易被破解,而且在实际应用中也无法保证通信安全。例子恺撒密码的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左或向右移动一个固定数目的位置。

2、kaiser加密算法具体程序:

#include<stdio.h>
#include<conio.h>
charencrypt(charch,intn)/*加密函数,把字符向右循环移位n*/
{
while(ch>='A'&&ch<='Z')
{
return('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return('a'+(ch-'a'+n)%26);
}
returnch;
}
voidmenu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf(" =========================================================");
printf(" 1.Encryptthefile");
printf(" 2.Decryptthefile");
printf(" 3.Forcedecryptfile");
printf(" 4.Quit ");
printf("========================================================= ");
printf("Pleaseselectaitem:");
return;
}
main()
{
inti,n;
charch0,ch1;
FILE*in,*out;
charinfile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*输入加密密码*/
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Encryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/
n=26-n;
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Decryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("========================================================== ");
printf("Theoutfileis: ");
printf("========================================================== ");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf(" ======================================================== ");
printf("Thecurrentkeyis:%d ",i);/*显示当前破解所用密码*/
printf("Press'Q'toquitandotherkeytocontinue...... ");
printf("========================================================== ");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/
{
clrscr();
printf(" GoodBye! ");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf(" Forcedecryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf(" GoodBye! ");
sleep(3);
}
阅读全文

与凯撒法加密的程序相关的资料

热点内容
为什么会服务器不可用 浏览:290
wow宏命令设置 浏览:264
解压神器一张纸折叠魔术球 浏览:23
怎么样可以取消加密软件oppo 浏览:580
屏幕共享源码哪家比较不错 浏览:665
vb中双击命令按钮 浏览:208
服务器做了磁盘阵列怎么重装 浏览:606
逻辑加密ic卡能用吗 浏览:883
c语言代码编译器手机版 浏览:289
recovery无命令 浏览:957
想妈妈的命令 浏览:578
网站接入方式怎么填写云服务器 浏览:859
薯仔视频APP怎么看不了 浏览:550
社交软件app该怎么聊 浏览:23
pc的启动文件夹 浏览:671
文件夹压缩过程中点击取消压缩 浏览:216
顺丰app专享优惠券怎么用 浏览:667
酷狗音乐分享文件夹 浏览:826
服务器mgmt旁边的接口是什么 浏览:844
单片机发光二极管原理图 浏览:50