decrypt_stream.cpp

Voici un exemple de programme permettant de déchiffrer un buffer mémoire (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 decrypt_stream()
{
    /* set algo and decryption 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 stream value, and create output buffer with necessary size
     */
    u1byte  in[]    = { 0x24, 0x34, 0x16, 0x33, 0x0e, 0xe5, 0x03, 0x41,
                        0x19, 0x77, 0x7d, 0x17, 0x5d, 0x13, 0xc1, 0x93,
                        0xf7, 0xf2, 0x56, 0x31, 0x45, 0xe4, 0xe5, 0xbf,
                        0x46, 0x97, 0x43, 0x1d, 0xc0, 0x8e, 0x70, 0xd3,
                        0x87, 0x72, 0x72, 0x14, 0x9a, 0x55, 0xbd, 0x38,
                        0xae, 0x17, 0x16, 0xe2, 0x21, 0x64, 0xf2, 0xa2},
            out[48];
    
    s4byte  ret     = -1,
            outsize = sizeof(out);

    /* 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_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key)/4, iv, sizeof(iv)/2, 1, mode);
            break;
        case ECCTK_CIPHER_ALGO_3DES_EDE_2:
            ret = DES_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key)/2, iv, sizeof(iv)/2, 2, mode);
            break;
        case ECCTK_CIPHER_ALGO_3DES_EDE_3:
            ret = DES_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv)/2, 3, mode);
            break;
        case ECCTK_CIPHER_ALGO_RC2:
            ret = RC2_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key)/2, iv, sizeof(iv)/2, mode);
            break;
        case ECCTK_CIPHER_ALGO_RC4:
            ret = RC4_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key));
            break;
        case ECCTK_CIPHER_ALGO_CAST128:
            ret = CAST128_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key)/2, iv, sizeof(iv)/2, mode);
            break;
        case ECCTK_CIPHER_ALGO_BLOWFISH:
            ret = BLOWFISH_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv)/2, mode);
            break;
#ifndef ECCTK_EVALUATION
        case ECCTK_CIPHER_ALGO_MARS:
            ret = MARS_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv), mode);
            break;
        case ECCTK_CIPHER_ALGO_SERPENT:
            ret = SERPENT_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv), mode);
            break;
        case ECCTK_CIPHER_ALGO_TWOFISH:
            ret = TWOFISH_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv), mode);
            break;
        case ECCTK_CIPHER_ALGO_RIJNDAEL:
            ret = RIJNDAEL_StreamDecrypt(in, sizeof(in), out, &outsize, key, sizeof(key), iv, sizeof(iv), mode);
            break;
#endif
        default:
            ret = -1;
            break;
    }

    if (ret != ECCTK_CIPHER_NO_ERROR)
        printf("Decrypt stream failed (Error : %ld)\n", ret);
    else
        printf("Decrypt stream success, result stream size = %ld\n", outsize);
}

Généré pour ECCTK (Elliptic Curve Cryptographic ToolKit) avec  doxygen