关于C++的简单文件加密解密

阅读: 评论:0

关于C++的简单文件加密解密

关于C++的简单文件加密解密

1.首先定义一个加密的口令
2.读取文件的内容,将读取到的每个字符循环遍历加上定义口令的ASCII值
3.再将加密后的文件内容覆盖回原本的文件

直接看代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream>
#include<iostream>
#include <cassert>
#include <list>using namespace std;void encfile(char *in_filename,char *pwd);
void decryptfile(char* in_filename,char *pwd);
void getFileContent(string str);string content;
string *content_array = new string[2048];struct generateInfo {string password;string checked;string username;string company;
};int main(int argc,char *argv[]) { char in_filename[30] = "e://";        char pwd[8] = "config";                   encfile(in_filename,pwd);decryptfile(in_filename,pwd);generateInfo info;info.checked = content_array[4];info.password = content_array[2];info.username = content_array[6];infopany = content_array[8];cout<< info.username <<endl;cout<< info.password <<endl;cout<< infopany <<endl;cout<< info.checked <<endl;return 0;
}/*加密函数*/
void encfile(char *in_filename,char *pwd) {FILE *fp1;register char ch;int j=0;int j0=0;string temp;fp1=fopen(in_filename,"r");/*打开要加密的文件*/if(fp1==NULL) {printf("cannot open in-file./n");exit(1);/*如果不能打开要加密的文件,便退出程序*/}while(pwd[++j0]);ch=fgetc(fp1);/*加密算法开始*/while(!feof(fp1)) {if(j0 >7)j0 = 0;ch += pwd[j0++];temp += ch;ch=fgetc(fp1);}fclose(fp1);    ofstream out(in_filename);   out<<temp<<endl;out.close();
}/*解密函数*/
void decryptfile(char *in_filename,char *pwd) {FILE *fp1;register char ch;int j=0;int j0=0;string temp;fp1=fopen(in_filename,"r");/*打开要解密的文件*/if(fp1==NULL) {printf("cannot open in-file./n");exit(1);}while(pwd[++j0]);ch=fgetc(fp1);while(!feof(fp1)) {if(j0 >7)j0 = 0;ch -= pwd[j0++];temp += ch;ch=fgetc(fp1);}fclose(fp1);/*关闭源文件*/getFileContent(temp.data());
}//获取解密后文件的内容
void getFileContent(string str) {const char *d = " =rn";   //根据空格、等于号、回车、换行截取字符串char s[2048];strcpy(s,str.c_str());  //将字符串转成char[]char *p;p = strtok(s,d);int i = 1;  //数组下标变量list<string> info_list;list<string>::iterator itor;while(p) {info_list.push_back(p);  //将元素放在list的最后面p=strtok(NULL,d);}itor = info_list.begin();  //循环迭代while(itor!=d()) {content_array[i] = *itor;*itor++;i++;}}

本文发布于:2024-02-01 11:49:14,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170675935636386.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:加密解密   简单   文件
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23