【C/C++学院】0801-重定向以及文件扫描/二进制加密解密/简单加密/按照密码加密/动态库与静态库

来源:互联网 发布:linux touch 多个文件 编辑:程序博客网 时间:2024/06/03 23:00

重定向以及文件扫描

#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");}







1 0
原创粉丝点击