c语言凯撒密码前后端代码

时间: 2023-09-24 12:09:11 浏览: 53
Sure! Here's an example of the Caesar Cipher encryption and decryption code in C language: ```c #include <stdio.h> // Function to encrypt the message using Caesar Cipher void encrypt(char message[], int key) { char ch; int i; for(i = 0; message[i] != '\0'; ++i) { ch = message[i]; if(ch >= 'a' && ch <= 'z') { ch = ch + key; if(ch > 'z') { ch = ch - 'z' + 'a' - 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z') { ch = ch + key; if(ch > 'Z') { ch = ch - 'Z' + 'A' - 1; } message[i] = ch; } } printf("Encrypted message: %s\n", message); } // Function to decrypt the message using Caesar Cipher void decrypt(char message[], int key) { char ch; int i; for(i = 0; message[i] != '\0'; ++i) { ch = message[i]; if(ch >= 'a' && ch <= 'z') { ch = ch - key; if(ch < 'a') { ch = ch + 'z' - 'a' + 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z') { ch = ch - key; if(ch < 'A') { ch = ch + 'Z' - 'A' + 1; } message[i] = ch; } } printf("Decrypted message: %s\n", message); } int main() { char message[100]; int key; printf("Enter a message: "); scanf("%[^\n]s", message); printf("Enter key: "); scanf("%d", &key); encrypt(message, key); decrypt(message, key); return 0; } ``` In this code, the `encrypt` function takes a message and a key as input and encrypts the message using the Caesar Cipher algorithm. The `decrypt` function takes an encrypted message and the same key as input and decrypts the message. The `main` function prompts the user to enter a message and a key, and then calls the `encrypt` and `decrypt` functions accordingly. Finally, it prints the encrypted and decrypted messages. Please note that this is a basic implementation of the Caesar Cipher and may not include error handling or additional features.

相关推荐

最新推荐

recommend-type

餐馆点菜系统C语言源代码

主要为大家详细介绍了餐馆点菜系统C语言源代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

CRC4的C语言实现代码

这里给大家分享一个函数,实现DSP通讯中的CRC4校验 。特征码为0x13。
recommend-type

C语言计算代码执行所耗CPU时钟周期

本文给大家介绍的是使用C语言来计算代码执行所耗CPU时钟周期的代码,非常的简单实用,不过要依托于sync,有需要的小伙伴自己参考下吧。
recommend-type

codeblocks五子棋c语言代码.docx

这是一款在codeblocks环境下,使用consolo application实现的小型五子棋游戏,使用语言为c,这家伙这么老了,现在我们老师竟然还要让我们用这个编游戏做课程设计,网上啥资源的没有,编的我想吐,上传这个是为了帮...
recommend-type

密码学 凯撒密码算法实验C语言

通过实验熟练掌握凯撒密码算法,学会凯撒密码算法程序设计,提高C++程序设计能力。 二、实验要求 (1)输入:任意的一段明文 (2)输出:根据加密算法,实现对明文的加密 (3)要求有对应的程序调试记录和验证记录
recommend-type

STC89C51 简单时钟

STC89C51 简单时钟,叫你从基础开始学习单片机,
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB归一化方法大比拼:5种方法,哪种最适合你的数据?

![MATLAB归一化方法大比拼:5种方法,哪种最适合你的数据?](https://www.finebi.com/wp-content/uploads/2019/11/FineBI%E8%A1%8C%E4%B8%9A%E9%A9%BE%E9%A9%B6%E8%88%B1-1024x510.png) # 1. MATLAB归一化概述 归一化是一种数据预处理技术,用于将数据缩放到特定范围内,从而消除不同特征之间的尺度差异。在MATLAB中,有各种归一化方法可用于不同类型的数据和应用程序。 归一化的主要目的是: - 提高模型的训练效率和准确性,通过消除特征之间的尺度差异,使模型能够更有效地学习
recommend-type

failed to register layer: ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header

很抱歉听到这个问题。这个错误通常是由于Docker镜像损坏或Docker存储库中缺少所需的图层引起的。您可以尝试以下几种方法来解决这个问题: 1. 清理Docker镜像和容器:使用命令“docker system prune”清理不再使用的Docker对象。 2. 检查Docker存储库:确保您正在使用可靠的Docker存储库,并检查是否存在所有必需的图层。 3. 重新下载Docker镜像:如果您确定问题是由于损坏的Docker镜像引起的,则可以尝试重新下载Docker镜像。 4. 更新Docker版本:如果您使用的是旧版Docker,则可能会出现此问题。尝试更新到最新版本的Docke
recommend-type

Linux系统常用操作命令大全手册

附件是Linux系统常用操作命令大全手册,是 markdown格式,其中覆盖了Linux系统管理、文件操作、网络配置等多个方面,都是日常工作中非常常用的命令,欢迎大家下载学习使用!