文件加解密,文件操作
1、fseek,ftell,fread,fwrite(簡單文件加密)
#define _CRT_SECURE_NO_WARNINGS? //去掉安全檢查
#include <stdio.h>
#include <stdlib.h>
?
/*讀取文件大小*/
int getfilesize(char *path)
{
??? FILE *pf = fopen(path, "r");
??? if (pf == NULL)
??? {
??????? fclose(pf);
??????? return -1;
??? }
??? else
??? {
??????? fseek(pf,0,SEEK_END);
??????? int length = ftell(pf);
??????? //獲取文件大小
??????? return length;
??? }
??? fclose(pf);
}
?
/*實現(xiàn)文件復(fù)制*/
void copy(char *oldpath,char *newpath)
{
??? FILE *pfr, *pfw;
??? //以打開二進制文件的方式打開
??? pfr = fopen(oldpath, "rb");
??? //寫入二進制模式
??? pfw = fopen(newpath, "wb");
??? if (pfr == NULL || pfw == NULL)
??? {
??????? fclose(pfr);?? //關(guān)閉文件
??????? fclose(pfw);
??????? return;
??? }
??? else
??? {
??????? int length = getfilesize(oldpath);
??????? //分配內(nèi)存,讀取文件
??????? char *p = (char *)malloc(length * sizeof(char));
??????? //讀取二進制到內(nèi)存
??????? fread(p,sizeof(char),length,pfr);
??????? //寫入二進制到文件
??????? fwrite(p,sizeof(char),length,pfw);
???????
??????? //關(guān)閉文件
??????? fclose(pfr);
??????? fclose(pfw);
??? }
}
?
void encryptyfile(char *oldpath,char *newpath)
{
??? FILE *pfr, *pfw;
??? pfr = fopen(oldpath, "rb");
??? //寫入二進制模式
??? pfw = fopen(newpath, "wb");
??? if (pfr == NULL || pfw == NULL)
??? {
??? ??? //關(guān)閉文件
??????? fclose(pfr);
??????? fclose(pfw);
??????? return;
??? }
??? else
??? {
??????? int length = getfilesize(oldpath);
??????? //分配內(nèi)存,讀取文件
??????? char *p = (char *)malloc(length*sizeof(char));
??????? //讀取二進制到內(nèi)存
??????? fread(p,sizeof(char),length,pfr);
??????? for (int i = 0; i < length;i++)
??????? {
??????????? //加密方法是,與制定字符進行異或操作
??????????? p[i] ^= 'A';
??????? }
??????? //寫入二進制到文件
??????? fwrite(p, sizeof(char), length, pfw);
?
??????? //關(guān)閉文件
??????? fclose(pfr);
??????? fclose(pfw);
??? }
}
?
/*解密文件*/
void decodefile(char *oldpath,char *newpath)
{
??? FILE *pfr, *pfw;
??? pfr = fopen(oldpath, "rb");
??? //寫入二進制模式
??? pfw = fopen(newpath,"wb");
??? if (pfr == NULL || pfw == NULL)
??? {
??????? fclose(pfr);
??????? fclose(pfw);
??????? return;
??? }
??? else
??? {
??????? int length = getfilesize(oldpath);
??????? //分配內(nèi)存,讀取文件
??????? char *p = (char *)malloc(length*sizeof(char));
??????? //讀取二進制到內(nèi)存
??????? fread(p,sizeof(char),length,pfr);
??????? int i;
??????? for (i = 0; i < length;i++)
??? ??? {
??????????? p[i] ^= 'A';
??? ??? }
??????? //寫入二進制到文件
??????? fwrite(p, sizeof(char), length, pfw);
??? }
??? //關(guān)閉文件
??? fclose(pfr);
??? fclose(pfw);
}
int main(int argc,char *argv[])
{
??? char *path1 = "G:\\1.txt";
??? char *path2 = "G:\\2.txt";
??? char *path3 = "G:\\3.txt";
?
??? encryptyfile(path1, path2);
??? decodefile(path2, path3);
??? //printf("%d\n",getfilesize(path1));
?
??? system("pause");
??? return 0;
}
上面的過程將會把加解密的文件輸出到文件中。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
?
char jiami(char ch)
{
??? return ch ^ 123;
}
?
char jiemi(char ch)
{
??? return ch ^ 123;
}
?
void jia(char *path, char *pathjia)
{
??? FILE *pfr, *pfw;
??? pfr = fopen(path, "r");//讀取
??? pfw = fopen(pathjia, "w");//寫入
??? if (pfr == NULL || pfw == NULL)
??? {
??????? fclose(pfw);
??????? fclose(pfr);
??????? return;
??? }
??? else
??? {
??????? //feof(FILE *pf) 到了文件末尾1,沒有到就是0
??????? //下面的過程將把文件內(nèi)容輸出到屏幕上。
??????? while (!feof(pfr))
??????? {
??????????? //讀取字符
??????????? char ch = fgetc(pfr);
??????????? putchar(ch);
??????????? //輸出字符的時候?qū)⒆址用?/span>
??????????? fputc(jiami(ch), pfw);
??????? }
??? }
??? fclose(pfr);
??? fclose(pfw);//關(guān)閉文件
}
?
/*解密*/
void jie(char *path,char *pathjie)
{
??? FILE *pfr, *pfw;
??? //讀取
??? pfr = fopen(path, "r");
??? //寫入
??? pfw = fopen(pathjie,"w");
??? if (pfr == NULL || pfw == NULL)
??? {
??????? fclose(pfr);
??????? fclose(pfw);
??????? return;
??? }
??? else
??? {
??????? //到了文件末尾1,沒有到就是0
??????? while (!feof(pfr))
??????? {
??????????? char? ch = fgetc(pfr);//讀取字符
??????????? putchar(ch);
??????????? fputc(jiemi(ch), pfw);//寫入一個加密的字符
??????? }
??? }
??? fclose(pfr);
??? //關(guān)閉文件
??? fclose(pfw);
}
?
int main(int argc,char *argv[])
{
??? char *path1 = "G:\\1.txt";
??? char *path2 = "G:\\2.txt";
??? char *path3 = "G:\\3.txt";
?
??? //jia(path1, path2);
??? jie(path2, path3);
?
??? system("pause");
??? return 0;
}
?
?
?
密碼加密
頭文件:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
?
//字符串加密
char * stringjiami(char *password, char *string);
//字符串解密
char * stringjiemi(char *password, char *jiastring);
void filejiami(char *path, char *pathjia, char *password);
void filejiemi(char *pathjia, char *pathjie, char *password);
?
?
?
?
?
#include "encrytiondecrytion.h"
?
int getfilesize(char *path)
{
??? FILE *pf = fopen(path,"r");
??? if (pf == NULL)
??? {
??????? return -1;
??? }
??? else
??? {
??????? fseek(pf, 0, SEEK_END);//文件末尾
??????? int length = ftell(pf);
??????? return length;//返回長度
??? }
}
?
char * stringjiami(char *password, char *string)
{
??? //獲取加密長度
??? int passlength = strlen(password);
??? //獲取字符串長度
??? int stringlength = strlen(string);
??? if (stringlength % passlength == 0)
??? {
??????? //循環(huán)次數(shù)
??????? int ci = stringlength / passlength;
??????? int i,j;
??????? for (i = 0; i < ci;i++)?? //循環(huán)次數(shù)
??????? {
??????????? //循環(huán)密碼
??????????? for (j = 0; j < passlength;j++)
??????????? {
??????????????? string[passlength*i + j] ^= password[j];
??????????? }
??????? }
??? }
??? else
??? {
??????? int ci = stringlength / passlength; //循環(huán)次數(shù)
??????? int i, j;
??????? for (int i = 0; i < ci;i++)
??????? {
??????????? //循環(huán)密碼
??????????? for (j = 0; j < passlength;j++)
??????????? {
??????????????? string[passlength * i + j] ^= password[j];
??????????? }
??????? }
??????? int lastlength = stringlength % passlength;//剩下的長度
??????? for (int i = 0; i < lastlength;i++)
??????? {
??????????? string[passlength*(stringlength / passlength) + i] ^= password[i];
??????? }
??? }
??? return string;
}
?
//字符串解密
char * stringjiemi(char *password, char *jiastring)
{
??? //獲取加密長度
??? int passlength = strlen(password);
??? //獲取字符串長度
??? int stringlength = strlen(jiastring);
??? if (stringlength %passlength == 0)
??? {
??????? int ci = stringlength / passlength;//循環(huán)次數(shù)
??????? int i, j;
??????? for (i = 0; i < ci; i++)
??????? {
??????????? for (j = 0; j < passlength;j++)
??????????? {
??????????? ??? //異或加密
??????????????? jiastring[passlength * i + j] ^= password[j];
??????????? }
??????? }
??? }
??? else
??? {
??????? //循環(huán)次數(shù)
??????? int ci = stringlength / passlength;
??????? int i, j;
??????? for (i = 0; i < ci;i++)
??????? {
??????????? //循環(huán)密碼
??????????? for (j = 0; j < passlength;j++)
??????????? {
??????????????? //異或加密
??????????????? jiastring[passlength*i + j] ^= password[j];
??????????? }
??????? }
??????? //剩下的長度
??????? int lastlength = stringlength % passlength;
??????? for (int i = 0; j < lastlength;i++)
??????? {
??????????? jiastring[passlength*(stringlength / passlength) + i] ^= password[i];
??????? }
??? }
??? return jiastring;
}
?
void filejiami(char *path, char *pathjia, char *password)
{
??? FILE *pfr, *pfw;
??? pfr = fopen(path, "r");
??? pfw = fopen(pathjia,"w");
??? if (pfr == NULL || pfw == NULL)
??? {
??????? fclose(pfr);
??????? fclose(pfw);
??????? return;
??? }
??? else
??? {
??????? int length = getfilesize(path);
??????? char *newstr = (char*)malloc(sizeof(char)*(length + 1));
??????? for (int i = 0; i < length;i++)
??????? {
??????????? char ch = fgetc(pfr);? //獲取一個字符
??????????? newstr[i] = ch;//不斷存入字符
??????? }
??????? //字符串處理為'\0'
??????? newstr[length] = '\0';
??????? //加密字符串
??????? stringjiami(password, newstr);
?
??????? int i;
??????? for (i = 0; i < length;i++)
??????? {
??????????? //挨個寫入字符
??????????? fputc(newstr[i], pfw);
??????? }
??? }
??? fclose(pfr);
??? //關(guān)閉文件
??? fclose(pfw);
}
?
main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include "encrytiondecrytion.h"
?
int main(int argc,char *argv[])
{
??? char string[50] = "鋤禾日當(dāng)午";
??? char *password = "123";
??? printf("%s\n",stringjiami(password,string));
??? printf("%s\n", stringjiami(password, string));
?
??? char *path1 = "G:\\1.txt";
??? char *path2 = "G:\\2.txt";
??? char *path3 = "G:\\3.txt";
?
??? filejiami(path1, path2, "ABCDE");
??? filejiami(path2, path3, "ABCDE");
?
??? system("pause");
}
?
fscanf
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
?
int main(int argc, char *argv[])
{
??? char str[100] = { 0 };
??? fscanf(stdin, "%s", str);
??? fprintf(stdout, "str=%s\n", str);
?
??? system(str);
??? return 0;
}
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的文件加解密,文件操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么在15点前看基金涨跌 通过基金估值
- 下一篇: 拍拍贷资金筹集中是不是稳了