ECCTK (Elliptic Curve Cryptographic ToolKit): encrypt_file.cpp

# encrypt_file.cpp

Voici un exemple de programme permettant de chiffrer un fichier (algorithme symétrique). Ce code est générique et permet d'utiliser tous les algorithmes présent dans la librairie. Les éléments fixés ne le sont qu'à titre d'exemple.

    /* -----------------------------------------------------------------------
     *
     * Copyright (c) 2002-2005 Mr Ludovic FLAMENT <ludovic.flament@free.fr>, LIEVIN, FRANCE
     *
     * Termes :
     *
     * L'utilisation et/ou la redistribution de ce code (avec ou sans modification),
     * ainsi que de tous les composants du produit (librairies, documentation, exemples, ...)
     * est soumise aux termes de la licence qui vous est attribuée par son auteur.
     *
     * -----------------------------------------------------------------------
     */

    #include <stdlib.h>
    #include <stdio.h>

    #include <include/ecctk-prototype.h>
    #include <include/ecctk-cipher.h>

    void encrypt_file()
    {
        s4byte  ret     = -1;

        /* set algo and encryption mode
         */
    #ifndef ECCTK_EVALUATION
        s2byte  algo    = ECCTK_CIPHER_ALGO_RIJNDAEL,
    #else
        s2byte  algo    = ECCTK_CIPHER_ALGO_BLOWFISH,
    #endif
                mode    = ECCTK_CIPHER_MODE_CBC;

        /* set input/output file name
         */
        s1byte  infile[]    = "plain_file",
                outfile[]   = "protected_file";

        /* set a static 256 bits key and 128 bits iv
         * in case of ECCTK_CIPHER_MODE_ECB mode, iv is ignore but can be pass at all function
         */
        u1byte  key[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                         0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
                         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
                         0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
                iv[]  = {0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
                         0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00};

        switch (algo)
        {
            case ECCTK_CIPHER_ALGO_DES:
                ret = DES_FileEncrypt(infile, outfile, key, sizeof(key)/4, iv, sizeof(iv)/2, 1, mode);
                break;
            case ECCTK_CIPHER_ALGO_3DES_EDE_2:
                ret = DES_FileEncrypt(infile, outfile, key, sizeof(key)/2, iv, sizeof(iv)/2, 2, mode);
                break;
            case ECCTK_CIPHER_ALGO_3DES_EDE_3:
                ret = DES_FileEncrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv)/2, 3, mode);
                break;
            case ECCTK_CIPHER_ALGO_RC2:
                ret = RC2_FileEncrypt(infile, outfile, key, sizeof(key)/2, iv, sizeof(iv)/2, mode);
                break;
            case ECCTK_CIPHER_ALGO_RC4:
                ret = RC4_FileEncrypt(infile, outfile, key, sizeof(key));
                break;
            case ECCTK_CIPHER_ALGO_CAST128:
                ret = CAST128_FileEncrypt(infile, outfile, key, sizeof(key)/2, iv, sizeof(iv)/2, mode);
                break;
            case ECCTK_CIPHER_ALGO_BLOWFISH:
                ret = BLOWFISH_FileEncrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv)/2, mode);
                break;
    #ifndef ECCTK_EVALUATION
            case ECCTK_CIPHER_ALGO_MARS:
                ret = MARS_FileEncrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv), mode);
                break;
            case ECCTK_CIPHER_ALGO_SERPENT:
                ret = SERPENT_FileEncrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv), mode);
                break;
            case ECCTK_CIPHER_ALGO_TWOFISH:
                ret = TWOFISH_FileEncrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv), mode);
                break;
            case ECCTK_CIPHER_ALGO_RIJNDAEL:
                ret = RIJNDAEL_FileEncrypt(infile, outfile, key, sizeof(key), iv, sizeof(iv), mode);
                break;
    #endif
            default:
                break;
        }

        if (ret != ECCTK_CIPHER_NO_ERROR)
            printf("Encrypt %s failed (Error : %ld)\n", infile, ret);
        else
            printf("Encrypt %s success, %s generated\n", infile, outfile);
    }