Hi,

I was wondering if somebody could help me or point me to the right place
the get help.

I'm having problems using the RSA routines from openssl 0.9.4.  I've got
a very simple C program which generates and RSA key (I'm not worried
about the randomness of the key at this stage) and then proceeds to
read data from a file encrypting the data and then decrypting it and
comparing the output with the original text. (See attached C file)

Whenever I pass straight ASCII text to the program it works fine and all
the output matches the input.  However, when I pass a binary file the
first dozen or so blocks encrypt and decrypt fine, but after that I get
*some* blocks (on some files it can be most blocks but not all) that
don't decrypt back to the original data.  

It varies from file to file, but usually there will be blocks that don't
en/decrypt properly and some that will, these are interspersed
throughout the file (ie I'll get a series of corrupt blocks then a
series of good ones, etc).

Hope this makes sense.  Any and all help appreciated.
thanx

Simon Edwards                \\ EMail: [EMAIL PROTECTED]
Oracle DBA  and  WebMaster    \\ #include <std.disclaimer>    TFIAB
Information Management         \\  Osborn's Law: Variables won't;
Education Queensland            \\                constants aren't.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>


RSA *rsa;
FILE *infile;
unsigned char *plain, *cypher;
unsigned char *plain2;
int rsa_size;



/*==============================================================================
 *  Main program
 *==============================================================================
 */
int main (
int argc,
char *argv[])
{
  long len;
  int num_items;
  unsigned int i, j;

  if (argc != 2) {
    printf("USAGE: %s <inpfile.txt>\n", argv[0]);
    return 20;
  }

  /* open input and output files */
  if ((infile = fopen(argv[1], "r")) == NULL) {
    printf("Unable to open input file for reading\n");
    exit(30);
  }

  rsa = RSA_generate_key(64, 0x10001, NULL, NULL);
  rsa_size = RSA_size(rsa);

  if ((plain2 = (unsigned char *) malloc(102400)) == NULL) {
    printf("malloc failed\n");
    exit(30);
  }
  if ((plain = (unsigned char *) malloc(102400)) == NULL) {
    printf("malloc failed\n");
    exit(30);
  }

  if ((cypher = (unsigned char *) malloc(102400)) == NULL) {
    printf("malloc failed\n");
    exit(30);
  }

  printf("Encrypting data...\n");
  while (1) {

    num_items = fread(plain, sizeof(unsigned char), rsa_size, infile);

    printf("Encrypting block rsa_size = %d\n", rsa_size);

    /* encrypt block */
    if ((len = RSA_public_encrypt(rsa_size, plain, cypher, rsa, RSA_NO_PADDING)) == -1 
) {
      printf("Error encrypting data\n");
      exit(10);
    }

    /* decrypt block */
    if ((len = RSA_private_decrypt(rsa_size, cypher, plain2, rsa, RSA_NO_PADDING)) == 
-1 ) {
      printf("Error decrypting data");
      exit(16);
    }
    if (len != rsa_size) printf("decrypted size does not match encrypted size\n");
    for (j=0; j < rsa_size; j++) {
      if (plain[j] != plain2[j]) {
        printf("bytes %d do not compare equal, %02x %02x\n", j, plain[j], plain2[j]);
      }
    }


    if (feof(infile)) break;
  }

  return 0;
}

Reply via email to