Encrypt message by private key

ronron78
Posts: 1
Joined: Sun Sep 15, 2019 6:00 pm

Encrypt message by private key

Postby ronron78 » Sun Sep 15, 2019 6:18 pm

I'm looking to encrypt a message with my private key but I can not do it. Somebody help me correct my code?

Code: Select all


#include "mbedtls/aes.h"
#include "mbedtls/ssl.h"
#include "mbedtls/pk.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"


unsigned char encrypted[MBEDTLS_MPI_MAX_SIZE];
unsigned char decrypted[MBEDTLS_MPI_MAX_SIZE];

unsigned char buf[6000];
unsigned char bufpub[6000];

void setup() {
  // Init buffer
    memset(encrypted, 0, sizeof(encrypted));
    memset(decrypted, 0, sizeof(decrypted));

   /*
   * GENEREATE KEY
   * 
   */
    int ret = 0;
    mbedtls_pk_context pk;
    mbedtls_ctr_drbg_context ctr_drbg;
    mbedtls_entropy_context entropy;
    mbedtls_pk_init( &pk );
    printf("Create entropy\n");
    mbedtls_ctr_drbg_init( &ctr_drbg );
    mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, 0, 0 );
    /* Generate Keys */
    
    printf("generate KEY\n");
    mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ));
    mbedtls_rsa_gen_key( mbedtls_pk_rsa( pk ), mbedtls_ctr_drbg_random, &ctr_drbg, 2048 , 65537 );

    printf("generate OK\n");
    int re=mbedtls_pk_write_key_pem(&pk,buf,6000);
    printf("Private Key(%d):%s\n",re,buf);
    re=mbedtls_pk_write_pubkey_pem(&pk,bufpub,6000);
    printf("Public Key(%d):%s\n",re,buf);

    
    /*
     * Read the RSA private key
     */
    mbedtls_pk_context pk_private;
    mbedtls_pk_init( &pk_private );
    if( ( ret = mbedtls_pk_parse_key( &pk_private, buf, strlen((char *)buf)+1,(unsigned char*)"",1 ) ) != 0 ){
        printf( " failed\n  ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
      
    }
    printf("Priv Key loaded...\n");
    
    
    size_t olen = 0;
    unsigned char to_encrypt[] = "This is a test..";
    size_t to_encrypt_len = sizeof(to_encrypt);
    
    mbedtls_ctr_drbg_context ctr_drbg_crypt;
    mbedtls_entropy_context entropy_crypt;
    char *personalization = "my_app_specific_string";
    mbedtls_entropy_init( &entropy_crypt );
    mbedtls_ctr_drbg_init( &ctr_drbg_crypt );
    mbedtls_ctr_drbg_seed( &ctr_drbg_crypt, mbedtls_entropy_func, &entropy_crypt, 0, 0 );

    ret = mbedtls_ctr_drbg_seed( &ctr_drbg , mbedtls_entropy_func, &entropy_crypt,
                     (const unsigned char *) personalization,
                     strlen( personalization ) );

    /*if( ( ret = mbedtls_pk_encrypt( &pk_private, to_encrypt, to_encrypt_len,
                                    encrypted, &olen, sizeof(encrypted),
                                    mbedtls_ctr_drbg_random, &ctr_drbg_crypt ) ) != 0 ) {
        printf( " failed\n  ! mbedtls_pk_encrypt returned -0x%04x\n", -ret );
        return;
    }*/
    if( ( ret = mbedtls_pk_encrypt( &pk_private, to_encrypt, to_encrypt_len,
                                    encrypted, &olen, sizeof(encrypted),
                                    mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) {
        printf( " failed\n  ! mbedtls_pk_encrypt returned -0x%04x\n", -ret );
        return;
    }
    printf("Encrypted...\n");
    printf("encrypted: %i \n", sizeof(encrypted));
    printf("Olen: %i \n", olen);
    
    int i;
    for( i = 0; i < 256; i++ ){
      printf( "%02x[%c]%c", encrypted[i], (encrypted[i]>31)?encrypted[i]:' ', ((i&0xf)!=0xf)?' ':'\n' );
    }
    printf( "\n" );







  /*
   * DECRYPT
   * 
   */


    /*
     * Read the RSA public key
     */
     mbedtls_pk_context pk_public;
     mbedtls_pk_init( &pk_public );
    if( ( ret = mbedtls_pk_parse_public_key( &pk_public, bufpub,strlen((char *)bufpub) ) ) != 0 )
    {
        printf( " failed\n  ! mbedtls_pk_parse_public_keyfile returned     dd 0x%04x\n", -ret );
         return;
    }
    printf("Pub Key loaded...\n");
    


    /*
     * Calculate the RSA encryption of the data.
     */
    printf( "\n  . Decrypting...\n" );
    fflush( stdout );

    static unsigned char decrypted[1024];
    printf("Decrypted: %i \n", sizeof(decrypted));
    if( ( ret = mbedtls_pk_decrypt( &pk_public, encrypted, olen, decrypted, &olen, sizeof(decrypted),
                                    mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) {
        printf( " failed\n  ! mbedtls_pk_decrypt returned -0x%04x\n", -ret );
 
    }
    printf("Decrypted...\n");
    printf("Decrypted Text: %s \n", decrypted);
    
    for( i = 0; i < 128; i++ ){
        printf( "%02x[%c]%c", decrypted[i], (decrypted[i]>31)?decrypted[i]:' ', ((i&0xf)!=0xf)?' ':'\n' );
    }
    printf( "\n" );

    return;
    
}

void loop() {
  // put your main code here, to run repeatedly:

}

Who is online

Users browsing this forum: No registered users and 50 guests