OldSchoolCode

.com



Combining what we've learned and then some
The next term we are going to cover is called "global". "Global" is not a keyword, it is the term used to describe a variable that doesn't belong to a specific function. You can declare the global variables at the top of the .cpp file, underneath the #includes.

example:
 
#include <stdio.h>
 
int g_MyGlobal = 5;
 
int main()
{
 
}
here we have declared a variable called g_MyGlobal and set its value to 5. We can reference this inside of any function we wish. It's good convention to preface a global varaible with "g_" so anyone glancing at the code knows it's a global.

For the sample code below:

Place the .exe on your desktop, create a .txt file and type a few lines of text in it. When you drag your .txt file over the .exe the program will ask you if you want to encrypt or decrypt and ask you for a key. The program will create a new file on your desktop with the text you entered encrypted and then delete your original .txt file. I kept the encryption functions to only two and the sub table to only a,e,i,o,and u. You can ( should ) play around with these functions, create your own functions and test it thoroughly. Enter an incorrect decrypt key to see the results, etc.

__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE   
   
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <shlwapi.h>
#include <stdlib.h>
   
int g_key;

//function to change any vowel to a
//number. If you had the time you could
//write a table that encapsulated every
//thing :)
int subTable( unsigned char *pByte )
{
   if ( *pByte == 'a' )
   {
      *pByte = '1';
   }
   else if ( *pByte == 'e' )
   {
      *pByte = '2';
   }
   else if ( *pByte == 'i' )
   {
      *pByte = '3';
   }
   else if ( *pByte == 'o' )
   {
      *pByte = '4';
   }
   else if ( *pByte == 'u' )
   {
      *pByte = '5';
   }
   return 0;
}

//the table to return our vowels back
int subTable2( unsigned char *pByte )
{
   if ( *pByte == '1' )
   {
      *pByte = 'a';
   }
   else if ( *pByte == '2' )
   {
      *pByte = 'e';
   }
   else if ( *pByte == '3' )
   {
      *pByte = 'i';
   }
   else if ( *pByte == '4' )
   {
      *pByte = 'o';
   }
   else if ( *pByte == '5' )
   {
      *pByte = 'u';
   }
   return 0;
}

//function to increment our bytes
int Value( void )
{
   //g_key is our global holding
   //the key we enter
   static int v = g_key;
   
   //add one more each time
   //the function called
   v++;
 
   return v;
}

//function to find out the string length
int StringLength( char *pString )
{
   int i;
	
   for (i=0;pString[i]!=0;i++)
   {
   }
	
   return i;
}

int main(int argc,char *pArgv[])
{
   char sourceName[260];
   char destName[260];
   unsigned char byte;
   int option;
	
   if (argc==2)
   {
      printf("\n\n %s\n", pArgv[1]);
      printf("\n\n Encrypt = 1 \n");
      printf("\n Decrypt = 2 \n");
      printf("\n 1 or 2 ? : ");
      scanf("%d",&option);

      if (option==1)
      {
         printf("\n Enter a key number for this file : ");

         //take the key entered and
         //save it in our global
         scanf("%d",&g_key);
			
         //copy the file dragged onto the exe
         //and save it to sourceName
         strcpy( sourceName, pArgv[1] );

         //add ,encrypted.txt to the original
         //filename and save it to destName
         sprintf( destName, "%s.encrypted.txt", sourceName );

         //open the original for reading
         FILE * pSource = fopen(sourceName,"rb");
			
         //open a new file for writing
         FILE * pDest = fopen(destName,"wb");

         int myVal = Value();

         //read one byte at a time from the original
         while ( fread( &byte,sizeof(unsigned char), 1, pSource ) != 0 )
         {
            //take a byte and send it to
            //our subTable function above
            subTable( &byte );

            //take our byte changed in our
            //subTable function and now send
            //it to our Value function to increment
            //it a bit. The first number the function
            //will use will be the key you type in.
            //see Value function above
            byte += myVal = Value();

            //write the byte out after manipulating it
            //two times. As you can see you could 
            //manipulate the byte as many times as you 
            //would like before calling fwrite. 
            fwrite( &byte, sizeof(unsigned char), 1, pDest );
         }

         //close our files !
         fclose( pSource );
         fclose( pDest );

         //delete the original
         DeleteFile( sourceName );
			
         printf("\n Your file has been encrypted.\n ");
         printf("\n Press enter to exit this screen. ");
      }

      else if (option==2)
      {
         printf("\n Enter a key number for this file : ");

         scanf("%d",&g_key);
			
         strcpy( sourceName, pArgv[1] );

         strcpy( destName, sourceName );

         //here we get the length of the filename
         //we dragged over the exe
         int length = StringLength( destName );

         //we added .encrypted.txt to our encrypted
         //file so now we take the length and subtract
         //14 from it to get our original filename
         destName[ length - 14 ] = 0;

         FILE *pDest = fopen( destName, "rb" );
         
         //if the file already exists, yell
         //at us
         if ( pDest != 0 )
         {
            fclose( pDest );
            printf(" \n\n\n WARNING!!! File already exists!!!");
            printf("\n Press enter to exit this screen. ");
            getch();
            return 0;
         }
         else 
         {
            FILE * pSource = fopen(sourceName,"rb");

            FILE * pDest = fopen(destName,"wb");

            int myVal = Value();

            while ( fread( &byte, sizeof(unsigned char), 1, pSource) != 0 )
            {
               //have to call our Value function
               //first ( reverse order to decrypt )
               byte -= myVal = Value();

               //call the second subTable to change
               //the numbers back to vowels
               subTable2( &byte );

               //write the byte out
               fwrite( &byte, sizeof(unsigned char), 1, pDest );
            }

            //don't forget to close!
            fclose( pSource );
            fclose( pDest );

            //delete the encrypted file
            DeleteFile( sourceName );

            printf("\n Your file has been decrypted.\n ");
            printf("\n Press enter to exit this screen. ");
         }
      }
   }

   else
   {
      printf("\n\n Please drag and drop your file onto the exe.\n\n ");
      printf("Press enter to exit this screen. ");
   }

   getch(); 

   return 0;
}