-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hi there

that's my first message, welcome to me :)

i'm having some troubles with this (simple) code...

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define PUB_KEY "../chiave/pubkey.pem"
#define PRIV_KEY "../chiave/keynopwd.pem"

int cifra_messaggio(char* messaggio_in_chiaro, char* messaggio_cifrato) {
        FILE* fdpub = fopen(PUB_KEY, "r");
        RSA* chiave_pubblica = RSA_new();
        PEM_read_RSA_PUBKEY(fdpub, &chiave_pubblica, NULL, NULL);
        messaggio_cifrato = malloc(RSA_size(chiave_pubblica));

        printf("Info:\tPubkey successfully loaded\n");
        printf("Info:\tMessage length is %d byte\n", 
strlen(messaggio_in_chiaro));

        int n_byte_cifrati = RSA_public_encrypt(strlen(messaggio_in_chiaro) +
1, messaggio_in_chiaro, messaggio_cifrato, chiave_pubblica,
RSA_PKCS1_OAEP_PADDING);
        if(n_byte_cifrati == 0 || n_byte_cifrati == -1) {
                printf("Err :\tUnable to encrypt\n");
                errore_non_recuperabile();
                exit(-1);
        }
        
        printf("Info:\tText successfully encrypted\n");
        printf("Info:\t%d byte encrypted\n", n_byte_cifrati);
        RSA_free(chiave_pubblica);
        fclose(fdpub);
        
        return n_byte_cifrati;
}

int decifra_messaggio(char* messaggio_cifrato, char* messaggio_decifrato) {
        FILE* fdpriv = fopen(PRIV_KEY, "r");
        RSA* chiave_privata;

        chiave_privata = PEM_read_RSAPrivateKey(fdpriv, NULL, NULL, NULL);
        messaggio_decifrato = malloc(RSA_size(chiave_privata));

        printf("Info:\tPrivkey successfully loaded\n");

        int n_byte_decifrati = RSA_private_decrypt(512, messaggio_cifrato,
messaggio_decifrato, chiave_privata, RSA_PKCS1_OAEP_PADDING);
        if(n_byte_decifrati == 0 || n_byte_decifrati == -1) {
                printf("Err :\tUnable to decrypt\n");
                
                char* errore = malloc(1024);
                bzero(errore, 1024);
                printf("%s\n", ERR_error_string(ERR_get_error(), errore));
                
                errore_non_recuperabile();
                exit(-1);
                
        }

        printf("Info:\tText successfully decripted\n");

        RSA_free(chiave_privata);
        fclose(fdpriv);

        return n_byte_decifrati;
}

int main(int argc, char* argv[]) {
        char* messaggio = "Questo è un messaggio di prova lungo qualche
decina di caratteri...";
        char* cifrato;
        char* decifrato;

        cifra_messaggio(messaggio, cifrato);
        decifra_messaggio(cifrato, decifrato);

        printf("Plaintext\t%s\n", messaggio);
        printf("Cyphertext\t%s\n", decifrato);
        
        if(strncmp(messaggio, decifrato, 512) != 0) {
                printf("Info:\tMessages are different\n");
                errore_non_recuperabile();
                exit(-1);
        }
        
        printf("Info:\tMessages are equal\n");

        return 1;
}

i got this error

error:0407A079:lib(4):func(122):reason(121)

and i'm not able to understand why...

someone can help?

regards
luca
- --
Chiave pubblica http://luca.costantino.googlepages.com/luca.costantino.asc

Prima di tutto vennero a prendere gli zingari e fui contento, perché
rubacchiavano.
Poi vennero a prendere gli ebrei e stetti zitto, perché mi stavano antipatici.
Poi vennero a prendere gli omosessuali, e fui sollevato, perché mi
erano fastidiosi.
Poi vennero a prendere i comunisti, e io non dissi niente, perché non
ero comunista.
Un giorno vennero a prendere me, e non c’era rimasto nessuno a protestare.
(Bertolt Brecht)


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Use GnuPG with Firefox : http://getfiregpg.org (Version: 0.7.10)

iQEcBAEBAgAGBQJLJBW8AAoJEFl9TsCPYHUhs64H/0u+f1cdT+sw/5YS+mxYdxaR
kqy34ULTeN5H5eC2UJmNX9nb2b9lKGbDfzb7xHB8iiMt79t3JvqVZQN4hOjGstcq
q6Na6ie8jj5MstaoRhM/kIaLye24ZTzN2CGPW0C3aN89aSGMeA7pkuaa1kiYBqus
xJurV/eNSr5qNyIQ985DRFTRWQyddi+j0MzoSECOqxwn12psTs26QPvrtoBgWghS
I6oSYFN2BMw/cCy33eEl9kWi7J5gGAmO/6SqEanpNRLAJMJn530RqKQypSjiM5OY
2Kt2UldYcgC6lYA7P1xFFtQPrq+O2Lnk6XBECL1dYxH+dFzDwRA8Do6CoW2IVYs=
=tnCI
-----END PGP SIGNATURE-----
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [email protected]
Automated List Manager                           [email protected]

Reply via email to