GMP - integer import and export

Erik Johnson erik_johnson1980@hotmail.com
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.

--------------------------------------

FYI:
  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.
            inp.read((char *)&buffer,(BUFFER_SIZE-padding));
        else
            // Read a full block.
            inp.read((char *)&buffer,BUFFER_SIZE);

        // Convert to MP int.
        mpz_import(m,1,1,BUFFER_SIZE,0,0,buffer);

        // Encrypt or decrypt the block.
        mpz_powm(m,m,x,n);

        // Convert the block back to binary.
        mpz_export(buffer,&count,1,BUFFER_SIZE,0,0,m);

        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));
        else
            // 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* 
http://join.msn.com/?page=features/junkmail