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"
#define N2 "1100000"
#define N3 "0000111"
#define N4 "0001011"
#define N5 "0010011"
#define N6 "0100011"
#define N7 "1000011"
#define N8 "0001101"
#define N9 "0010101"
#define N0 "0100101"
#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"
#define Q "0011111"
#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;
f=fopen(out,"w");
g=fopen(file,"r");
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.
strcpy(c,"a");
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
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.