項目實戰--文件加解密簡單實現

引子

我們在寫完一個東西(文檔,PPT,等)總是不想隨便的被其他人查看,故而我們就需要有一個程序幫我們將這個文件“藏”起來。

適合學習本篇文章的人羣

不需要很高深的技術,多麼厲害的編程能力,只要會C語言編程(入門),懂文件操作部分就好了。

正式進入話題

複習一些在項目中用到的庫函數

//頭文件
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

@1:讀取文本文件

int main(){
    char *path = "C:\\test.txt";
    //打開
    FILE *fp = fopen(path, "r");
    if (fp == NULL){
        printf("文件打開失敗。。。");
        return 0;
    }
    //讀取
    char buff[50];
    while (fgets(buff, 50, fp)){
        printf("%s\n", buff);
    }
    //關閉
    fclose(fp);
    system("pause");
    getchar();
    return 0;
}

@2:寫入文本文件

void main(){
    char *path = "C:\\test.txt";
    //打開
    FILE *fp = fopen(path, "w");
    char *text = "Hello, c++";
    fputs(text, fp);
    //關閉流
    fclose(fp);
    getchar();
}

@3:二進制文件讀寫

計算機的文件存儲在物理上都是二進制
文本文件和二進制之分,其實是一個邏輯之分
c讀寫文本文件與二進制文件的差別僅僅體現在回車換行符
寫文本時,每遇到一個”\n”,會將其轉換成”\r\n”(回車換行)
讀文本時,每遇到一個”\r\n”,會將其轉換成”\n”
*/
//文件複製
void main(){
    char *read_path = "C:\\test.txt";
    char *write_path = "C\\text_copy.txt";
    //讀的文件,b字符表示操作二進制文件binary
    FILE *read_fp = fopen(read_path, "rb");
    FILE *write_fp = fopen(write_path, "wb");
    //複製
    int buff[50];//緩衝區域
    int len = 0; //每次都到的數據長度
    while (len = fread(buff, sizeof(int), 50, read_fp) != 0){
        //將讀到的內容寫入新的文件
        fwrite(buff, sizeof(int), len, write_fp);
    }
    //關閉流
    fclose(read_fp);
    fclose(write_fp);
    getchar();
}

@4:獲取文件的大小

void main(){
    char *read_path = "C:\\test.txt";
    FILE *fp = fopen(read_path, "r");
    //重新定位文件指針
    //SEEK_END文件末尾,0偏移量
    fseek(fp, 0, SEEK_END);
    //返回當前的文件指針,相對於文件開頭的偏移量
    long filesize = ftell(fp);
    printf("%d\n", filesize);
    getchar();
}

@5:文本文件加密

/*
文本文件加密

異或規則:
1. a ⊕ a = 0
2. a ⊕ b = b ⊕ a
3. a ⊕b ⊕ c = a ⊕ (b ⊕ c) = (a ⊕ b) ⊕ c;
4. d = a ⊕ b ⊕ c 可以推出 a = d ⊕ b ⊕ c.
5. a ⊕ b ⊕ a = b
6. 若x是二進制數0101,y是二進制數1011
則x⊕y=1110
只有在兩個比較的位不同時其結果是1,否則結果爲0
即“兩個輸入相同時爲0,不同則爲1”!
*/
////異或
//規則:相同爲0,不同爲1 
//加密
void crpypt(char normal_path[], char crypt_path[]){
    //打開文件
    FILE *normal_fp = fopen(normal_path, "r");
    FILE *crypt_fp = fopen(crypt_path, "w");
    //一次讀取一個字符
    int ch;
    while((ch = fgetc(normal_fp)) != EOF){//End of File
    //寫入異或運算
    fputc(ch ^ 7, crypt_fp);
    }
    //關閉
    fclose(crypt_fp);
    fclose(normal_fp);
}

//解密
void decrpypt(char crypt_path[], char decrypt_path[]){
//打開文件
FILE *normal_fp = fopen(crypt_path, "r");
FILE *crypt_fp = fopen(decrypt_path, "w");
//一次讀取一個字符
int ch;
while((ch = fgetc(normal_fp)) != EOF){
//寫入(異或運算)
fputc(ch ^ 7, crypt_fp);
}
//關閉
fclose(crypt_fp);
fclose(normal_fp);
}
void main(){
char *normal_path = "D:\\atestc\\test.txt";
char *crypt_path = "D:\\atestc\\test_cry.txt";
char *decrypt_path = "D:\\atestc\\test_dec.txt";
//加密
crpypt(normal_path, crypt_path);
//解密
decrpypt(crypt_path, decrypt_path);
getchar();
}

