C语言设计,替换密码开发设计,9月信息安全课程作业分享
#include<stdio.h>#include<string.h>#include<stdlib.h>#include <ctype.h>#define N 1000void encrypt();void decrypt();void decryptAll();int key() {char k[N];printf("输入密钥(1~26):");gets(k);while (1) {if (atoi(k) > 0 && atoi(k) <= 26)break;printf("格式错误,输入密钥(1~26):");gets(k);}return atoi(k);}void change(char code[],char code2[] ,int n) {char min[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 max[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'};int k,m;for (int i = 0; i < strlen(code); i++) {if('a'<=code[i] && code[i]<='z'){k=code[i]-'a';m=(26+k-n)%26;code2[i]=min[m];}else if('A'<=code[i] && code[i]<='Z'){k=code[i]-'A';m=(26+k-n)%26;code2[i]=max[m];}else{code2[i]=code[i];}}}void encrypt() {char min[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 max[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 code[N] = {};//接受字符串 int n,k,m;getchar();printf("请输入要加密的内容:");gets(code);n = key();//获得加密顺序的密钥for (int i = 0; i < strlen(code); i++) {if('a'<=code[i] && code[i]<='z'){k=code[i]-'a';m=(26+k+n)%26;code[i]=min[m];}else if('A'<=code[i] && code[i]<='Z'){k=code[i]-'A';m=(26+k+n)%26;code[i]=max[m];}else{code[i]=code[i];} }printf("加密之后的内容为:");puts(code);}void decrypt() {char code[N] = {}, code2[N] = {};int k;getchar();printf("输入密文:");gets(code);k = key(); change(code,code2, k);printf("解密的内容为:");puts(code2);}void decryptAll() {char code[N] = {},code2[N]={};getchar();printf("输入密文:");gets (code);for (int k = 1; k <= 26; ++k) { change(code,code2, k); printf("当key = %d时,解密是:", k);puts(code2);}}int main() {int x;while (1) { printf("\n\n\t================= 欢迎使用凯撒密码加密系统 =============\n\n");printf("\t=====================请选择服务=====================\n");printf("\t**\n");printf("\t* 1 加密 2 解密 *\n");printf("\t**\n");printf("\t* 3 暴力解密4 退出 *\n");printf("\t====================================================\n");printf("\t\t\t请作出选择(1-4):\n"); scanf("%d", &x);if (x == 1)encrypt();else if (x == 2)decrypt();else if (x == 3)decryptAll();else if(x==4){printf("已退出,谢谢使用\n");printf("欢迎您下次使用,凯撒密码加密系统时刻在。"); break; }else{ printf("警告输入错误!!!\n");} }}
如果觉得《凯撒密码加密 解密 暴力破解(C语言实现)》对你有帮助,请点赞、收藏,并留下你的观点哦!