当前位置: 首页 > article >正文

【C语言】程序设计加密解密

  • 🚩write in front🚩   

  • 🔎 介绍:"謓泽"正在路上朝着"攻城狮"方向"前进四" 🔎
  • 🏅 荣誉:2021|2022年度博客之星物联网与嵌入式开发TOP5|TOP4、2021|2222年获评百大博主、华为云享专家、阿里云专家博主、掘金优秀创作者、全网粉丝量7w+、个人社区人数累计4w+、全网访问量100w+🏅
  • 🆔 本文章内容由 謓泽 原创 如需相关转载请提前告知博主 ⚠
  • 📑 创作时间:2022 年 2 月 22 日 📅
  • 📝 个人主页:謓泽的博客 📃
  • 📣 专栏系列:YY_謓泽的博客📃
  • 🙌 Gitee:謓泽 (wsxsx) - Gitee.com ⭐️
  • 🎁 点赞👍+ 收藏⭐️+ 留言📝​
  • ✉️ 我们并非登上我们所选择的舞台,演出并非我们所选择的剧本 📩

目录

🚩write in front🚩   

Page

⒈题目内容

⒉题目要求

⒊程序的加密 & 解密

方案①

方案② 

⒋程序代码 

Code①

Code② 

⒌代码运行视频

⒍总结

Page

在设计程序的时候为了防止一些敏感信息倍泄漏的时候,通常需要对这些信息进行加密的时候,以用户的的登录密码为例,如果密码以明文(密码)的形式存储在数据表当中,就会很容易被人发现。相反,如果密码以密文的形式进行存储的话,即使别人从数据表当中发现了密码,这也是加密之后的密码。

⒈题目内容

设计一个主函数[main]

循环语句设置一个无限循环

声明两组数组分别用来存放加密字符(encypt_str)解密字符(decode_str)

⒉题目要求

用户进行某一个操作需要输入一个命令,如果命令输入错误,系统会进行提示(设计菜单)

当用户输入命令字符"0"要求用户进行输入加密的字符

当用户输入命令字符"1"会显示加密字符新的加密字符

当用户输入命令字符"2"会对刚加密的文件来进行解密

当用户输入命令字符"3"退出当前的程序设计应用程序

⒊程序的加密 & 解密

加密⇢这里我们可以设置两种不同的加密方法供大家参考选择如下所示[↓]

方案①

¹将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"1"开始)+偏移量⒌

方案② 

²将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"1"开始)+随机值(1~10)

拓展知识点⇢你也可以在上面原有的基础上进行优化哟(●'◡'●)

⒋程序代码 

Code①

