你好,我需要一段c语言写的代码,用来给txt文件里的结构体中定义为账号和密码的部分进行加密写入,并要求可以解密展示在屏幕上
时间: 2024-05-20 09:19:33 浏览: 21
非常高兴为您服务!以下是对您问题的回答:
这个问题可以成为一个技术问题,因此我可以为您提供解决方案。可以使用C语言中的加密算法来加密账号和密码,比如MD5、SHA-1等算法。加密后的数据可以写入文件中。当需要显示账号和密码时,可以通过解密算法将数据解密并显示在屏幕上。但是请注意,确保对加密数据进行安全管理,以保护用户隐私。
希望这个回答对您有所帮助!
相关问题
你好,我需要一段c语言写的代码,用来给txt文件里的结构体的部分信息进行加密写入,并要求可以解密展示在屏幕上
你好!以下是一段C语言代码,可以将txt文件中结构体的部分信息进行加密,并且可以解密并展示在屏幕上。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char name[20];
int age;
float grade;
};
void encryptString(char* str) {
int length = strlen(str);
for (int i = 0; i < length; i++) {
str[i] += 1;
}
}
void decryptString(char* str) {
int length = strlen(str);
for (int i = 0; i < length; i++) {
str[i] -= 1;
}
}
void encryptStruct(struct Student* stud) {
encryptString(stud->name);
stud->age++;
stud->grade *= 1.5;
}
void decryptStruct(struct Student* stud) {
decryptString(stud->name);
stud->age--;
stud->grade /= 1.5;
}
int main() {
struct Student stud;
char fileName[20];
FILE* fp;
printf("Enter the file name: ");
scanf("%s", fileName);
fp = fopen(fileName, "a+");
if (fp == NULL) {
printf("File open failed!");
exit(1);
}
printf("Enter the student's name: ");
scanf("%s", stud.name);
printf("Enter the student's age: ");
scanf("%d", &stud.age);
printf("Enter the student's grade: ");
scanf("%f", &stud.grade);
encryptStruct(&stud);
fwrite(&stud, sizeof(struct Student), 1, fp);
printf("Information encrypted and written to file.\n");
rewind(fp);
fread(&stud, sizeof(struct Student), 1, fp);
decryptStruct(&stud);
printf("Decrypted information:\nName: %s\nAge: %d\nGrade: %.2f\n", stud.name, stud.age, stud.grade);
fclose(fp);
return 0;
}
```
在这段代码中,我们定义了一个`Student`结构体,其中包含了学生的姓名、年龄和成绩。然后,我们定义了一组加密和解密函数,可以对字符串和结构体中的信息进行加密和解密。在`main`函数中,我们将信息写入文件中,并对其中的姓名、年龄和成绩进行加密。我们然后重新打开文件,读取其中的信息,并对其进行解密。最后,我们展示解密后的信息。
编写一个C语言程序,将一段文字写入磁盘文件《姓名》.dat保存起来。要求文件的第一部分保存文件的结构(含密钥的变形码),第二部分是文字用密钥加密的密文。文件被重新打开时要求输入密码并验证密码并限定密码的输入次数。
好的,这是一个比较复杂的程序,需要分段来实现。首先是文件的创建和结构体的定义:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义结构体,保存文件结构信息
struct file_info {
char name[20]; // 文件名
char password[20]; // 密码
int key; // 密钥
int data_offset; // 数据偏移量
};
// 定义加密函数,将明文加密为密文
void encrypt(char *plain, char *cipher, int key) {
int len = strlen(plain);
for (int i = 0; i < len; i++) {
cipher[i] = plain[i] + key;
}
}
// 定义解密函数,将密文解密为明文
void decrypt(char *cipher, char *plain, int key) {
int len = strlen(cipher);
for (int i = 0; i < len; i++) {
plain[i] = cipher[i] - key;
}
}
int main() {
// 定义文件指针和结构体变量
FILE *fp;
struct file_info info;
// 输入文件名和密码
printf("请输入文件名(不超过20个字符):");
scanf("%s", info.name);
printf("请输入密码(不超过20个字符):");
scanf("%s", info.password);
// 生成随机密钥
info.key = rand() % 256;
// 打开文件并写入结构体
fp = fopen(strcat(info.name, ".dat"), "wb");
fwrite(&info, sizeof(struct file_info), 1, fp);
// 输入明文并加密写入文件
char plain[100], cipher[100];
printf("请输入要加密的文字:");
scanf("%s", plain);
encrypt(plain, cipher, info.key);
info.data_offset = ftell(fp);
fwrite(cipher, strlen(cipher) + 1, 1, fp);
// 关闭文件
fclose(fp);
return 0;
}
```
接下来是文件的读取和密码验证:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义结构体,保存文件结构信息
struct file_info {
char name[20]; // 文件名
char password[20]; // 密码
int key; // 密钥
int data_offset; // 数据偏移量
};
// 定义加密函数,将明文加密为密文
void encrypt(char *plain, char *cipher, int key) {
int len = strlen(plain);
for (int i = 0; i < len; i++) {
cipher[i] = plain[i] + key;
}
}
// 定义解密函数,将密文解密为明文
void decrypt(char *cipher, char *plain, int key) {
int len = strlen(cipher);
for (int i = 0; i < len; i++) {
plain[i] = cipher[i] - key;
}
}
int main() {
// 定义文件指针和结构体变量
FILE *fp;
struct file_info info;
// 输入文件名和密码
printf("请输入要打开的文件名(不超过20个字符):");
scanf("%s", info.name);
// 打开文件并读取结构体
fp = fopen(strcat(info.name, ".dat"), "rb");
fread(&info, sizeof(struct file_info), 1, fp);
// 输入密码并验证
int tries = 3;
while (tries--) {
printf("请输入密码(不超过20个字符):");
char input_password[20];
scanf("%s", input_password);
if (strcmp(info.password, input_password) == 0) {
break;
}
printf("密码错误,还有%d次机会\n", tries);
}
if (tries == -1) {
printf("密码输入错误次数过多,程序退出\n");
return 0;
}
// 读取密文并解密
char cipher[100], plain[100];
fseek(fp, info.data_offset, SEEK_SET);
fread(cipher, 1, 100, fp);
decrypt(cipher, plain, info.key);
printf("解密后的文字为:%s\n", plain);
// 关闭文件
fclose(fp);
return 0;
}
```
注意,为了保证密码输入次数的限制,我使用了一个 `tries` 变量,记录还有几次机会。如果在三次机会内输入正确的密码,则跳出循环;否则,循环结束后 `tries` 的值将变为 -1,表示密码输入错误次数过多,程序退出。
以上就是我的实现,希望能对你有所帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)