decrypt_stream_asym.cpp

Voici un exemple de programme permettant de déchiffrer un buffer mémoire (algorithme asymé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-ecc.h>

s2byte decrypt_stream_asym()
{
    u2byte  ret;

    /* set algo
     */
    s2byte  algo    = ECCTK_ECC_ALGO_PSEC3;

    /* set private key file name
     */
    s1byte  kprvfile[]  = "private_key_file";

    /* set input stream value, and create output buffer
     */
    u1byte  in[] = {0x41, 0x00, 0x7c, 0x01, 0x03, 0x02, 0x10, 0x08,
                    0x05, 0x26, 0x98, 0xad, 0xef, 0x56, 0x21, 0xd5,
                    0x1e, 0xff, 0x04, 0xb1, 0x4d, 0x3d, 0xa1, 0x62,
                    0x3f, 0x97, 0x94, 0x27, 0xc1, 0xc4, 0x07, 0xfe,
                    0x2a, 0x8a, 0x05, 0x2f, 0x4a, 0x7c, 0x75, 0x85,
                    0x81, 0x08, 0x59, 0x89, 0x13, 0xfe, 0x19, 0x1e,
                    0x21, 0xd5, 0x4d, 0xcc, 0xa0, 0x0e, 0xae, 0x62,
                    0x0b, 0x4f, 0x87, 0xf6, 0xb2, 0x5a, 0xc4, 0x1e,
                    0x8b, 0x3f, 0xca, 0x9c, 0xd1, 0xe6, 0x55, 0x18,
                    0xe6, 0x36, 0x9e, 0x64, 0xa5, 0x03, 0x2c, 0x9b,
                    0x2f, 0xba, 0x08, 0xcb, 0xa7, 0xdd, 0x2c, 0xd4,
                    0xcb, 0x9f, 0xa8, 0x7d, 0x9d, 0x21, 0xa6, 0x4c,
                    0xe6, 0xea, 0x8b, 0xf2, 0xe6, 0xb6, 0x49, 0x4f,
                    0xbc, 0xe5, 0x4f, 0x4c, 0xfb, 0x4b, 0xad, 0x56,
                    0x1e, 0x1b, 0xa6, 0x6e, 0xc6, 0x2d, 0xdb, 0x41,
                    0x59, 0x85, 0x06, 0x42, 0xe8, 0x40, 0x21, 0x3b,
                    0x43, 0x0f, 0x4f, 0xd0, 0xf0, 0xb4, 0x67, 0x58,
                    0xd3, 0x7a, 0x61, 0x4c, 0xe0, 0xd0, 0xcb, 0xd5,
                    0x7a, 0x22, 0x21, 0x53, 0x45, 0x49, 0x35, 0xc6,
                    0xb1, 0xec, 0x1b, 0x71, 0xaa, 0x97, 0x99, 0x2a},
            *out;

    /* used to get the size of output buffer
     */
    s4byte  outsize;

    /* private key context
     */
    KPRV_CTX *kprv;
    
    /* read private key on file (protected by password : "1e4s!5,Ht4")
     */
    kprv = ReadFileKprvCtx(kprvfile);
    if (kprv == NULL)
    {
        fprintf(stderr, "Read private key on %s failed\n", kprvfile);
        return EXIT_FAILURE;
    }

    /* Initialize value for all curve in memory
     */
    ret = InitializeEcctkAll();
    if (ret != ECCTK_ECC_NO_ERROR)
    {
        fprintf(stderr, "InitializeEcctkAll failed (%d)\n", ret);
        return EXIT_FAILURE;
    }

    switch (algo)
    {
    case ECCTK_ECC_ALGO_ECIES:
        out = DecryptStreamEcies(in, sizeof(in), &outsize, kprv, "1e4s!5,Ht4");
        break;
    case ECCTK_ECC_ALGO_PSEC3:
        out = DecryptStreamPsec3(in, sizeof(in), &outsize, kprv, "1e4s!5,Ht4");
        break;
    default:
        out = NULL;
        break;
    }

    if (out == NULL)
        fprintf(stderr, "Decryp stream failed\n");
    else
        fprintf(stdout, "Decrypt stream success, result stream size = %ld\n", outsize);

    free(out);

    /* free context
     */
    KprvCtxFree(kprv);

    /* remove value for all curve in memory
     */
    FreeEcctkAll();

    return EXIT_SUCCESS;
}


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