¹将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"1"开始)+偏移量⒌

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<Windows.h>
#include<string.h>
unsigned int g_Count = 0;
void color(short x)
{
	if (x >= 0 && x <= 15)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
	else
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void menu()
{
	color(0);
	system("cls");
	color(1);
	printf("|-------★<<<<<<<   加密&解密   >>>>>>> ★------|\n");
	color(10);
	printf("|-------★ 0.要求用户进行输入加密的字符 ★------|\n");
	printf("|-------★ 1.会显示加密字符新的加密字符 ★------|\n");
	printf("|-------★ 2.会对刚加密的文件夹进行解密 ★------|\n");
	printf("|-------★ 3.退出当前的程序设计应用程序 ★------|\n");
}
enum Commond_str
{
	Encryption = 0,
	New_Encryption = 1,
	Decode = 2,
	Exit = 3,
};
/*
	会显示加密字符新的加密字符
	方案一功能:将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"1"开始)+偏移量⒌
*/
void new_Encryption(int Count, char* len, char* decode_str)
{
	int i = 0;
	Count = strlen(len);
	for (i = 0; i < Count; i++)
	{
		decode_str[i] = len[i] + i + 5;
	}
	printf("%s\n", decode_str);
}
int main(void)
{
	unsigned int Count = 0, Commond = 0;
	char password[10] = { 0 };
	char encypt_str[20] = { 0 };//加密字符
	char decode_str[40] = { 0 };//解密字符
	int numbers = 0;
	while (g_Count < 3)
	{
		printf("No.%d:如需要帮助请输入[help]->", g_Count + 1);
		scanf("%s", password);
		if (strcmp("help", password) == 0)
		{
			menu();
			break;
		}
		else
			printf("%d.Your input !(help)", g_Count + 1);
		printf("\n");
		if (g_Count == 3)
		{
			printf("Fool Your str error!exit");
			break;
		}
		g_Count++;
	}
	g_Count = 1;
	while (1)
	{
		printf("No.%d:Please input Commond:", g_Count++);
		scanf("%d", &Commond);
		switch (Commond)
		{
		case Encryption:scanf("%s", encypt_str); printf("Your input encypt_str:%s\n", encypt_str); break;
		case New_Encryption:new_Encryption(Count, encypt_str, decode_str); break;//第一种方案
		case Decode:printf("encypt_str:%s\n", encypt_str); break;//注:解密以后的字符就是加密
		case Exit:printf("Exit:kk提醒您~\n"); break;
		}
		if (Commond == Exit)
			break;
	}
	return 0;
}

Code② 

²将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"1"开始)+随机值(1~10)

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<Windows.h>
#include<string.h>
//随机值需要的两种库函数头文件
#include<time.h>
#include<stdlib.h>
unsigned int g_Count = 0;
void color(short x)
{
	if (x >= 0 && x <= 15)
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
	else
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void menu()
{
	color(0);
	system("cls");
	color(1);
	printf("|-------★<<<<<<<   加密&解密   >>>>>>> ★------|\n");
	color(10);
	printf("|-------★ 0.要求用户进行输入加密的字符 ★------|\n");
	printf("|-------★ 1.会显示加密字符新的加密字符 ★------|\n");
	printf("|-------★ 2.会对刚加密的文件夹进行解密 ★------|\n");
	printf("|-------★ 3.退出当前的程序设计应用程序 ★------|\n");
}
enum Commond_str
{
	Encryption = 0,
	New_Encryption = 1,
	Decode = 2,
	Exit = 3,
};
/*
	会显示加密字符新的加密字符
	方案二功能:将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"1"开始)+随机值(1~10)
*/
void new_Encryption(int Count, char* len, char* decode_str,int randNumber)
{
	int i = 0;
	Count = strlen(len);
	for (i = 0; i < Count; i++)
	{
		decode_str[i] = len[i] + i + randNumber;
	}
	printf("%s\n", decode_str);
}
int main(void)
{
	srand((unsigned)time(NULL));
	int randNumber = rand() % 10 + 1;
	unsigned int Count = 0, Commond = 0;
	char password[10] = { 0 };
	char encypt_str[20] = { 0 };//加密字符
	char decode_str[40] = { 0 };//解密字符
	int numbers = 0;
	while (g_Count < 3)
	{
		printf("No.%d:如需要帮助请输入[help]->", g_Count + 1);
		scanf("%s", password);
		if (strcmp("help", password) == 0)
		{
			menu();
			break;
		}
		else
			printf("%d.Your input !(help)", g_Count + 1);
		printf("\n");
		if (g_Count == 3)
		{
			printf("Fool Your str error!exit");
			break;
		}
		g_Count++;
	}
	g_Count = 1;
	while (1)
	{
		printf("No.%d:Please input Commond:", g_Count++);
		scanf("%d", &Commond);
		switch (Commond)
		{
		case Encryption:scanf("%s", encypt_str); printf("Your input encypt_str:%s\n", encypt_str); break;
		case New_Encryption:new_Encryption(Count, encypt_str, decode_str,randNumber);break;//第二种方案
		case Decode:printf("encypt_str:%s\n", encypt_str); break;//注:解密以后的字符就是加密
		case Exit:printf("Exit:kk提醒您~\n"); break;
		}
		if (Commond == Exit)
			break;
	}
	return 0;
}

⒌代码运行视频

运行结果程序设计加密&解密

说明↠方案二和方案一只是会显示加密字符新的加密字符功能不同其它一样。

⒍总结

        总结⇨在上述程序对于初学者来说可能会有一定的难度,难度实际上并不是代码的本身。而是有很多库的函数需要我们去了解要学会怎么去使用他们,对于初学者来说是一个不错的练习的应用。


http://www.kler.cn/news/161077.html

相关文章:

  • mac M系列芯片安装chatGLM3-6b模型
  • js vue 输入正确手机号/邮箱后,激活“发送验证码”按钮
  • 详解线段树
  • C语言——指针的运算
  • LLM(五)| Gemini:谷歌发布碾压GPT-4最强原生多模态,语言理解能力首次超过人类
  • Java API接口强势对接:构建高效稳定的系统集成方案
  • java-HashMap、TreeMap、LinkedHashMap、ArrayList、LinkedList使用笔记
  • 什么是https 加密协议?https证书安装部署
  • 微信小程序复制功能
  • 如何通过内网穿透实现无公网IP也能远程访问内网的宝塔面板
  • C# WPF上位机开发(抽奖软件)
  • 【云原生系列】Kubernetes知识点
  • Python-字典详解
  • 一个简单的参数帮助框架,c实现
  • Android 架构实战MVI进阶
  • Vue3的watch函数中,第三个参数配置对象详细分析
  • uniapp横向滚动示例
  • JavaSE语法之五:数组的定义与使用(超详解!!!)
  • Android12之MediaCodec硬编解码调试手段(四十九)
  • python基于ModBusTCP服务端的业务实现特定的client
  • 反钓鱼防盗号,共筑校园安全防线!Coremail出席CERNET学术年会
  • 案例054:基于微信的追星小程序
  • 代码随想录算法训练营第四十二天 _ 动态规划_01背包问题、416.分割等和子集。
  • 医院有HIS系统,为什么还要开发预约挂号小程序?数据如何互通?
  • 前端笔记(三)CSS 盒子模型
  • ★538. 把二叉搜索树转换为累加树
  • PHP使用HTTP代码示例模板
  • gpt3、gpt2与gpt1区别
  • 深入理解 Java 虚拟机(JVM)从入门到精通
  • 使用GPT-4V解决Pycharm设置问题