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

Data Encryption

4.50/5 (2 votes)
22 Jun 2015CPOL4 min read 8.6K   157  

Before

Having an idea and putting it into practice by creating a program is one thing, but we must also remember it must interface with other utilities in order to make the program easier to use. When we look at the amount of code necessary to write the basic idea and comparing it to that of utilities, we see that the volume of code utilized is much more important than identifying the basic idea. Please note: I don’t take into account the volume of static or dynamic libraries codes.

I tried to make the most flexible program. I did not invent anything, I simply modified what others did online and adapted it by making it a little more flexible.

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 with Visual Studio 2013 C ++ and InstallShield Express Setup.

Principle

The Data encryption is made with "CALG_RC4" encoding, which I used as the foundation and developed interfaces around it. It is always interesting to have more flexibility. Proponents of Drag drop and those of the SendTo will be pleased and we will not forget the GUI.

It can encrypt / decrypt any file type. The output file is in the same size 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.txt> file (look at the space resource issue). It is presented as a resource.

Coding Phase Deconding 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 interface:

  • 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

The default is encode

Image 1

Or

Image 2

Command Files

From a simple "command prompt" we can start the program

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 it to the shortcut one or more files

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, and built dynamically.

From code 1 : Dynamic creation of the "SendTo" icon in the user space

C++
SendTo::SendTo()
{
    Charge_Constante();
    
    Chemin_complet = Current_Dir() + Back_slash ;
    Nom_executable_complet = Chemin_complet + AfxGetAppName() + Extention; // <chemin>AES.exe
    nom_du_Racoourcis = Nom_app;
    nom_sortie_complet = Sepcial_Folder(CSIDL_SENDTO) + Back_slash + AfxGetAppName() + Ext_link;
    
    if (PathFileExists(non_sortie_complet) == FALSE)
    {
        CreateShortCut(Nom_executable_complet, nom_du_Racoourcis, nom_sortie_complet, Chemin_complet, Chemin_complet);
    }
}

From code 2 :  Extraction password

C++
ICString C_Password::Get_Password()
{
    CString chaine = _T("");
    
    HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_MYFILE), RT_RCDATA);
    
    if (hRes != NULL)
    {
    
        // Load the resource and save its total size.
        DWORD dwSize = SizeofResource(NULL, hRes);
        HGLOBAL MemoryHandle = LoadResource(NULL, hRes);
        if (MemoryHandle != NULL){
        
            // LockResource returns a BYTE pointer to the raw data in
            // the resource
                BYTE "MemPtr = (BYTE ")LockResource(MemoryHandle);
                chaine = MemPtr;
                
        }
        FreeResource((HANDLE)hRes);
    }
    return (chaine);
}

From code 3:  Analysis of arguments passed to the program

C++
BOOL Analyse_back_groung::Run(LPTSR *pt_chaine, int Count)
{

    if (Count == 1) return (FALSE);
    
    // Au moins 1 parameters passe
    for (int i = 1; i < Count; i++)
    {
        // le premier parametre n'est pas memorise
        // Car il contient le chemin complet ainsi que le nom du programme
        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);
}

From code 4: Read the arguments from line command

C++
BOOL CAESApp::Interactif()
{
    BOOL Status = FALSE;
    
    Analyse_back_groung *pt_analyse = new Analyse_back_groung;
    int Count;
    LPTSR *szArglist = ::CommandLineToArghvW(::GetCommandLine(), &Count);
    Status = pt_analyse->Run(szArglist, Count);
    LocalFree(szArglist);
    delete pt_analyse;
    
    return Status;
}

From code 5 :

Choosing encode / decoding is done by the detection of the file extension.

C++
bool Analyse_back_groung::Traitement_fichier(CString chaine)
{

    if (pt_fichier->Test_Extention(chaine, ext) == TRUE)
    {
        // Le fichier est a decoder
        pt_decode->MyDecryptFile(chaine.GetBuffer(), (pt_finchier->Supprime_Extension(chaine)).GetBuffer(), (pt_pass->Get_Password()).GetBuffer());
    }
    else
    {
        // le fichier est a encoder
        pt_decode->MyDecryptFile(chaine.GetBuffer(), (pt_finchier->Ajoute_Extension(chaine, ext)).GetBuffer(), (pt_pass->Get_Password()).GetBuffer());
    }
    
    return true;
}

Evolution Program

We can evolve the basis provided, here are some ideas:

  1. The project is built with static libraries and we can evolve to use dynamic libraries. In general, Microsoft gave us tools to includie links to an application ("C: \ Program Files (x86) \ Windows Kits \ 8.0 \ Tools \ x86 \ depends.exe"). It is part of Kit SDK for Windows.
  2. The chosen password is in the "key.txt" file. I chose a simple password (we can change it easily) it must simply regenerate the entire project.
  3. The selected encryption method is simple, Microsoft provides more robust encryption modes. We must change the encryption mode in two files (C_AES_encode.cpp and C_AES_Decode.cpp). The definition is in stdafx.h.
  4. You can change both encoding files (C_AES_encode.cpp and C_AES_Decode.cpp). by calls from the library to CryptoLib++, for example.

Structure of Project

The complete project is provided is composed of several elements:

  1. AES: the complete project with source
  2. DLL: All necessary "dll" in debug mode and release
  3. Documentation: The word presentation of the project file
  4. Reg: File base register included when the Setup generation phase,
  5. Setup: Complete construction project.

Image 4

Project AES

Here are highlights of project options in Visual Studio 2013

Image 5

Registry file

This file contains several distribution elements:

  1. Run program in admin mode
  2. Disable UAC,
  3. Create a class ".yltr".

This file is on the directory Reg. This name is "AEs Register.reg"

Conclusion

We could have chosen to have a password in clear text in the executable, but this was not the initial purpose. I just wanted to show you that you could add a more user friendly interface to this program.

License

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