Click here to Skip to main content
15,889,216 members
Articles / Desktop Programming / MFC
Article

A Little Encryption Utility that will turn all your keys from the keyboard in to a binary file

Rate me:
Please Sign up or sign in to vote.
1.53/5 (14 votes)
9 Dec 20042 min read 35.2K   328   12   5
This program will take all lettlers from a given file and turn them into a binary code defined by me. Attention, it only encrypts keys that are on you keyboard and it is not case sensitive.

Introduction

The idea for this program initially came from a friend of mine who wanted to have a diary on the computer and asked me to give her a program that will turn her diary text file in to another file that she will be able to decrypt later.

The program is really not much. All that it does is, gets every character from the file and then compares it with the definition given by me that you will see later. If the character read is on the definitions list, then it writes in the output file, the binary code defined for it.

The definition file

The definition file is the file I was talking about.

#define A   "0000001"
#define B   "0000010"
#define C   "0000100" 
#define D   "0001000"
#define E   "0010000"
#define F   "0100000"
#define G   "1000000"
#define H   "0000011"
#define I   "0000101"
#define J   "0001001"
#define K   "0010001"
#define L   "0100001"
#define M   "1000001"
#define N   "0000110"
#define O   "0001010"
#define P   "0010010"
#define R   "0100010"
#define S   "1000010"
#define T   "0001100"
#define U   "0010100"
#define V   "0100100"
#define W   "1000100"
#define X   "0011000"
#define Y   "0101000"
#define Z   "1001000"
#define SP   "0110000" 
#define N1   "1010000" //1
#define N2   "1100000" //2
#define N3   "0000111"
#define N4   "0001011"
#define N5   "0010011"
#define N6   "0100011"
#define N7   "1000011"
#define N8   "0001101"
#define N9   "0010101"
#define N0   "0100101" //0
#define CONS  "1000101" //`
#define CONS1  "0011001" //~
#define EXCLAM  "0101001" //!
#define DIEZ  "1001001" //#
#define DOLLAR  "0110001" //$
#define PERCENT     "1010001" //%
#define HAT   "1100001" //^
#define AND   "0001110" //&
#define STAR  "0010110" //*
#define PARANTEZAD  "0100110" //(
#define PARANTEZAI  "1000110" //)
#define LINE  "0011100" //-
#define UNDERLINE   "0101100" //_
#define PLUS  "1001100" //+
#define EGAL  "0111000" //=
#define ACOLADAD    "1011000" //{
#define ACOLADAI    "1110000" //}
#define PPATRATAD   "0001111" //[
#define PPATRATAI   "0010111" //] 
#define PCTSIVIRG   "0100111" //;
#define DOUAPCT     "1000111" //:
#define APOSTR  "0011011" //'
#define GHILIMELE   "0101011" //"
#define VIRG  "1001011" //,
#define PCT   "0110011" //.
#define MAIMIC  "1010011" //<
#define MAIMARE     "1100011" //>
#define QUESTION    "0011101" //?
#define SLASH  "0101101" // /
#define BACKSLASH   "1001101" // \
#define UPLINE  "0111001" // |
#define SPACE  "1011001" //" "
#define RO   "1110001" // \n
#define Q   "0011111" // q
#define AROND  "0101111" //@

Now as you can see, for example, the "@" character is defined as "0101111" and this is how it will be written in the output file.

Implementation

Now as I said, the implementation is not a very big deal.

   OPENFILENAME ofn;
   char file[1024],out[1024];
   file[0]='\0';
   out[0]='\0';   ZeroMemory(&ofn,sizeof(ofn));
   ofn.lpstrFileTitle="Alege fisierele sau fisierul care sa fie cryptat";
   ofn.Flags=OFN_PATHMUSTEXIST;
   ofn.lStructSize=sizeof(OPENFILENAME);
   ofn.lpstrFilter="Text Files (*.txt)\0*.txt\0Log Files "
        "(*.log)\0*.log\0Documents(*.doc)\0*.doc\0All Files(*.*)\0*.*\0";
   ofn.nMaxFile=512;
   ofn.lpstrFile=file;
   GetOpenFileName(&ofn); SetDlgItemText(hdlg,IDC_EDIT2,file);
   GetDlgItemText(hdlg,IDC_EDIT3,out,sizeof(out));
   if (strcmp(out,"")==0)
   {
     MessageBox(NULL,"output file missing","Gabby",MB_ICONSTOP);
     break;
   }

After getting the file that you want to encrypt and the output file, you will need a variable that will be used to read the file char by char.

    char bu;

    FILE *f,*g;  //the 2 files poiters
    //the output file will be opened with the  "w" 
    //parameter which means is for writting
    f=fopen(out,"w");
    //the file to be encrypted will be opened with the  
    //"r" parameter which means is for reading 
    g=fopen(file,"r"); 

    //we will have to disable the button now until the decryption process is over 
    EnableWindow(GetDlgItem(hdlg,DEC),FALSE);

Now, after we have the files opened and the handles in f and g, we start to read.

while(true)
{  
  
  bu=fgetc(g);

  if(feof(g))
    break;
  if ((bu=='%'))
    fprintf(f,PERCENT);
  else
    if ((bu=='A')|| (bu=='a'))
      fprintf(f,A);
  else . . . . . /we test all the definitions

}

In the end, all we have to do is close both files and enable the button back.

 fclose(f);
 fclose(g);
 MessageBox(NULL,"Encryption ready","Gabby",MB_ICONINFORMATION);
 EnableWindow(GetDlgItem(hdlg,DEC),TRUE);

This was it!

Now the reversable part where we have the binary file and we want it turned in characters.

We do absolutely the same thing. Open the two files.

    char bua[8];
    FILE *altu,*alt;
    altu=fopen(fil,"r");
    alt=fopen(ou,"w");

Then we will need to read from the file the first seven chars, then the following seven and so on.

Very important to know is that a character is ended in NULL.

//ex :when you copy "a" in a char
strcpy(c,"a");
//c actually looks like this c="a<NULL>" so it's length is 2 not 1

This is important because when you read from the file, you will read actually the first 8 characters, not the first 7.

Now the implementation:

while(true)
{   
  fgets(bua,8,altu);
  if(feof(altu))
    break;
  if (strcmp(bua,A)==0)
    fprintf(alt,"A");
  else // checks if the 7 characters read match with the one in the defines if yes
  //writes it in the file otherwise continues on with checking
  //in the end we have to enable the button again and close the files
 fclose(alt);
 fclose(altu);
 EnableWindow(GetDlgItem(hdlg,CFILE),TRUE);;

Now this code can be worked out and for example switch the binaries after a certain algorithm and really turn it into an encryption program. As you can see, it is not case sensitive but you can add defines with all the characters from the ASCII table to make it full compatible if you know what I mean :)

Hope you enjoy it. Thanks in advance.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Romania Romania
I love VC++

Comments and Discussions

 
Questionmy vote 1 Pin
Roger6529-Aug-19 5:55
Roger6529-Aug-19 5:55 
garbage
Old dog learning new tricks!

QuestionEncryption? Pin
blippblapp9-Dec-04 20:41
blippblapp9-Dec-04 20:41 
AnswerRe: Encryption? Pin
gamitech10-Dec-04 2:28
gamitech10-Dec-04 2:28 
GeneralTO DO Pin
gamitech9-Dec-04 12:03
gamitech9-Dec-04 12:03 
GeneralRe: TO DO Pin
ThatsAlok9-Dec-04 19:06
ThatsAlok9-Dec-04 19:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.