problem

This is a discussion on problem within the C Programming forums, part of the General Programming Boards category; So I came up with this simple encrypt/decrypt algorythm: void code(char *in, int size) { while(x < size) in[x++] += ...

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    problem

    So I came up with this simple encrypt/decrypt algorythm:

    void code(char *in, int size)
    {
    while(x < size)
    in[x++] += in[x] < 64? 64: -64;
    }

    ...and it works about 95%-99% correctly!!! Why?

    When I fread into a buffer, "code()", fwrite, then repeat the process, opening the file to view it I notice some characters have changed into sort of wing-dings. Not what I intended...and it absolutely corrupts image files because of this...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    encrypting image files, eh? heh heh...

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hey, cut me some slack, I want it to work with ALL files!!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    //edit: I'm stupid, I missed a plus sign.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Hmm....

    It would be worth you seeing if the char it started to choke on was '@'......

    Just a theory......

    ASCI 64 = '@'........

    so

    in[x++] += in[x] < 64? 64: -64;

    is

    64 += 65 <64?:-64; == 64 - 64....

    Therefore the char is now 0....which in ASCII is NULL......so when you do something with that byte maybe its causing problems ???

    May be wrong, but have a look at what char is causing trouble

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >and it absolutely corrupts image files because of this...

    Your algorithm is only going to work for text files (characters in the 0-127 range). For image files, you need to change your algorithm a bit.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    then there isn't a type of alg that would work with ALL files?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You might try this.
    Code:
    void code(char *in, int size)
    {
       int x = 0; 
       while(x < size)
       {
          if (in[x] < 0)
             in[x++] -= -128;
          else
             in[x++] += -128;
       }
    }
    For a char, there is 256 possible values, so you kind of need to be able to divide this into two halfs, a top half and a bottom half. That way the algorithm knows whether to add or subtract.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21