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

【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"开始)+偏移量⒌

代码语言:javascript复制#define _CRT_SECURE_NO_WARNINGS 1

#include

#include

#include

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)

代码语言:javascript复制#define _CRT_SECURE_NO_WARNINGS 1

#include

#include

#include

//随机值需要的两种库函数头文件

#include

#include

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;

}⒌代码运行视频运行结果程序设计加密&解密

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

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

相关阅读

怎麼拓展交友圈?有效方法與技巧
久发365电子游戏网址多少

怎麼拓展交友圈?有效方法與技巧

🕒 09-23 👁️‍🗨️ 7995
反冲的力量——火箭是怎样升空的
365bet官方投注网址

反冲的力量——火箭是怎样升空的

🕒 10-19 👁️‍🗨️ 4954
久发365电子游戏网址多少

"屏幕无法连接主机:全面解析故障成因与解决方案"

🕒 07-16 👁️‍🗨️ 7045
问天然气表滴滴响是咋了
久发365电子游戏网址多少

问天然气表滴滴响是咋了

🕒 06-26 👁️‍🗨️ 6774
lol节日活动大全
久发365电子游戏网址多少

lol节日活动大全

🕒 07-22 👁️‍🗨️ 2178
天机城必然加强,再次分析技能可玩性
久发365电子游戏网址多少

天机城必然加强,再次分析技能可玩性

🕒 11-09 👁️‍🗨️ 4491
剝柚皮苦手必看!農業部傳授「剝柚三妙招」,輕鬆製作柚子盅、柚子帽~
SQLite 入门教程
365bet官网赌场

SQLite 入门教程

🕒 09-23 👁️‍🗨️ 8102
中美科技人才争夺战:为何美国半导体行业华人比例飙升?
365bet官网赌场

中美科技人才争夺战:为何美国半导体行业华人比例飙升?

🕒 10-26 👁️‍🗨️ 1866