如何用C语言编写程序实现通用文件的加密与解密操作?

2026-05-08 18:453阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计940个文字,预计阅读时间需要4分钟。

如何用C语言编写程序实现通用文件的加密与解密操作?

本例展示了如何使用C语言实现文件加密和解密功能。以下是一个简单的示例代码,用于演示如何使用命令提示符提示用户输入,并实现文件的加密和解密。

c#include #include #include

#define KEY secretkey

void encrypt(char *input, char *output, const char *key) { FILE *in=fopen(input, rb); FILE *out=fopen(output, wb); if (!in || !out) { printf(Error opening files.\n); return; }

char ch; while ((ch=fgetc(in)) !=EOF) { ch=ch ^ (key[strlen(key) - 1]); fputc(ch, out); }

fclose(in); fclose(out);}

void decrypt(char *input, char *output, const char *key) { FILE *in=fopen(input, rb); FILE *out=fopen(output, wb); if (!in || !out) { printf(Error opening files.\n); return; }

char ch; while ((ch=fgetc(in)) !=EOF) { ch=ch ^ (key[strlen(key) - 1]); fputc(ch, out); }

fclose(in); fclose(out);}

如何用C语言编写程序实现通用文件的加密与解密操作?

int main() { char choice; char input[100], output[100];

printf(Enter the file name to encrypt: ); scanf(%99s, input); printf(Enter the output file name: ); scanf(%99s, output);

encrypt(input, output, KEY);

printf(Do you want to decrypt the file? (y/n): ); scanf( %c, &choice); // 注意前面的空格,用于跳过前面的换行符

if (choice=='y' || choice=='Y') { decrypt(output, decrypted_output.txt, KEY); printf(Decryption successful. Output file: decrypted_output.txt\n); }

return 0;}

这段代码首先定义了一个简单的加密和解密函数,使用异或运算和固定的密钥进行加密和解密。主函数中,程序会提示用户输入要加密的文件名和输出文件名,然后调用加密函数。如果用户选择解密,程序会调用解密函数并输出解密后的文件名。

本文实例为大家分享了C语言实现文件加密解密功能的具体代码,供大家参考,具体内容如下

使用命令提示符,实现任何文件的加密和解密功能。

代码如下:

//#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include<time.h> #include<sys/stat.h> #pragma warning(disable:4996) //加密 void Encryption(char *p, size_t n) { for (int i = 0; i < n;++i) { *p += 7; } } //解密 void Decrypt(char *p, size_t n) { for (int i = 0; i < n;++i) { *p -= 7; } } char *EnOrDe = {0}; int main(int argc, char *args[]) { clock_t c1 = clock();//系统当前时间,毫秒为单位 char *FileNameSrc = (char *)calloc(160, sizeof(char));//待操作文件 char *p2 = (char *)calloc(200, sizeof(char));//操作后文件名 //从命令行获取文件名和要处理的操作 FileNameSrc = args[1];//文件名,包含路径 char *p1 = args[1]; EnOrDe = args[2];//en表示加密,de表示解密 /*FileNameSrc = "E:\\iPhone6-new.txt" ; char *p1 = FileNameSrc; EnOrDe = "de";*/ /**********处理生成新的文件名***********/ //char *p2 = { 0 }; /*FileNameSrc = p1;*/ //printf("%s\n", FileNameSrc); //printf("%s\n", EnOrDe); int index = 0; while (*p1) { if (*p1!='.') { *p2 = *p1; p2++; p1++; index++; } else if (*p1 == '.') { *p2 = '_'; p2++; *p2 = 'H'; p2++; *p2 = '.'; p2++; p1++; index+=2; } } printf("\n"); printf("信息摘要:\n"); printf("--------------------------------------\n"); printf("原文件:%s\n", FileNameSrc); printf("操作:%s (en——加密,de——解密)\n", EnOrDe); printf("预计结果文件:%s\n", p2 - index - 1); printf("--------------------------------------\n\n"); printf("请稍后,玩命处理中......\n"); char *FileNameDst = p2 - index - 1; FILE *pr = fopen(FileNameSrc, "rb"); FILE *pw = fopen(FileNameDst, "wb"); struct stat st = { 0 }; size_t fileSize = st.st_size;//以字节为单位 //char *buf = NULL; //if (fileSize<1024*1024)//小于1M //{ // buf = malloc(sizeof(char) * 1024 * 20);//分配20K //} //else //{ // buf = malloc(sizeof(char)*fileSize / 10); //} char *buf = calloc(1024 * 1024 * 25, sizeof(int));//分配100M /*************定义函数指针***************/ void(*pFunc)(char *, size_t); pFunc = NULL; if (strcmp(EnOrDe, "en") == 0) { pFunc = Encryption; } else if (strcmp(EnOrDe, "de") == 0) { pFunc = Decrypt; } /*************定义函数指针***************/ while (!feof(pr)) { //memset(buf, 0, sizeof(buf));//calloc自动初始化为0 size_t res = fread(buf, sizeof(char), sizeof(buf), pr); pFunc(buf, res); fwrite(buf, sizeof(char), res, pw); } fclose(pr); fclose(pw); printf("\n"); printf("--------------------------------------\n"); printf("执行成功!\n所在目录:%s\n", FileNameDst); clock_t c2 = clock();//系统当前时间,毫秒为单位 printf("耗时:%u毫秒\n", c2-c1); printf("--------------------------------------\n"); return 0; }

