Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Data Encryption V2.0

4.20/5 (2 votes)
17 Oct 2019CPOL4 min read 5.7K   293  
Data Encryption V2.0

Introduction

Data encryption is a sensitive issue. It has been the subject of numerous articles in various publications. The program presented here is based on the native Windows API. It was developed using Visual Studio 2019 and C++.

Principle

The Data encryption is made with a type of encoding "CALG_RC4" I developed around this principle a different interface. It is always interesting to have more flexibility. Proponents of drag drop and those of the SendTo shall find their account, we will not forget the GUI and the command file. It can encrypt / decrypt any file type. The output file is in the same space as the input file. Whenever the question of the password talking about data encryption arises, I made a choice, and the password is included dynamically during the generation of the program. It is located in a <key.huff> file (look at Space resource issue). It is presented as a resource. This file is encoded with Huffman method.

Coding phase Decoding phase
Input: Fichier.<extension> Input: Fichier.<extension>.yltr
Output: d’entré.<extension>.yltr Output: Fichier.<extension>

The encryption / decryption time depends on several factors:

  • The size of the file
  • The power of CPU
  • RAM available on your PC

Different Interfaces

In order to make more flexible use of the program, I voluntarily have several user interfaces:

  • GUI Interface
  • Command Files
  • SendTo
  • Drag Drop (over DialogBox)
  • Drag Drop (over Icone)
  • File Association « .yltr »

GUI Interface

The launch by double-clicking the icon in the graphical interface appears. It is rudimentary. Depending on the user's choice of text launch button changes.

By default, encode:

Image 1

Or:

Image 2

There are two choices, the color and the text change with the user’s choice.

Command Files

From a simple "command prompt", the program can start.

  • Encryption: <path>AES.exe <path>File for Encryption. Extension
  • Decoding: <path>AES.exe <path>File to be Decoded.extension.yltr

SendTo

The program automatically creates a shortcut in user space (space "SendTo"). We send one or more files to the shortcut.

Image 3

Drag Drop (over DialogBox)

After the launch of the program in GUI mode, the program will accept multiple files.

File Association

The « .yltr ».” extension is added during the setup phase automatically.

Sample Code

The entire program is written in C ++ with MFC Microsoft with a massive use of classes and objects built dynamically. It is difficult to present all code in this presentation and will make it boring. This is not the purpose of this post.

Example 1: Dynamic creation of the "SendTo" icon in the user space.

C#
Send_::SendTO()
{
	Charge_Constante();
	Chemin_complet = Current_Dir() + Back_slash ;
	Nom_exécutable_complet = Chemin_complet + AfxGetAppName() + 
                             Extention nom_du_Racoourcis = Nom_app;
	nom_sortie_complet = Sepcial_Folder(CSIDL_SENDTO) + 
                         Back_slash + AfxGetAppName() + Ext_lnk;
CreateShortCut(Nom_exécutable_complet, nom_du_Racoourcis, 
               nom_sortie_complet, Chemin_complet, Chemin_complet);
}

Example 2: Analysis of arguments passed to the program:

C#
BOOL Analyse_back_groung::Run(LPTSTR *pt_chaine, int Count)
{
	if (Count == 1) return (FALSE);
	 for (int i = 1; i < Count; i++)
	{
	 Liste_fichier.add(pt_chaine[i]);
	}

	for (int i = 0; i < liste_fichier.GetSize(); i++)
	  {
	   Traitement_fichier(liste_fichier.GetAt(i));
	  }
	Liste_fichier.RemoveAll();

     return (TRUE);
}

Example 3: Adjust File Name

The program only requests the name of the input file, it calculates the name of the output file by itself. Here’s how during the Coding Phase:

  • File input: file.txt If the output file does not exist, the output file will be: file.yltr
  • File input: file.txt If the output file exists (file.yltr), the output file will be: File (1).yltr
  • File input: file.txt If the output file exists (file (1).yltr), the output file will be: File (2).yltr
  • and so on

Class <AjustFileName>

File: AjustFileName.cpp and AjustFileName.h

Example 4: Some Color

There are 3 colorful objects in the ENIGMA program: a text, a button and a radio button. It’s 3 classes:

Text
  • File: ColorStatic.cpp and ColorStatic.h
  • Use: Declares on Object m_text
  • Then: m_text.SubclassDlgItem(IDC_STATIC, this);
  • Then: m_text.SetTextColor(RED);
Button
  • File: Mybutton.cpp and Mybutton.h
  • Use: Declares one Object m_chk
  • Then: m_chk.SubclassDlgItem (IDOK, this);
  • Then: m_chk.SetTextColor (RED) ;
Radio Button
  • File: ColorRadiobutton.cpp and ColorRadiobutton.cpp
  • Use: Declares one Object m_radio_0
  • Then: m_radio_0.SubclassDlgItem (IDC_RADIO1, this);
  • Then: m_radio_0.SetTextColor (RED);
  • Then: m_radio_0.SetCheck (true);

Example 5: Extract Data from resource encoded

Files: C_Ressource.cpp and C_Ressource.h

C++
Get_Ressource_huff(char * result, int ID)
{
DWORD dwSize = 0;
int lg = 0;
HESRULT hRes = FindResource (NULL, MAKEINTRESOURCE(ID), RT_RCDATA); // RT_RCDATA
if (hRes != NULL)
 {
dwSize = SizeofResource(NULL, hRes);
 HGLOBAL MemoryHandle = LoadResource(NULL, hRes);
 if (MemoryHandle != NULL)
	{
	 char* resText = (char *) LockResource(MemoryHandle);
	 char* text = (char*)malloc(dwSize + 1);
	 if (text  != NULL) { memcpy(text, resText, dwSize);}
	text[dwSize] = 0;
	simple_Huffman *pt_huff = new simple_Huffman;
	lg = pt_huff->Decompress((BYTE *)text, dwSize);
	memcpy(result, pt_huff->getOutput(), lg);
	pt_huff->Finalize();
	delete pt_huff;
	FreeResource(MemoryHandle);
	free (text);
	}
	FreeResource((HANDLE)hRes);
	}
	return (lg);
}

Linker Option

The Linker instructions are used to merge the section “.text” with “.rcdata” without modifying the code source only by add this option linker.

C#
#pragma comment(linker,"/merge:.rdata=.text")

​​​​​​Before (Release):

Image 4

After (Release):

Image 5

Conclusion

With that new version, the password is stored in the executable but it’s compressed by Huffman method. I just show you that you could add to a program in this case, an encryption program with a more user-friendly interface.

History

  • 16th October, 2019: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)