c++檔案的加密與解密

來源:互聯網
上載者:User
<spanstyle="font-family:KaiTi_GB2312;font-size:18px;">//FileEncryDecry.cpp:定義控制台應用程式的進入點。///*利用異或預算對檔案進行加密主要是讀取檔案中的字元對每個字元對KEY進行異或運算儲存到另外一個檔案中從而實現加密的功能由於異或的性質加密解密可以使用同一個函數*/#include"stdafx.h"#include<filesystem>#include<stdlib.h>#defineOK0#defineERR-1#defineKEY0xa9intFileConvert(charszOldFile[],charszNewFile[]){FILE*pOldFile=NULL;FILE*pNewFile=NULL;//指標初始化為NULLcharcTemp;if(szOldFile==NULL||szNewFile==NULL){returnERR;}pOldFile=fopen(szOldFile,"rb");//二進位讀寫檔案if(pOldFile==NULL){returnERR;}pNewFile=fopen(szNewFile,"wb");if(pNewFile==NULL){fclose(pOldFile);returnERR;}//一定要先讀取字元然後做檔案末尾判斷cTemp=fgetc(pOldFile);while(!feof(pOldFile))//遇到檔案結束傳回值為0{putchar(cTemp);fputc(cTemp,pNewFile);cTemp=fgetc(pOldFile)^KEY;//異或運算加密檔案}fclose(pOldFile);fclose(pNewFile);//用完檔案之後需要關閉檔案returnOK;}int_tmain(intargc,_TCHAR*argv[]){charszFile1[]="1.txt";charszFile2[]="2.txt";charszFile3[]="3.txt";//可以修改不同的檔案格式FileConvert(szFile1,szFile2);system("pause");return0;}</span>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.