目录
🚩write in front🚩
Page
⒈题目内容
⒉题目要求
⒊程序的加密 & 解密
方案①
方案②
⒋程序代码
Code①
Code②
⒌代码运行视频
⒍总结
在设计程序的时候为了防止一些敏感信息倍泄漏的时候,通常需要对这些信息进行加密的时候,以用户的的登录密码为例,如果密码以明文(密码)的形式存储在数据表当中,就会很容易被人发现。相反,如果密码以密文的形式进行存储的话,即使别人从数据表当中发现了密码,这也是加密之后的密码。
设计一个主函数[main]
循环语句设置一个无限循环。
声明两组数组分别用来存放加密字符(encypt_str)和解密字符(decode_str)
用户进行某一个操作需要输入一个命令,如果命令输入错误,系统会进行提示(设计菜单)
当用户输入命令字符"0"要求用户进行输入加密的字符。
当用户输入命令字符"1"会显示加密字符新的加密字符。
当用户输入命令字符"2"会对刚加密的文件来进行解密。
当用户输入命令字符"3"退出当前的程序设计应用程序。
加密⇢这里我们可以设置两种不同的加密方法供大家参考选择如下所示[↓]
¹将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"1"开始)+偏移量⒌
²将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"1"开始)+随机值(1~10)
拓展知识点⇢你也可以在上面原有的基础上进行优化哟(●'◡'●)
¹将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"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;
}
²将字符串中每个字符加上它在字符所在的位置(默认最开始的字符为"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;
}
运行结果程序设计加密&解密
说明↠方案二和方案一只是会显示加密字符新的加密字符功能不同其它一样。
总结⇨在上述程序对于初学者来说可能会有一定的难度,难度实际上并不是代码的本身。而是有很多库的函数需要我们去了解要学会怎么去使用他们,对于初学者来说是一个不错的练习的应用
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有