Dear All,

I am sorry to bother you...I need your help...

Have you faced such problem - loose some data at the end of a buffer...

The code is like this
I want to encrypt a file 1024 blocks by 1024 blocks
and at the final block which is not 1024 blocks will do the final management
like follows:

============================================================================
        unsigned char inbuf[1024], outbuf[1024];

        // Obtain the file size
        fseek(fin, 0, SEEK_END);
        size = ftell(fin);
        rewind(fin);

        buffer = (char *)malloc(size);
        output = (char *)malloc(size);
        int shift = 0;
        int kBufferSize = 1024;

        fread(buffer, 1, size, fin);
        if (buffer == NULL) exit(2);

        // THE PROBLEM STARTS HERE, TAKE 1024 BYTES SLIDINGLY
        while (shift < size) {
                inlen = 0;
                for (int i=0; i<kBufferSize; i++) {
                        if (i+shift < size){
                                inbuf[i] = (unsigned)buffer[i+shift];
                                inlen++;
                        }
                }
                if (!EVP_DecryptUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) {
                        printf("ERROR 1");
                        exit(-1);
                }
                for (int j=0; j<outlen; j++)
                        if (j+shift < size) {
                                output[j+shift] = (char)outbuf[j];
                                printf("%c", output[j+shift]); //PRINT 1
                        }
                shift+=kBufferSize;
        }
        if (!EVP_DecryptFinal(&ctx, outbuf, &outlen)) {
                printf("ERROR 2");
                exit(-1);
        }
        for (int k=0; k<outlen; k++) {
                output[k+shift] = (char)outbuf[k];
                printf("%c", output[k+shift]); //PRINT 2
        }
        printf("\nsize:%d\n", size); // PRINT 3
        printf("output size:%d\n", strlen(output)); // PRINT 4
        for (int x=0; x<size; x++) {
                printf("%c", output[x]); // PRINT 5
        }
        fwrite(output, 1, size, fout);
============================================================================
===

        You don't have to look into the codes. When I try to deal with a file
"test.txt" which has a sentence "This is a test.". The problem is at the end
of the code, it prints out:

This is a test. //THIS IS THE CORRECT DATA IN OUTPUT BUFFER (PRINT 1 & 2)
size: 16 //THIS IS THE SIZE OF THE FILE WHICHI IS CORRECT TOO (PRINT 3)
output size: 8 //NOW I DON'T UNDERSTAND WHY HERE... (PRINT 4)
This is //THERE IS SOME DATA MISSED IN THE OUTPUT BUFFER (PRINT 5)

        Please help me if you have the time. I don't know why there is some data
lost in the buffer...

        Thousand thanks. Wish you all the best.


Best regards,
--------------------------------------------
Jordan Cheun Ngen, Chong
INF-4067 Universiteit Twente
Postbus 217
7500 AE Enschede
The Netherlands

Distributed and Embedded Systems (DIES)
--------------------------------------------
Office Phone: +31 53 4894655
Web site: http://www.cs.utwente.nl/~chong
Email Add.: [EMAIL PROTECTED]
============================================

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to