Thread: GCC Problem Windows/Linux Encryption Program

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    5

    GCC Problem Windows/Linux Encryption Program

    I have the following decryption code which compiles correctly using both GCC in windows (using MingW) and in ubuntu linux. However, only the windows code works correctly to decrypt an encrypted file. The code is exactly the same in both. I am stumped and as we are moving our server to linux we need to have the decryption program working on linux. Any ideas or help is greatly appreciated.

    Here is the code:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    
    void decode(unsigned long* v, unsigned long* k);
    void code(long *v, long *k);
    
    
    //int main(int argc, char *argv[]) {
    int main() {
        // FILE *incoming;
        FILE *outgoing;
        // FILE *outbound;
        FILE *inbound;
    
    
        //unsigned char input[9]="";
        unsigned char incipher[15];
        unsigned char unscramble[15];
        char file_select1[15] = "";
        char file_select2[15] = "";
    
    
        //  char spacer = ' ';
        int current = 0;
        unsigned long output[2], key[4];
    
    
        int i = 0;
    
    
        key[0] = 37444384;
        key[1] = 02229721;
        key[2] = 21338835;
        key[3] = 97034809;
    
    
        current = 0;
    
    
        //sprintf(file_select1, "%s", argv[1]);
        inbound = fopen("/home/parallels/codeblocks/encrypt/newtea/bin/Debug/TEST.TXT","r");
    
    
    
    
        if(NULL == inbound)
        {
            printf("\n fopen() inbound Error!!!\n");
            return 1;
        }
    
    
        //sprintf(file_select2, "%s", argv[2]);
        //outgoing = fopen("track.txt", "wb");
        outgoing = fopen("/home/parallels/codeblocks/encrypt/newtea/bin/Debug/DECRYPT.txt","w");
    
    
    
    
        if(NULL == outgoing)
        {
            printf("\n fopen() outgoing Error!!!\n");
            return 1;
        }
    
    
        while (current == 0) {
    
    
            if (fread(incipher, 8, 1, inbound) != 0) {
                output[0] = incipher[0];
                output[0] += incipher[1] << 8;
                output[0] += incipher[2] << 16;
                output[0] += incipher[3] << 24;
    
    
                output[1] = incipher[4];
                output[1] += incipher[5] << 8;
                output[1] += incipher[6] << 16;
                output[1] += incipher[7] << 24;
            } else
                break;
    
    
    
    
            decode(output, key);
    
    
            unscramble[0] = output[0] & 0xFF;
            unscramble[1] = (output[0] >> 8) & 0xFF;
            unscramble[2] = (output[0] >> 16) & 0xFF;
            unscramble[3] = (output[0] >> 24) & 0xFF;
    
    
            unscramble[4] = output[1] & 0xFF;
            unscramble[5] = (output[1] >> 8) & 0xFF;
            unscramble[6] = (output[1] >> 16) & 0xFF;
            unscramble[7] = (output[1] >> 24) & 0xFF;
    
    
            for (i = 0; i < 8; i++)
                fputc(unscramble[i], outgoing);
    
    
        }
    
    
        fclose(outgoing);
        fclose(inbound);
    
    
        return 0;
    
    
    }
    
    
    void decode(unsigned long* v, unsigned long* k) {
        unsigned long n = 32, sum, y = v[0], z = v[1], delta = 0x2e1889b4;
        sum = delta << 5;
    // start cycle //
        while (n-- > 0) {
            z -= ((y << 4) + k[2]) ^ (y + sum) ^ ((y >> 5) + k[3]);
            y -= ((z << 4) + k[0]) ^ (z + sum) ^ ((z >> 5) + k[1]);
            sum -= delta;
        }
    // end cycle //
        v[0] = y;
        v[1] = z;
    }
    
    
    void code(long *v, long *k) {
    
    
        unsigned long y = v[0], z = v[1], sum = 0, /* set up */
        delta = 0x2e1889b4, /* a key schedule constant */
        n = 32;
    
    
        while (n-- > 0) { /* basic cycle start */
            sum += delta;
            y += ((z << 4) + k[0]) ^ (z + sum) ^ ((z >> 5) + k[1]);
            z += ((y << 4) + k[2]) ^ (y + sum) ^ ((y >> 5) + k[3]);
        } /* end cycle */
        v[0] = y;
    
    
        v[1] = z;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest comparing sizeof(unsigned long) on your Windows and Linux installations to see if there is a difference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Try to open the files in binary mode - call fopen() with "rb" or "wb" flag.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Tried this already and it makes no difference

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    I suspect this is the problem. I'm not quite sure how to go about fixing it though - will experiment. Thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... so sizeof(unsigned long) results in the same value for both?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    nope - you've nailed it. sizeof(unsigned long) is twice the size on linux gcc as it is on windows

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    5
    Is there an option to compile using 4 byte longs instead of 8 byte longs?

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Try using fixed size integers.
    <stdint.h>
    It should be portable in theory, but I'm not very sure about its availability in a windows environment.

    [Edit: gcc does have an option to force long size! "-mlong32"]
    Last edited by manasij7479; 04-29-2013 at 09:51 AM.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Code:
    key[1] = 02229721;
    Good constant

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Shurik View Post
    Code:
    key[1] = 02229721;
    Good constant
    I am guessing you meant bad constant.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    gcc also has an int64_t type that is explicitly 64 bits wide.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-08-2012, 05:37 PM
  2. Replies: 11
    Last Post: 01-20-2009, 02:13 PM
  3. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 04:41 AM
  4. Running C program in Linux or Windows
    By kepler in forum C Programming
    Replies: 4
    Last Post: 09-30-2003, 08:31 AM
  5. Encryption Program Problem
    By WhoCares in forum C++ Programming
    Replies: 0
    Last Post: 04-01-2002, 11:50 AM