C语言 凯撒加密

古罗马时期,凯撒大帝曾经使用密码来传递信息,它是一种替代密码,对于信件中的每个字母,会用其它字母代替,比如:a用s代替,b用u来代替,具体替换规则如下表所示:

 

#include <stdio.h>
void jiami(char []);
void jiemi(char []); 
int i=0;
int main(){
	int u;
	char str[100];
	printf("加密请键入“1 ”,解密请键入“2 ”:  "); 
	scanf("%d",&u);
	if(u==1){
	printf("请输入要加密的字段(100个字符以内):\n");
	fflush(stdin);//清空文件缓冲区
	//scanf("%s",str); 
	gets(str);
	printf("加密结果为:\n");
	jiami(str);//调用加密 
	}else{
	    if(u==2){
	    printf("请输入要解密的字段(100个字符以内):\n");
	    fflush(stdin);//清空文件缓冲区
	    //scanf("%s",str); 
	    gets(str);
	    printf("解密结果为:\n");
	    jiemi(str);//调用解密 
	    //scanf("%s",str); 
	}else{
	printf("输入错误"); 
	}
	}
}
void jiami(char str[100]){//加密算法 
	if(str[i]!='\0'){
		if(str[i]==65||str[i]==97) str[i]+=18; 
		else 
	    if(str[i]==66||str[i]==98) str[i]+=19;
		else 
		if(str[i]==67||str[i]==99) str[i]+=20;
		else 
		if(str[i]==68||str[i]==100) str[i]+=21;
		else 
		if(str[i]==69||str[i]==101) str[i]-=4;
		else 
		if(str[i]==70||str[i]==102) str[i]-=3;
		else 
		if(str[i]==71||str[i]==103) str[i]-=2;
		else 
		if(str[i]==72||str[i]==104) str[i]-=1;
		else 
		if(str[i]==73||str[i]==105) ;
		else 
		if(str[i]==74||str[i]==106) str[i]+=1;
		else 
		if(str[i]==75||str[i]==107) str[i]+=2;
		else 
		if(str[i]==76||str[i]==108) str[i]+=3;
		else 
		if(str[i]==77||str[i]==109) str[i]+=4;
		else 
		if(str[i]==78||str[i]==110) str[i]+=4;
		else 
		if(str[i]==79||str[i]==111) str[i]+=5;
		else 
		if(str[i]==80||str[i]==112) str[i]+=6;
		else 
		if(str[i]==81||str[i]==113) str[i]+=7;
		else 
		if(str[i]==82||str[i]==114) str[i]+=8;
		else 
		if(str[i]==83||str[i]==115) str[i]-=17;
		else 
		if(str[i]==84||str[i]==116) str[i]-=16;
		else 
		if(str[i]==85||str[i]==117) str[i]-=15;
		else 
		if(str[i]==86||str[i]==118) str[i]-=14;
		else 
		if(str[i]==87||str[i]==119) str[i]-=13;
		else
		if(str[i]==88||str[i]==120) str[i]-=12;
		else
		if(str[i]==89||str[i]==121) str[i]-=11;
		else
		if(str[i]==90|str[i]==122) str[i]-=10;
		
		printf("%c",str[i]);
	    i++;
		jiami(str);
		}
}
void jiemi(char str[100]){//解密算法 
	if(str[i]!='\0'){
		if(str[i]==65||str[i]==97) str[i]+=4;
		else 
	    if(str[i]==66||str[i]==98) str[i]+=17;
		else 
		if(str[i]==67||str[i]==99) str[i]+=3;
		else 
		if(str[i]==68||str[i]==100) str[i]+=16;
		else 
		if(str[i]==69||str[i]==101) str[i]+=2;
		else 
		if(str[i]==70||str[i]==102) str[i]+=15;
		else 
		if(str[i]==71||str[i]==103) str[i]+=1;
		else 
		if(str[i]==72||str[i]==104) str[i]+=14;
		else 
		if(str[i]==73||str[i]==105) ;
		else 
		if(str[i]==74||str[i]==106) str[i]+=13;
		else 
		if(str[i]==75||str[i]==107) str[i]-=1;
		else 
		if(str[i]==76||str[i]==108) str[i]+=12;
		else 
		if(str[i]==77||str[i]==109) str[i]+=4;
		else 
		if(str[i]==78||str[i]==110) str[i]+=11;
		else 
		if(str[i]==79||str[i]==111) str[i]-=3;
		else 
		if(str[i]==80||str[i]==112) str[i]+=10;
		else 
		if(str[i]==81||str[i]==113) str[i]-=4;
		else 
		if(str[i]==82||str[i]==114) str[i]-=4;
		else 
		if(str[i]==83||str[i]==115) str[i]-=18;
		else 
		if(str[i]==84||str[i]==116) str[i]-=5;
		else 
		if(str[i]==85||str[i]==117) str[i]-=17;
		else 
		if(str[i]==86||str[i]==118) str[i]-=6;
		else 
		if(str[i]==87||str[i]==119) str[i]-=20;
		else
		if(str[i]==88||str[i]==120) str[i]-=7;
		else
		if(str[i]==89||str[i]==121) str[i]-=21;
		else
		if(str[i]==90|str[i]==122) str[i]-=8;
		
		printf("%c",str[i]);
	    i++;
		jiemi(str);
		} 
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语言编写,欢迎扔板砖 //移位算法 #include #include #define SIZE 50 int main() { //i 用于计数输入个数,j 为临时变量, plain 存放明文, cipher 存放密文,decryption存放解密后文本,fpp 为明文文件指针,fpc 为密文文件指针 int i,j; char plain[SIZE],cipher[SIZE],decryption[SIZE],ciphertext[SIZE]; FILE * fpp,* fpc,* fpd; //加密 //建立新的明文TXT文件 printf("Caesar algorithm\n"); if((fpp=fopen("plain.txt","w+"))==NULL) { printf("creat new plain file error!\n"); exit(0); } //输入明文 printf("input plain alphabet:\n"); i=0; scanf("%c",&plain[i]); while(plain[i]!='\n'&&i<SIZE) { i++; scanf("%c",&plain[i]); } printf("success input %d characters\n",i); //将明文转存到文件中 for(j=0;j<i;j++) { if(fwrite(&plain[j],sizeof(char),1,fpp)!=1) { printf("saving plain file error!\n"); exit(0); } } printf("success saving plain text!\n"); //加密 for(j=0;j<i;j++) { cipher[j]=plain[j]+3; if(cipher[j]99) { printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>122) { cipher[j]=cipher[j]%122+96; printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>90) { cipher[j]=cipher[j]%90+64; printf("cipher %d = %c\n",j,cipher[j]); } else { printf("cipher %d = %c\n",j,cipher[j]); } } //建立密文文件 if((fpc=fopen("cipher.txt","w+"))==NULL) { printf("create new cipher file error!"); exit(0); } for(j=0;j<i;j++) { if(fwrite(&cipher[j],sizeof(char),1,fpc)!=1) { printf("saving cipher file error!"); exit(0); } } printf("success saving cipher file!"); printf("\n"); //解密 printf("input ciphertext alphabet:\n"); i=0; scanf("%c",&ciphertext[i]); while(ciphertext[i]!='\n'&&i<SIZE) { i++; scanf("%c",&ciphertext[i]); } for(j=0;j<i;j++) { decryption[j]=ciphertext[j]-3; if(decryption[j]90&&decryption[j]<97) { decryption[j]=123-(97-decryption[j]); printf("character %d = %c\n",j,decryption[j]); } else {

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值