《凯撒密码C语言实现》由会员分享,可在线阅读,更多相关《凯撒密码C语言实现(8页珍藏版)》请在人人文库网上搜索。

1、凯撒密码是一种非常古老的加密方法, 相传当年凯撒大地行军打仗时为了保证自己的命令不 被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。他的原理很简单, 说到底就是字母于字母之间的替换。下面让我们看一个简单的例子: “baidu”用凯撒密码法 加密后字符串变为“edlgx” ,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序 向后移 3 位,所得的结果就是刚才我们所看到的密文。 /*凯撒密码实现 要求,将明文字母变成它后面第三个字母,后面的循环到前面! 公式为 f(a)=(f(a)+3)%26 */ #include int main() char P100;/*定义明。

2、文长度*/ char C100;/*定义密文长度*/ int K=3,i; printf(Please input Plaintext:n); /*输入明文*/ gets(P); /* 接受明文*/ for(i=0;Pi!=0;i+) /*逐个判断字母的大小*/ if(Pi=a else Ci= ;/*如果不是字母,转换为空格*/ printf(The Ciphertext is :n%sn,C);/*输出密文*/ getch(); return 0; 1、程序结构化,用函数分别实现 2、对文件的加密,解密输出到文件 #include #include void menu()/*菜单,1.加密。

3、 2.解密 3.退出*/ clrscr(); printf(n= =); printf(n1.Encrypt the file); printf(n2.Decrypt the file); printf(n3.Quitn); printf(= =n); printf(Please select a item:); return; char encrypt(char ch,int n)/*加密函数,把字符向右循环移位 n*/ while(ch=A return ch; main() int i,n; char ch0,ch1; FILE *in,*out; char infile10,outfi。

4、le10; 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); prin。

5、tf(Please input the key:); scanf(%d,/*输入加密密码*/ 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); 。

6、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。

7、(%d,/*输入解密密码(可以为加密时候的密码)*/ 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。

8、 over!n); fclose(in); fclose(out); sleep(1); clrscr(); printf(nGood Bye!n); sleep(3); getch(); - - /*移位法:*/ #include #include char *Encrypt(char *pwd,int key) /*加密*/ for(int i=0;*(pwd+i)!=0;i+) if(*(pwd+i)=a else *(pwd+i)=z-(key%26-(*(pwd+i)-a)-1; else if(*(pwd+i)=A else *(pwd+i)=Z-(key%26-(*(pwd+i)。

9、-A)-1; return pwd; void main() char *pwd; int key; pwd=(char*)malloc(sizeof(char); printf(Input your password:); gets(pwd); printf(Input a key:); scanf(%d, printf(The Ciphertext is:); printf(%sn,Encrypt(pwd,key); - /*替换法:*/ #include #include #include void table(char *keyword) /*筛选密钥(去重复去空格)*/ int i,。

10、j,k; for(i=0;*(keyword+i)!=0;i+) for(j=i;*(keyword+j)!=0;j+) if(i!=j) if(*(keyword+i)=*(keyword+j)|*(keyword+j)= ) for(k=j;*(keyword+k)!=0;k+) *(keyword+k)=*(keyword+k+1); j-; void newTab(char *keyword) /*生成密钥表*/ char ch; int i; int t; for(t=0;*(keyword+t)!=0;t+); for(ch=a;ch=z;ch+) for(i=0;*(keywor。

11、d+i)!=ch;i+) if(*(keyword+i)=0) *(keyword+t)=ch; t+; break; *(keyword+t)=0; char *Ciphertext(char *keyword,char *Plaintext) /*按密码表加密*/ char ch; int i,j; for(i=0;*(Plaintext+i)!=0;i+) for(ch=a,j=0;ch=z;ch+,j+) if(*(Plaintext+i)=ch) *(Plaintext+i)=*(keyword+j); break; return Plaintext; char *Decrypt(c。

12、har *keyword,char *Plaintext) /*解密*/ char ch; int i,j; for(i=0;*(Plaintext+i)!=0;i+) for(ch=a,j=0;*(keyword+j)!=0;ch+,j+) if(*(Plaintext+i)=*(keyword+j) *(Plaintext+i)=ch; break; return Plaintext; void main() char *keyword,*Plaintext,*tmp=NULL; keyword=(char*)malloc(sizeof(char); Plaintext=(char*)malloc(sizeof(char); printf(Input key word:);/*输入欲用密钥*/ gets(keyword); printf(Input Plaintext:); /*输入要转换的明文*/ gets(Plaintext); table(keyword);/*去空格去重复*/ newTab(keyword); /*生成密码表*/ tmp=Ciphertext(keyword,Plaintext); /*对应着密码表生成密文*/ puts(tmp); /*输出密文*/ puts(Decrypt(keyword,tmp); /*解密输出*/。

凯撒密码C语言去掉空格字符,凯撒密码C语言实现相关推荐

  1. 凯撒密码C语言去掉空格字符,凯撒密码的问题C语言

    满意答案 xlaijing19 推荐于 2016.04.01 采纳率:43%    等级:12 已帮助:6257人 最低0.27元开通文库会员,查看完整内容> 原发布者:耿万德 凯撒密码是一种非 ...

  2. 如何用c语言去掉空格

    在 C 语言中,可以使用以下方法去掉空格: 使用 scanf 和 %[^ ] 的格式字符: #include <stdio.h>int main(void) {char input[100 ...

  3. c语言中空格字符怎么表示_漫画:腾讯面试题,请实现把字符串中的空格替换为“%20”...

    面试现场 题目描述请实现一个函数,将一个字符串中的每个空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. i ...

  4. 由于c语言是由字符流组成的,C语言试题及答案

    大学C语言考试题库 第1章 C语言概述习题 1. 单项选择题 (1) C 语言是在 B 语言的基础上产生的. A. A B. B C. D D. E (2) 在 C 语言中,每个语句必须以 D 结束. ...

  5. c语言输出换行字符,float_printf格式换行_c语言printf里如何换行

    信息从计算机的外部设备流入计算机称为输入.printf格式换行 从计算机主机流向外部设备称为输出. C语言中没有输入输出语句,而是由输入输出函来实现的输入/输出的. C标准函数库中包含的常用输入输出函 ...

  6. c语言中空格字符怎么表示_C语言中常用的字符串操作函数

    作者:陈太浪 出处:https://home.cnblogs.com/u/TomHe789/ C语言中提供了许多的字符串操作函数,常见的字符串操作函数有以下几种: 1.求字符串长度的函数 原型函数:s ...

  7. c语言中合法的字符型常量是,C语言习题库(带答案)-排版.doc

    一.选择题 1.以下不能定义为用户标识符的是( ). (A) MAIN (B) _HJ (C) 2ong (D) LINE1 2.以下不能定义为用户标识符的是( ) (A) If (B) H_J (C ...

  8. c语言用空格分隔和用逗号分隔,c语言程序设计课呵件第02章简单程序设计.ppt

    c语言程序设计课呵件第02章简单程序设计 第二章 C++简单程序设计 本章主要内容 C++语言概述 基本数据类型和表达式 数据的输入与输出 算法的基本控制结构 自定义数据类型 C++语言的产生 C++ ...

  9. c语言长空格的代码是什么,c语言中表示空格的是什么代码?

    分析如下: 不是所有字符都需要转义的,空格直接就敲空格,或者使用ASCII码值赋值为32. 空格没有转义字符.合法转义字符如下: \a 响铃(BEL) .\b 退格(BS).\f 换页(FF).\n ...

最新文章

  1. 【ASP.NET Core】依赖注入高级玩法——如何注入多个服务实现类
  2. iOS UITableView
  3. 2007年度最具投资价值100强网站揭晓——博客园榜上有名
  4. MySQL 笔记2 -- MySQL 基础
  5. SQLServer之PRIMARY KEY约束
  6. nagios监控mysql主机,nginx,cpu,网卡流量
  7. vilatile 深入理解java虚拟机_深入理解Java虚拟机(jvm性能调优+内存模型+虚拟机原理)...
  8. python分词统计词频_python jieba分词并统计词频后输出结果到Excel和txt文档方法
  9. linux运行不了.sh文件,linux下不能执行/bin/sh脚本的原因:command not found
  10. PHP nodejs session,Nodejs中session的简单使用及通过session实现身份验证的方法
  11. SQL 错误: ORA-12910
  12. 阿里云Web播放器使用
  13. 成神之路——实施工程师
  14. 扫码下载apk文件浏览器会直接打开并显示乱码的问题
  15. DM642的PCI驱动编程笔记:遍历一块内存空间的源码
  16. 小蝌蚪找妈妈(召唤神龙)源码
  17. 2006考研阅读Text2翻译
  18. python中查看相对路径_python提取相对路径
  19. linux脚本除号,Shell脚本编程(上)
  20. 函数的节流(throttle)

热门文章

  1. 淘宝产品详情页 上拉加载图片详情 效果实现
  2. N1刷openwrt,部分app图片无法显示
  3. IT项目管理个人作业05
  4. 当前版本与卡刷包android_手机端提取Android 8.x及以上版本卡刷包中的system文件
  5. 怎么让笔记本合上后显示屏不灭
  6. 从时间管理到管理情绪,这些自我管理的技巧你知道几个?
  7. C#简易播放器(WindowsMediaPlayer)
  8. 北京市中小学信息学竞赛汇总
  9. ups不间断电源品牌_德国阳光蓄电池_蓄电池代理-山东万仁电源设备有限公司
  10. 没有windows安装光盘怎么修复计算机,电脑没有u盘光盘重装windows7系统的方法步骤教程 - 系统家园...