[C/C ++ school] 0801-redirection and file scanning/binary encryption/Decryption/simple encryption/password-based encryption/dynamic library and static library

Source: Internet
Author: User
Tags fread

[C/C ++ school] 0801-redirection and file scanning/binary encryption/Decryption/simple encryption/password-based encryption/dynamic library and static library
Redirection and file scanning

 

# Define _ CRT_SECURE_NO_WARNINGS // close the security check # include
 
  
# Include
  
   
Void main1 () {char str [100] = {0}; scanf (% s, str); printf (str = % s, str); system (str );} void main2 () {char str [100] = {0}; fscanf (stdin, % s, str); fprintf (stdout, str = % s, 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); // accept the keyboard input FILE * pf; pf = fopen (path, w); // write if (pf = NULL) {printf (File opening failed);} else {fprintf (pf, for/l % I in (, % 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 \ new0000.txt; FILE * pf = fopen (path, r); // read if (pf = NULL) {printf (File opening failed); return;} else {fscanf (pf, for/l % I in, % d) do % s, & num, docmd); printf (num = % d, docmd = % s, num, docmd);} system (pause );} void main () {int num; scanf (num = % d, & num); // The printf (num = % d, num); system (pause );}
  
 

 

 

Binary encryption and decryption
# Define _ CRT_SECURE_NO_WARNINGS // close the security check # include
 
  
# Include
  
   
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; // get FILE size} void copy (char * oldpath, char * newpath) {FILE * pfr, * pfw; pfr = fopen (oldpath, rb); pfw = fopen (newpath, wb); // write binary mode if (pfr = NULL | pfw = NULL) {fclose (pfr); // close the file fclose (pfw); return;} else {int length = getfilesize (oldpath); char * p = (char *) malloc (length * sizeof (char); // allocates memory and reads the file fread (p, sizeof (char), length, pfr ); // read the binary data to the memory fwrite (p, sizeof (char), length, pfw); // write the binary data to the file fclose (pfr ); // close the FILE fclose (pfw);} void jia (char * oldpath, char * newpath) {FILE * pfr, * pfw; pfr = fopen (oldpath, rb ); pfw = fopen (newpath, wb); // write binary mode if (pfr = NULL | pfw = NULL) {fclose (pfr ); // close the file fclose (pfw); return;} else {int length = getfilesize (oldpath); char * p = (char *) malloc (length * sizeof (char )); // allocate memory and read the file fread (p, sizeof (char), length, pfr); // read the binary data to the memory for (int I = 0; I <length; I ++) {p [I] ^ = 'a'; // or} fwrite (p, sizeof (char), length, pfw ); // write binary data to the FILE fclose (pfr); // close the FILE fclose (pfw);} void jie (char * oldpath, char * newpath) {FILE * pfr, * pfw; pfr = fopen (oldpath, rb); pfw = fopen (newpath, wb); // write binary mode if (pfr = NULL | pfw = NULL) {fclose (pfr); // close the file fclose (pfw); return;} else {int length = getfilesize (oldpath); char * p = (char *) malloc (length * sizeof (char); // allocates memory and reads the file fread (p, sizeof (char), length, pfr ); // read binary data to the memory for (int I = 0; I <length; I ++) {p [I] ^ = 'a'; // decrypt} fwrite (p, sizeof (char), length, pfw); // write binary data to the file fclose (pfr); // close the file 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 );}
  
 

Simple Encryption
# Define _ CRT_SECURE_NO_WARNINGS // close the security check # include
 
  
# Include
  
   
// 'A'-> 'B' // hello-> ifmmp // textjjia.txt // textjie.txt // encrypt char jiami (char ch) in byte mode) {return ch ^ 123;} char jiemi (char ch) {return ch ^ 123;} void jia (char * path, char * pathjia) {FILE * pfr, * pfw; pfr = fopen (path, r); // read pfw = fopen (pathjia, w); // write if (pfr = NULL | pfw = NULL) {return;} else {while (! Feof (pfr) // to the end of the file 1, is not 0 {char ch = fgetc (pfr); // read the character putchar (ch ); fputc (jiami (ch), pfw); // write an encrypted character} fclose (pfr); fclose (pfw ); // close the FILE} void jie (char * path, char * pathjie) {FILE * pfr, * pfw; pfr = fopen (path, r ); // read pfw = fopen (pathjie, w); // write if (pfr = NULL | pfw = NULL) {return;} else {while (! Feof (pfr) // to the end of the file 1, is not 0 {char ch = fgetc (pfr); // read the character putchar (ch ); fputc (jiemi (ch), pfw); // write an encrypted character} fclose (pfr); fclose (pfw); // close the file} 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 (File opening failed );} else {printf (original data); while (! Feof (pfr) // feof does not return 0 to the end of the file, to return 1 {char ch = fgetc (pfr); // read a character putchar (ch) from the file ); // output character} rewind (pfr); // return to the beginning of the file printf (encrypted data); while (! Feof (pfr) // feof does not return 0 to the end of the file, and returns 1 {char ch = fgetc (pfr ); // read one character from the file: putchar (ch + 1); // output character} fclose (pfr); // close the file} system (pause );}
  
 

Encrypt by password
# Define _ CRT_SECURE_NO_WARNINGS // close the security check # include
 
  
# Include
  
   
# Include
   
    
// Encrypted, encrypted by password // file // string encrypted char * stringjiami (char * password, char * string); // string decrypted 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 // close the security check # include
 
  
# Include
  
   
# Include
   
    
// Encrypted, encrypted by password // file // string encrypted char * stringjiami (char * password, char * string); // string decrypted char * stringjiemi (char * password, char * jiastring); void filejiami (char * path, char * pathjia, char * password); void filejiemi (char * pathjia, char * pathjie, char * password ); # include password encryption. h int getfilesize (char * path) {FILE * pf = fopen (path, r); if (pf = NULL) {return-1;} else {fseek (pf, 0, SEEK_END); // int lengt at the end of the file H = ftell (pf); return length; // return length} char * stringjiami (char * password, char * string) {int passlength = strlen (password ); // obtain the encrypted length int stringlength = strlen (string); // obtain the string length if (stringlength % passlength = 0) {int ci = stringlength/passlength; // number of cycles for (int I = 0; I <ci; I ++) // number of cycles {for (int j = 0; j <passlength; j ++) // cyclic password {string [passlength * I + j] ^ = password [j]; // unique or encrypted} else {int ci = Stringlength/passlength; // number of cycles for (int I = 0; I <ci; I ++) // number of cycles {for (int j = 0; j <passlength; j ++) // cyclic password {string [passlength * I + j] ^ = password [j]; // unique or encrypted} int lastlength = stringlength % passlength; // The remaining length for (int I = 0; I <lastlength; I ++) {string [passlength * (stringlength/passlength) + I] ^ = password [I] ;}return string ;}// string decryption char * stringjiemi (char * password, char * jiastring) {int Passlength = strlen (password); // obtain the encrypted length int stringlength = strlen (jiastring); // obtain the string length if (stringlength % passlength = 0) {int ci = stringlength/passlength; // number of cycles for (int I = 0; I <ci; I ++) // number of cycles {for (int j = 0; j <passlength; j ++) // cyclic password {jiastring [passlength * I + j] ^ = password [j]; // exclusive or encrypted }}else {int ci = stringlength/passlength; // number of cycles for (int I = 0; I <ci; I ++) // loop count {for (int j = 0; j <passlength; j ++) // cyclic password {jiastring [passlength * I + j] ^ = password [j]; // exclusive or encrypted} int lastlength = stringlength % passlength; // The remaining length 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 ); // get length 430 // int passlength = strlen (password); // get password 20 char * newstr = (char *) malloc (sizeof (char) * (length + 1); for (int I = 0; I <length; I ++) {char ch = fgetc (pfr ); // obtain the character newstr [I] = ch; // keep saving the character // fputc (newstr [I], pfw ); // write characters one by one} newstr [length] = ''; // the string is processed as ''stringjiami (password, newstr); // The encrypted string For (int I = 0; I <length; I ++) {fputc (newstr [I], pfw); // write characters one by one} fclose (pfr ); fclose (pfw); // close the FILE} 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); // encrypted Ah fputs (string, pfw); // write the jie Password File} fclose (pfr); fclose (pfw); // close the file }}
   
  
 

# Define _ CRT_SECURE_NO_WARNINGS // close the security check # include
 
  
# Include
  
   
# Include password encryption. h void main () {char string [50] =; char * password = 123; printf (% s, stringjiami (password, string); printf (% s, 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, getfilesize (path); filejiami (path, pathjia, ABCDE ); filejiami (pathjia, pathjie, ABCDE); system (pause );}
  
 

Dynamic library and static library

Static Library

Lib. h

void msg();int  add(int a, int b);
Lib. c
#include
 
  #include
  
   #include
   
     void msg(){         MessageBoxA(0, 1, 2, 0);} int  add(int a, int b){         return a + b;}
   
  
 
Main. c
# Include
 
  
# Include
  
   
# Define delib. h # pragma comment (lib, static library and dynamic library. lib) // the header file only indicates that lib already has the void main () {printf (% d, add (10, 9); msg ();}
  
 

Dynamic library

Dll. c

# Include
 
  
# Include
  
   
# Include
   
    
// Export function, which can be called when loading _ declspec (dllexport) void msg () {MessageBoxA (0, 1, 2, 0);} // export function, call _ declspec (dllexport) int add (int a, int B) {return a + B ;}
   
  
 

Main. c
# Include
 
  
# Include
  
   
# Include
   
    
Typedef void (* pmsg) (); // simplified function pointer typedef int (* padd) (int a, int B); // simplified function pointer void main () {HMODULE mydll = LoadLibraryA (dynamic library. dll); if (mydll = NULL) {printf (dynamic library loading failed);} else {pmsg pmsg1; // define a function pointer pmsg1 = (pmsg) GetProcAddress (mydll, msg); // obtain the function address if (pmsg1! = NULL) {pmsg1 (); // execution} padd padd1; // define the function pointer padd1 = (padd) GetProcAddress (mydll, add); if (padd1! = NULL) {printf (% d, padd1 (10, 29) ;}} FreeLibrary (mydll); system (pause );}
   
  
 







 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.