【C/C++學院】0801-重新導向以及檔案掃描/二進位加密解密/簡單加密/按照密碼加密/動態庫與靜態庫,0801-加密解密

來源:互聯網
上載者:User

【C/C++學院】0801-重新導向以及檔案掃描/二進位加密解密/簡單加密/按照密碼加密/動態庫與靜態庫,0801-加密解密
重新導向以及檔案掃描

#define   _CRT_SECURE_NO_WARNINGS//關閉安全檢查#include<stdio.h>#include<stdlib.h>void main1(){       char str[100] = { 0 };       scanf("%s", str);       printf("str=%s\n", str);       system(str);}void main2(){       char str[100] = { 0 };       fscanf(stdin,"%s", str);       fprintf(stdout,"str=%s\n", str);       system(str);}void  main3(){       char *path = "C:\\Users\\wuyq\\Desktop\\newcmd.txt";            int num=0;       char docmd[30] = { 0 };       scanf("%d%s", &num, docmd);//接受鍵盤輸入       FILE *pf;       pf = fopen(path, "w");//寫入       if (pf == NULL)       {              printf("檔案開啟失敗");       }       else       {              fprintf(pf, "for /l %%i in (1,1,%d) do %s", num, docmd);              fclose(pf);       }       system("pause");} void main4(){       char cmd[100] = { 0 };       int num = 0;       char docmd[30] = { 0 };       char *path = "C:\\Users\\wuyq\\Desktop\\newcmd.txt";       FILE *pf = fopen(path, "r");//讀取       if (pf == NULL)       {              printf("檔案開啟失敗");              return;       }       else       {              fscanf(pf, "for /l %%i in (1,1,%d) do %s", &num, docmd);              printf("num=%d,docmd=%s", num, docmd);       }       system("pause");} void main(){       int num ;       scanf("num=%d", &num);//必須精確對應       printf("num=%d", num);       system("pause");}

二進位加密解密
#define  _CRT_SECURE_NO_WARNINGS//關閉安全檢查#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 jia(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);//讀取二進位到記憶體              for (int i = 0; i < length; i++)              {                     p[i] ^= 'A';//抑或              }                          fwrite(p, sizeof(char), length, pfw);//寫入二進位到檔案               fclose(pfr);//關閉檔案              fclose(pfw);       }} void jie(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);//讀取二進位到記憶體              for (int i = 0; i < length; i++)              {                     p[i] ^= 'A';//解密              }               fwrite(p, sizeof(char), length, pfw);//寫入二進位到檔案               fclose(pfr);//關閉檔案              fclose(pfw);       }} void main(){     char  *oldpath = "C:\\Users\\wuyq\\Desktop\\calc.exe";       char  *newpath = "C:\\Users\\wuyq\\Desktop\\calc.exe";       char  *newjiepath = "C:\\Users\\wuyq\\Desktop\\calc.exe";       jia(oldpath, newpath);       jie(newpath, newjiepath);             system("pause");}

簡單加密
#define  _CRT_SECURE_NO_WARNINGS//關閉安全檢查#include<stdio.h>#include<stdlib.h> // 'a' ->'b'//hello -> ifmmp//textjjia.txt//textjie.txt//按照位元組的方式加密 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)       {              return;       }       else       {              while (!feof(pfr))//到了檔案末尾1,沒有到就是0              {                     char  ch = fgetc(pfr);//讀取字元                     putchar(ch);                     fputc(jiami(ch), pfw);//寫入一個加密的字元              }              fclose(pfr);              fclose(pfw);//關閉檔案       }} void jie(char *path, char *pathjie){       FILE *pfr, *pfw;       pfr = fopen(path, "r");//讀取       pfw = fopen(pathjie, "w");//寫入       if (pfr == NULL || pfw == NULL)       {              return;       }       else    {              while (!feof(pfr))//到了檔案末尾1,沒有到就是0              {                     char  ch = fgetc(pfr);//讀取字元                     putchar(ch);                     fputc(jiemi(ch), pfw);//寫入一個加密的字元              }              fclose(pfr);              fclose(pfw);//關閉文       }} void main(){       char *path = "C:\\Users\\yincheng01\\Desktop\\text.txt";       char *pathjia = "C:\\Users\\yincheng01\\Desktop\\textjia.txt";       char *pathjie = "C:\\Users\\yincheng01\\Desktop\\textjie.txt";       jia(path, pathjia);       jie(pathjia, pathjie);        system("pause");} void main1(){       FILE *pfr;       char *path = "C:\\Users\\yincheng01\\Desktop\\text.txt";       pfr = fopen(path, "r");       if (pfr == NULL)       {              printf("檔案開啟失敗");       }       else       {              printf("\n原來的資料\n");              while (!feof(pfr))//feof沒有到檔案末尾返回0,到了返回1              {                     char ch=fgetc(pfr);//從檔案讀取一個字元                     putchar(ch);//輸出字元               }              rewind(pfr);//回到檔案開頭              printf("\n加密後的資料\n");              while (!feof(pfr))//feof沒有到檔案末尾返回0,到了返回1              {                     char ch = fgetc(pfr);//從檔案讀取一個字元                     putchar(ch+1);//輸出字元               }              fclose(pfr);//關閉檔案        }       system("pause");}

