2012-05-24 13 views
5

Próbuję wygenerować klucz publiczny RSA z modułu typu char [], a teraz wykładnikiem jest RSA_F4 (65537); Ale gdy próbuję wygenerować mój klucz publiczny przy użyciu tych wartości dla "n" i "e", RSA_public_encrypt, return -1;Jak utworzyć klucz publiczny RSA z unsigned char * modulus i wykładnika 65537 (RSA_F4)

Dzięki!

Mój kod:

#include <string.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <crypt.h> 
#include <iostream> 
#include <stdlib.h> 
#include <openssl/rsa.h> 
#include <openssl/aes.h> 
#include <openssl/opensslconf.h> 
#include <openssl/engine.h> 
#include <openssl/pem.h> 
#include <openssl/rc4.h> 

using namespace std; 
int main(void) 
{ 

//modulus in format char hex; 
char key[] = "C0E7FC730EB5CF85B040EC25DAEF288912641889AD651B3707CFED9FC5A1D3F6C40062AD46E3B3C3E21D4E71CC4800C80226D453242AEB2F86D748B41DDF35FD"; 

    char palavra[] = "teste"; 
    char crip[512]; 
    int ret; 
    RSA * pubkey = RSA_new(); 
    BIGNUM * modul = BN_new(); 
    BIGNUM * expon = BN_new(); 

    BN_hex2bn(&modul, (const char *) key); 
    BN_hex2bn(&expon, "010001"); 

    cout << "N KEY: " << BN_bn2hex(modul) << endl; 
    cout << "E KEY: " << BN_bn2hex(expon) << endl; 

    pubkey->n = modul; 
    pubkey->e = expon; 

    cout << "N PUB KEY: " << BN_bn2hex(pubkey->n) << endl; 
    cout << "E PUB KEY: " << BN_bn2hex(pubkey->e) << endl; 

    if (RSA_public_encrypt(strlen((const char *) palavra), (const unsigned char *) palavra, (unsigned char *) crip, pubkey, RSA_PKCS1_PADDING)) 
    { 
     printf("ERRO encrypt\n"); 
    } 
    else 
    { 
     printf("SUC encrypt\n"); 
    } 
return 0; 
} 
+0

pojawiasz się czytać moduł w 'x', która nigdy nie deklarują, pozostawiając' modul' jako 0. ty też nigdy umieścić moduł w 'pubkey', więc prawdopodobnie nadal jest puste, co powoduje błąd, gdy y spróbuj wywołać 'RSA_public_encrypt'. Jeśli potrzebujesz prawdziwej pomocy, musisz opublikować prawdziwy kod, który próbujesz skompilować i uruchomić. –

+0

Przepraszam, ale mój ostatni wpis został wstawiony za pomyłkę. –

Odpowiedz

2

Prawdopodobnie chcesz coś, co wygląda bardziej jak:

RSA *pubkey = RSA_new(); 
int len = BN_hex2bn(&pubkey->n, (const char *)p); 
if (len == 0 || p[len]) 
    fprintf(stderr, "'%s' does not appear to be a valid modulus\n", p); 
BN_hex2bn(&pubkey->e, "010001"); 

edycji

Twój kod działa poprawnie, z wyjątkiem kontroli błędów. RSA_public_encrypt zwraca rozmiar szyfrogram o sukcesie, a nie 0, tak aby powyższy kod pracy, dodaj <= 0 testy do if wiersz:

if (RSA_public_encrypt(....) <= 0) 
+0

Przepraszam, ale mój ostatni wpis został wstawiony za pomyłkę. Teraz moje pytanie jest kompletne, czy możesz mnie uratować? Dzięki! –

1

Oto kod C, który załatwia sprawę:

#include <string.h> 
#include <openssl/rsa.h> 
#include <openssl/evp.h> 
#include <openssl/bn.h> 
#include <openssl/pem.h> 

// cheating, .. ignoring deprecation warnings 
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" 

unsigned char *base64_decode(const char* base64data, int* len) { 
    BIO *b64, *bmem; 
    size_t length = strlen(base64data); 
    unsigned char *buffer = (unsigned char *)malloc(length); 
    b64 = BIO_new(BIO_f_base64()); 
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); 
    bmem = BIO_new_mem_buf((void*)base64data, length); 
    bmem = BIO_push(b64, bmem); 
    *len = BIO_read(bmem, buffer, length); 
    BIO_free_all(bmem); 
    return buffer; 
} 

BIGNUM* bignum_base64_decode(const char* base64bignum) { 
    BIGNUM* bn = NULL; 
    int len; 
    unsigned char* data = base64_decode(base64bignum, &len); 
    if (len) { 
     bn = BN_bin2bn(data, len, NULL); 
    } 
    free(data); 
    return bn; 
} 

EVP_PKEY* RSA_fromBase64(const char* modulus_b64, const char* exp_b64) { 
    BIGNUM *n = bignum_base64_decode(modulus_b64); 
    BIGNUM *e = bignum_base64_decode(exp_b64); 

    if (!n) printf("Invalid encoding for modulus\n"); 
    if (!e) printf("Invalid encoding for public exponent\n"); 

    if (e && n) { 
     EVP_PKEY* pRsaKey = EVP_PKEY_new(); 
     RSA* rsa = RSA_new(); 
     rsa->e = e; 
     rsa->n = n; 
     EVP_PKEY_assign_RSA(pRsaKey, rsa); 
     return pRsaKey; 
    } else { 
     if (n) BN_free(n); 
     if (e) BN_free(e); 
     return NULL; 
    } 
} 

void assert_syntax(int argc, char** argv) { 
    if (argc != 4) { 
     fprintf(stderr, "Description: %s takes a RSA public key modulus and exponent in base64 encoding and produces a public key file in PEM format.\n", argv[0]); 
     fprintf(stderr, "syntax: %s <modulus_base64> <exp_base64> <output_file>\n", argv[0]); 
     exit(1); 
    } 
} 

int main(int argc, char** argv) { 
    assert_syntax(argc, argv); 

    const char* modulus = argv[1]; 
    const char* exp = argv[2]; 
    const char* filename = argv[3]; 

    EVP_PKEY* pkey = RSA_fromBase64(modulus, exp); 

    if (pkey == NULL) { 
     fprintf(stderr, "an error occurred :(\n"); 
     return 2; 
    } else { 
     printf("success decoded into RSA public key\n"); 
     FILE* file = fopen(filename, "w"); 
     PEM_write_PUBKEY(file, pkey); 
     fflush(file); 
     fclose(file); 
     printf("written to file: %s\n", filename); 
    } 

    return 0; 
} 

Zobacz ten link w celu uzyskania dalszych informacji: http://www.techper.net/2012/06/01/converting-rsa-public-key-modulus-and-exponent-into-pem-file/

+0

Witam Jestem w stanie napisać public pem, ale otrzymuję pusty klucz prywatny. tutaj jest mój kod - char szModulus = "1162"; char * szExp = "827655"; RSA rsa1 = RSA_new(); int ret = BN_hex2bn (& rsa1-> n, szModulus); ret = BN_hex2bn (& rsa1-> d, szExp); FILE * fp; fp = fopen ("/ Users/ysi/Desktop/privateKey.pem", "wb"); .Proszę pomóż mi. Dziękuję Ci –

Powiązane problemy