GMP - integer import and export

Erik Johnson
Mon, 18 Nov 2002 15:53:05 -0600

I'm using the GMP library for an demonstration of various cryptographic 
algorithms for an independent study project. I seem to have a problem, 
however, with the integer import and export functions. Basically, I am 
reading in binary data, using the import function to convert it to the GMP 
multi-precision number format, performing the algorithm, and then using the 
export function to convert the data back to binary and write it out to the 
destination file.

I have no problems with text files, the algorithms encrypt and decrypt them 
perfectly. The problem is this: when using characters with an ANSI value 
higher than 127, the decrypted result does not match the original file. So, 
binary files like wav files, PDFs, etc. don't work, and text files with 
special characters (accented letters, etc.) also do not. Since these 
characters do not work, but regular text does, I figure it is likely a 
problem with the conversion function not properly converting the unsigned 
char data to the GMP format.  Like it treats everything as a signed char or 
something when converting.  My buffer is an unsigned char array, so I know 
that the buffer is not the problem.

Can anyone else that has used GMP offer any insight into this problem?

Code sample is below.


  BUFFER_SIZE is a const int.
  padding is an integer value calculated earlier in the program.

    unsigned char buffer[BUFFER_SIZE];

    for(int i=1; i<=num_blocks; i++)
        if(a == 'E' && i == num_blocks)
            // If the algorithm is encrypting, and this is the last block,
            // Read the last bytes from the input file.
            // Read a full block.

        // Convert to MP int.

        // Encrypt or decrypt the block.

        // Convert the block back to binary.

        if(a == 'D' && i == num_blocks)
            // If the algorithm is decrypting, and this is the last block,
            // then disregard the padding when writing.
            out.write((char *)&buffer,(BUFFER_SIZE-padding));
            // Write the data out to the destination file.
            out.write((char *)&buffer,BUFFER_SIZE);

STOP MORE SPAM with the new MSN 8 and get 2 months FREE*