@6:二進制文件加解密

//二進制文件加解密
//讀取二進制文件中的數據時,一個一個字符讀取
//密碼:hellocpp
//加密
void crypt(char normal_path[], char crypt_path[], char password[]){
    //打開文件
    FILE *normal_fp = fopen(normal_path, "rb");
    FILE *crypt_fp = fopen(crypt_path, "wb");
    //一次讀取一個字符
    int ch;
    int i = 0; //循環使用密碼中的字母進行異或運算
    int pwd_len = strlen(password);//密碼長度
    while ((ch = fgetc(normal_fp)) != EOF){
        //寫入異或運算
        fputc(ch ^ password[i % pwd_len], crypt_fp);
        i++;
    }
    //關閉
    fclose(crypt_fp);
    fclose(normal_fp);
}
//解密
void decrpypt(char crypt_path[], char decrypt_path[], char password[]){
    //打開文件
    FILE *normal_fp = fopen(crypt_path, "rb");
    FILE *crypt_fp = fopen(decrypt_path, "wb");
    //一次讀取一個字符
    int ch;
    int i = 0;
    int pwd_len = strlen(password);//密碼的長度
    while ((ch = fgetc(normal_fp)) != EOF){
        //寫入(異或運算)
        fputc(ch ^ password[i % pwd_len], crypt_fp);
        i++;
    }
    //關閉
    fclose(crypt_fp);
    fclose(normal_fp);
}
void main(){
    char *normal_path = "D:\\atestc\\test.txt";
    char *crypt_path = "D:\\atestc\\test_cry.txt";
    char *decrypt_path = "D:\\atestc\\test_dec.txt";
    //加密:
    char* str = "hellocpp";
    crypt(normal_path, crypt_path, str);
    //解密
    decrpypt(crypt_path, decrypt_path, str);
    getchar();
}

項目級程序終極版(殘次品)

/*
改進部分:
@1:文件名由用戶手動輸入
@2:密鑰也可以讓用戶手動設置
@3:保存的加密後的文件位置保存方式
    A.用默認路徑  B.用戶自己設置
@4:界面方面也可以美化
@5:等下一篇放代碼,改進中。
*/
void crypt(char normal_path[], char crypt_path[], char password[]){
    //打開文件
    FILE *normal_fp = fopen(normal_path, "rb");
    FILE *crypt_fp = fopen(crypt_path, "wb");
    //一次讀取一個字符
    int ch;
    int i = 0; //循環使用密碼中的字母進行異或運算
    int pwd_len = strlen(password);//密碼長度
    while ((ch = fgetc(normal_fp)) != EOF){
        //寫入異或運算
        fputc(ch ^ password[i % pwd_len], crypt_fp);
        i++;
    }
    //關閉
    fclose(crypt_fp);
    fclose(normal_fp);
}
//解密
void decrpypt(char crypt_path[], char decrypt_path[], char password[]){
    //打開文件
    FILE *normal_fp = fopen(crypt_path, "rb");
    FILE *crypt_fp = fopen(decrypt_path, "wb");
    //一次讀取一個字符
    int ch;
    int i = 0;
    int pwd_len = strlen(password);//密碼的長度
    while ((ch = fgetc(normal_fp)) != EOF){
        //寫入(異或運算)
        fputc(ch ^ password[i % pwd_len], crypt_fp);
        i++;
    }
    //關閉
    fclose(crypt_fp);
    fclose(normal_fp);
}
void main(){
    char *normal_path; // = "D:\\atestc\\test.txt";
    char *crypt_path = "D:\\atestc\\test_cry.txt";
    char *decrypt_path = "D:\\atestc\\test_dec.txt";

    printf("請輸入要加密的文件名:");
    scanf("%s", &normal_path);
    printf("加密後文件名路徑:D:\\atestc\\test_cry.txt\n");
    //加密:
    char* str = "hellocpp";
    crypt(normal_path, crypt_path, str);
    //解密
    decrpypt(crypt_path, decrypt_path, str);
    getchar();
}
發佈了65 篇原創文章 · 獲贊 35 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章