Bonjour,
Je suis actuellement en train de faire un petit snippet pour encoder une chaine avec Blowfish et base64. Pour cela, j'utilise les BIO de OpenSSL.
Voici mon extrait de code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
 
#define P_BUF_SIZE 8192
#define BUF_SIZE ((P_BUF_SIZE * 3 / 4) - 24) /* 24 is the overhead of blowfish, 3/4 is the overhead of base64 */
int main(int argc, char ** argv) {
        FILE *fd;
        char *sReadBuf, *sWriteBuf;
        short iReadSize = 0;
        short result;
        BIO *b64, *bmem, *bfish, *bStart;
        BUF_MEM  *bptr;
 
        if(argc < 3) {
                fprintf(stderr, "USAGE: %s filename key\n", argv[0]);
                return 3;
        }
        fd = fopen(argv[1], "r");
        if(NULL == fd) {
                fprintf(stderr, "Can't open file \"%s\"for reading\n", argv[1]);
                return 4;
        }
 
        sReadBuf = (char *) malloc(BUF_SIZE * sizeof(char) + 1);
        sWriteBuf = (char *) malloc(P_BUF_SIZE * sizeof(char) + 1);
 
        b64 = BIO_new(BIO_f_base64());
        BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
 
        bfish = BIO_new(BIO_f_cipher());
        BIO_set_cipher(bfish, EVP_bf_cbc(), "pouet", "pouet", 1);
 
        bfish = BIO_push(bfish, b64);
 
        bmem = BIO_new(BIO_s_mem());
 
        b64 = BIO_push(b64, bmem);
 
        bStart = bfish;
 
        while(0 < (iReadSize = fread(sReadBuf, sizeof(char), BUF_SIZE, fd))) {
                result = BIO_write(bStart, sReadBuf, iReadSize);
                BIO_flush(bStart);
 
                if(result > 0) {
                        BIO_get_mem_ptr(bStart, &bptr);
                        memcpy(sWriteBuf, bptr->data, bptr->length);
                        sWriteBuf[bptr->length] = 0;
 
                        fprintf(stdout, "%s\n", sWriteBuf);
                        fflush(stdout);
 
              }
                else {
                        return 5;
                }
        }
        BIO_free_all(bStart);
        free(sReadBuf);
        free(sWriteBuf);
        fclose(fd);
 
        return 0;
}
Mon problème est que l'encodage en base64 marche très bien (j'ai testé le filtre seul, et le décodage est OK), mais que le chiffrement par blowfish... me retourne un résultat qu'il m'est impossible de décoder avec openssl en ligne de commande
# openssl enc -d -bf -a -nosalt -in /tmp/test.rand
enter bf-cbc decryption password:
bad decrypt
10468:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:454:
# openssl enc -d -bf -a -in /tmp/test.rand
enter bf-cbc decryption password:
error reading input file
J'en déduis que j'ai raté une étape... result contient bien un résultat non-nul, ce qui pourrait vouloir dire que l'encodage s'est bien passé, mais ... non.

Qqn pourrait-il m'indiquer où je fais erreur ?

Merci !
X_Cli