sana
sana

Reputation: 135

RSA encryption change the data size

i used the example from the openssl demos for rsa encryption and decription , it work but the data after encryption was not the same as the text before encryption ., i need it to be the same .. so where can i modify the code so it can be return the same data size without any corruption of the encryption proccesss ., ?? Thanks in Advance ..

below is the code :

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>

#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>

#include "loadkeys.h"

#define PUBFILE   "cert.pem"
#define PRIVFILE  "privkey.pem"
#define STDIN     0
#define STDOUT    1 

int main()
{
    char *ct = "This the clear text";
char *buf;   
char *buf2;
EVP_PKEY *pubKey;
EVP_PKEY *privKey;
int len;

    ERR_load_crypto_strings();

    privKey = ReadPrivateKey(PRIVFILE);
    if (!privKey) 
{  
    ERR_print_errors_fp (stderr);    
    exit (1);  
}

    pubKey = ReadPublicKey(PUBFILE);  
if(!pubKey)
{
   EVP_PKEY_free(privKey);   
       fprintf(stderr,"Error: can't load public key");
   exit(1);
}

/* No error checking */
    buf = malloc(EVP_PKEY_size(pubKey));
    buf2 = malloc(EVP_PKEY_size(pubKey));

len = RSA_public_encrypt(strlen(ct)+1, ct, buf, pubKey->pkey.rsa,RSA_PKCS1_PADDING);

if (len != EVP_PKEY_size(pubKey))
{
    fprintf(stderr,"Error: ciphertext should match length of key\n");
    exit(1);
}

printf("%d\n", strlen(buf));
printf("%d\n", strlen(ct));
RSA_private_decrypt(len, buf, buf2, privKey->pkey.rsa,RSA_PKCS1_PADDING);

printf("%s\n", buf2);

EVP_PKEY_free(privKey);
EVP_PKEY_free(pubKey);
free(buf);
free(buf2);
    return 0;
 }

Upvotes: 0

Views: 2524

Answers (1)

WhozCraig
WhozCraig

Reputation: 66254

I'm not sure you understand how RSA encryption works.

An RSA encryption produces a block of cipher text that is as wide as the modulus of the RSA key pair. This is not a negotiable attribute of the RSA encryption/decryption process. If you're using a 1024-bit RSA key, you're going to get a 128-byte cipher text for each "block" of cipher text input, and each block can range from 1 byte up to he size of the modulus (a little less, actually, read more about PKCS#1 standards here). Likewise, a 2048-bit key will generate a 256 byte cipher text .

RSA is expensive; indeed most-all asymmetric algorithms are. For this reason is is much more common to use RSA to encrypt a symmetric algorithm key (like a AES128 key), also called a "session" key, after using said key to encrypt your actual data, then sending both the encrypted session key and the encrypted data to the recipient. If the recipient has the proper private RSA key, they can decrypt the session key, then use that to symmetrically decrypt the actual data.

If you want small blocks of encrypted data, use symmetric encryption. You have much more flexibility in choices.

For more information on RSA Encryption, see here. For information on AES encryption, a standard for symmetric encryption, see here.

Upvotes: 1

Related Questions