效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计940个文字,预计阅读时间需要4分钟。

如何用C语言编写程序实现通用文件的加密与解密操作?

本例展示了如何使用C语言实现文件加密和解密功能。以下是一个简单的示例代码,用于演示如何使用命令提示符提示用户输入,并实现文件的加密和解密。

c#include #include #include

#define KEY secretkey

void encrypt(char *input, char *output, const char *key) { FILE *in=fopen(input, rb); FILE *out=fopen(output, wb); if (!in || !out) { printf(Error opening files.\n); return; }

char ch; while ((ch=fgetc(in)) !=EOF) { ch=ch ^ (key[strlen(key) - 1]); fputc(ch, out); }

fclose(in); fclose(out);}

void decrypt(char *input, char *output, const char *key) { FILE *in=fopen(input, rb); FILE *out=fopen(output, wb); if (!in || !out) { printf(Error opening files.\n); return; }

char ch; while ((ch=fgetc(in)) !=EOF) { ch=ch ^ (key[strlen(key) - 1]); fputc(ch, out); }

fclose(in); fclose(out);}

如何用C语言编写程序实现通用文件的加密与解密操作?

int main() { char choice; char input[100], output[100];

printf(Enter the file name to encrypt: ); scanf(%99s, input); printf(Enter the output file name: ); scanf(%99s, output);

encrypt(input, output, KEY);

printf(Do you want to decrypt the file? (y/n): ); scanf( %c, &choice); // 注意前面的空格,用于跳过前面的换行符

if (choice=='y' || choice=='Y') { decrypt(output, decrypted_output.txt, KEY); printf(Decryption successful. Output file: decrypted_output.txt\n); }

return 0;}

这段代码首先定义了一个简单的加密和解密函数,使用异或运算和固定的密钥进行加密和解密。主函数中,程序会提示用户输入要加密的文件名和输出文件名,然后调用加密函数。如果用户选择解密,程序会调用解密函数并输出解密后的文件名。

本文实例为大家分享了C语言实现文件加密解密功能的具体代码,供大家参考,具体内容如下

使用命令提示符,实现任何文件的加密和解密功能。

代码如下:

//#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include<time.h> #include<sys/stat.h> #pragma warning(disable:4996) //加密 void Encryption(char *p, size_t n) { for (int i = 0; i < n;++i) { *p += 7; } } //解密 void Decrypt(char *p, size_t n) { for (int i = 0; i < n;++i) { *p -= 7; } } char *EnOrDe = {0}; int main(int argc, char *args[]) { clock_t c1 = clock();//系统当前时间,毫秒为单位 char *FileNameSrc = (char *)calloc(160, sizeof(char));//待操作文件 char *p2 = (char *)calloc(200, sizeof(char));//操作后文件名 //从命令行获取文件名和要处理的操作 FileNameSrc = args[1];//文件名,包含路径 char *p1 = args[1]; EnOrDe = args[2];//en表示加密,de表示解密 /*FileNameSrc = "E:\\iPhone6-new.txt" ; char *p1 = FileNameSrc; EnOrDe = "de";*/ /**********处理生成新的文件名***********/ //char *p2 = { 0 }; /*FileNameSrc = p1;*/ //printf("%s\n", FileNameSrc); //printf("%s\n", EnOrDe); int index = 0; while (*p1) { if (*p1!='.') { *p2 = *p1; p2++; p1++; index++; } else if (*p1 == '.') { *p2 = '_'; p2++; *p2 = 'H'; p2++; *p2 = '.'; p2++; p1++; index+=2; } } printf("\n"); printf("信息摘要:\n"); printf("--------------------------------------\n"); printf("原文件:%s\n", FileNameSrc); printf("操作:%s (en——加密,de——解密)\n", EnOrDe); printf("预计结果文件:%s\n", p2 - index - 1); printf("--------------------------------------\n\n"); printf("请稍后,玩命处理中......\n"); char *FileNameDst = p2 - index - 1; FILE *pr = fopen(FileNameSrc, "rb"); FILE *pw = fopen(FileNameDst, "wb"); struct stat st = { 0 }; size_t fileSize = st.st_size;//以字节为单位 //char *buf = NULL; //if (fileSize<1024*1024)//小于1M //{ // buf = malloc(sizeof(char) * 1024 * 20);//分配20K //} //else //{ // buf = malloc(sizeof(char)*fileSize / 10); //} char *buf = calloc(1024 * 1024 * 25, sizeof(int));//分配100M /*************定义函数指针***************/ void(*pFunc)(char *, size_t); pFunc = NULL; if (strcmp(EnOrDe, "en") == 0) { pFunc = Encryption; } else if (strcmp(EnOrDe, "de") == 0) { pFunc = Decrypt; } /*************定义函数指针***************/ while (!feof(pr)) { //memset(buf, 0, sizeof(buf));//calloc自动初始化为0 size_t res = fread(buf, sizeof(char), sizeof(buf), pr); pFunc(buf, res); fwrite(buf, sizeof(char), res, pw); } fclose(pr); fclose(pw); printf("\n"); printf("--------------------------------------\n"); printf("执行成功!\n所在目录:%s\n", FileNameDst); clock_t c2 = clock();//系统当前时间,毫秒为单位 printf("耗时:%u毫秒\n", c2-c1); printf("--------------------------------------\n"); return 0; }

效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。