按照密碼加密
#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);

#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"密碼加密.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)         {                   int ci = stringlength /passlength;//迴圈次數                   for (int i = 0; i < ci; i++)//迴圈次                   {                            for (int j =0 ; j < passlength; j++)//迴圈密碼                            {                                     string[passlength*i+j] ^= password[j];//異或加密                            }                   }          }         else         {                   int ci = stringlength / passlength;//迴圈次數                   for (int i = 0; i < ci; i++)//迴圈次                   {                            for (int 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;//迴圈次數                   for (int i = 0; i < ci; i++)//迴圈次                   {                            for (int j = 0; j < passlength; j++)//迴圈密碼                            {                                     jiastring[passlength*i + j] ^= password[j];//異或加密                            }                   }         }         else         {                   int ci = stringlength / passlength;//迴圈次數                   for (int i = 0; i < ci; i++)//迴圈次                   {                            for (int j = 0; j < passlength; j++)//迴圈密碼                            {                                     jiastring[passlength*i + j] ^= password[j];//異或加密                            }                   }                   int lastlength = stringlength%passlength;//剩下的長度                   for (int i = 0; i < 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);//擷取長度   430                   //int passlength = strlen(password);//擷取密碼20                   char *newstr = (char*)malloc(sizeof(char)*(length+1));                   for (int i = 0; i < length; i++)                   {                            char ch = fgetc(pfr);//擷取一個字元                            newstr[i] = ch;//不斷存入字元                            //fputc(newstr[i], pfw);//挨個寫入字元                    }                   newstr[length] = '\0';//字串處理為'\0'                   stringjiami(password, newstr);//加密字串                    for (int i = 0; i < length ; i++)                   {                            fputc(newstr[i], pfw);//挨個寫入字元                   }                         fclose(pfr);                   fclose(pfw);//關閉檔案         }} void filejiemi(char *pathjia, char *pathjie, char *password){         FILE *pfr, *pfw;         pfr = fopen(pathjia, "rb");         pfw = fopen(pathjie, "wb");         if (pfr == NULL || pfw == NULL)         {                   fclose(pfr);                   fclose(pfw);                   return;         }         else         {                   while (!feof(pfr))                   {                            char string[256] = { 0 };                            fgets(string, 256, pfr);                            stringjiemi(password, string);//加密啊                            fputs(string, pfw);//寫入jie密檔案                    }                   fclose(pfr);                   fclose(pfw);//關閉檔案         }}

#define  _CRT_SECURE_NO_WARNINGS//關閉安全檢查#include<stdio.h>#include<stdlib.h>#include"密碼加密.h"  void   main(){         char string[50] = "鋤禾日當午";         char *password = "123";         printf("%s\n", stringjiami(password, string));         printf("%s\n", stringjiami(password, string));           char *path = "C:\\Users\\yincheng01\\Desktop\\text.txt";         char *pathjia = "C:\\Users\\yincheng01\\Desktop\\textjia.txt";         char *pathjie = "C:\\Users\\yincheng01\\Desktop\\textjie.txt";         //printf("%d\n", getfilesize(path));         filejiami(path, pathjia, "ABCDE");         filejiami(pathjia, pathjie, "ABCDE");          system("pause");}

動態庫與靜態庫

靜態庫

Lib.h

void msg();int  add(int a, int b);
Lib.c
#include<stdio.h>#include<stdlib.h>#include<Windows.h> void msg(){         MessageBoxA(0, "1", "2", 0);} int  add(int a, int b){         return a + b;}
Main.c
#include<stdio.h>#include<stdlib.h>#include"lib.h" #pragma comment(lib, "靜態庫與動態庫.lib")//標頭檔只是說明,lib自己已經存在介面void main(){         printf("%d", add(10, 9));         msg(); }

動態庫

Dll.c

#include<stdio.h>#include<stdlib.h>#include<Windows.h> //匯出函數,可以載入的時候調用_declspec(dllexport) void msg(){         MessageBoxA(0, "1", "2", 0);} //匯出函數,可以載入的時候調用_declspec(dllexport) int  add(int a, int b){         return a + b;}

Main.c
#include<stdio.h>#include<stdlib.h>#include<Windows.h> typedef void  (*pmsg)();//簡化函數指標typedef int(*padd)(int a, int b);//簡化函數指標 void main(){         HMODULE mydll = LoadLibraryA("動態庫.dll");         if (mydll == NULL)         {                   printf("動態庫載入失敗");         }         else         {                   pmsg pmsg1;//定義一個函數指標                   pmsg1 = (pmsg)GetProcAddress(mydll, "msg");//擷取函數地址                   if (pmsg1 != NULL)                   {                            pmsg1();//執行                   }                   padd padd1;//定義函數指標                   padd1 = (padd)GetProcAddress(mydll, "add");                   if (padd1 != NULL)                   {                            printf("\n%d", padd1(10, 29));                   }          }         FreeLibrary(mydll);         system("pause");}







著作權聲明:本部落格所有文章均為原創,歡迎交流,歡迎轉載;轉載請勿篡改內容,並且註明出處,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.