C語言之文件的加密與解密


 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/zym326975/article/details/72822325
#include<stdio.h> #include<stdlib.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; } } /* void copy(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); char *p = (char *)malloc(length*sizeof(char)); fread(p, sizeof(char), length, pfr); fwrite(p, sizeof(char), length, pfw); fclose(pfr); fclose(pfw); } } */ void EncryptFile(char *oldpath, char *newpath) { int i = 0; 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); char *p = (char *)malloc(length*sizeof(char)); fread(p, sizeof(char), length, pfr); for (i = 0; i < length; i++) { p[i] ^= 'A'; } fwrite(p, sizeof(char), length, pfw); fclose(pfr); fclose(pfw); } } void DecryptFile(char *oldpath, char *newpath) { int i = 0; 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); char *p = (char *)malloc(length*sizeof(char)); fread(p, sizeof(char), length, pfr); for (i = 0; i < length; i++) { p[i] ^= 'A'; } fwrite(p, sizeof(char), length, pfw); fclose(pfr); fclose(pfw); } } void main() { char *oldpath = "tmAppAbDBB.db"; char *newpath = "aa.db"; char *newDecryptFilepath = "cc.db"; EncryptFile(oldpath, newpath); DecryptFile(newpath, newDecryptFilepath); }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM