加密文件

本文介绍了一个简单的文件加密解密程序实现,该程序通过用户输入的密码进行基本的加密和解密操作,并能将加密后的文件保存到指定路径。

#include<stdio.h>
#include<stdlib.h>
void main() {
 //复制一个文件,需要输入两个数据。  一个是被复制的文件,一个是需要复制到哪里。
 //加密程序其实已经完成了70%。
 char l_in_path[200] = { 0 };
 char l_out_path[200] = { 0 };
 char l_pass[50] = { 0 };
 int l_tools = NULL;

 printf("请输入源文件路径:");
 scanf("%s", l_in_path);
 printf("请输入新文件路径:");
 scanf("%s", l_out_path);
 printf("请输入密码:");
 scanf("%s", l_pass);

 printf("输入1为加密,输入2为解密:");
 scanf("%d", &l_tools);
 if (l_tools != 1 && l_tools != 2) {
  printf("功能输入选择错误\n");
  return;
 }
 FILE * l_fp_read = fopen(l_in_path, "rb");
 FILE * l_fp_write = fopen(l_out_path, "wb");

 if (l_fp_read != NULL && l_fp_write != NULL) {
  if (l_tools == 1) {
   char l_temp = fgetc(l_fp_read);
   while (feof(l_fp_read) == 0) {
    l_temp = l_temp + 40;
    fputc(l_temp, l_fp_write);
    l_temp = fgetc(l_fp_read);
   }
  }
  else {
   char l_temp = fgetc(l_fp_read);
   while (feof(l_fp_read) == 0) {
    l_temp = l_temp - 40;
    fputc(l_temp, l_fp_write);
    l_temp = fgetc(l_fp_read);
   }
  }


 }
 fclose(l_fp_read);
 fclose(l_fp_write);
 printf("已复制成功\n");
 system("pause");
}

 


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main() {
 //复制一个文件,需要输入两个数据。  一个是被复制的文件,一个是需要复制到哪里。
 //加密程序其实已经完成了70%。
 char l_in_path[200] = { 0 };
 char l_out_path[200] = { 0 };
 char l_pass[50] = { 0 };
 int l_tools = NULL;

 printf("请输入源文件路径:");
 scanf("%s", l_in_path);
 printf("请输入新文件路径:");
 scanf("%s", l_out_path);
 printf("请输入密码:");
 scanf("%s", l_pass);

 printf("输入1为加密,输入2为解密:");
 scanf("%d", &l_tools);
 if (l_tools != 1 && l_tools != 2) {
  printf("功能输入选择错误\n");
  return;
 }


 FILE * l_fp_read = fopen(l_in_path, "rb");
 FILE * l_fp_write = fopen(l_out_path, "wb");
 int l_length = strlen(l_pass);
 int l_count = 0;

 if (l_fp_read != NULL && l_fp_write != NULL) {
  if (l_tools == 1) {
   char l_temp = fgetc(l_fp_read);
   while (feof(l_fp_read) == 0) {
    l_temp = l_temp ^ l_pass[l_count];
    l_temp = l_temp + l_count;
    l_count++;
    if (l_count == l_length) {
     l_count = 0;
    }

    fputc(l_temp, l_fp_write);
    l_temp = fgetc(l_fp_read);
   }
  }
  else {
   char l_temp = fgetc(l_fp_read);
   while (feof(l_fp_read) == 0) {
    l_temp = l_temp - l_count;
    l_temp = l_temp ^ l_pass[l_count];
    l_count++;
    if (l_count == l_length) {
     l_count = 0;
    }
    fputc(l_temp, l_fp_write);
    l_temp = fgetc(l_fp_read);
   }
  }


 }
 fclose(l_fp_read);
 fclose(l_fp_write);
 if (l_tools == 1) {
  printf("已加密成功\n");
 }
 else {
  printf("已解密成功\n");
 }

 system("pause");
}

转载于:https://www.cnblogs.com/xiaodaxiaonao/p/8699680.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值