Like Tree3Likes

Encrypting and knowing if it's encrypted

This is a discussion on Encrypting and knowing if it's encrypted within the C Programming forums, part of the General Programming Boards category; Well, off course a better system is possible, but I think it's robust enough: the possibility of someone writing a ...

  1. #16
    C <3er
    Join Date
    Jul 2011
    Posts
    46
    Well, off course a better system is possible, but I think it's robust enough: the possibility of someone writing a phrase that starts with the magic key exists, but it is very low, even more if you think that this is a file format (the next line needn't be user input, in fact in my case it is numerical data), also the magic key is added just after the encryption is done... so I'm pretty sure that the possibility of the magic key being the same as the first encrypted blob is very very low. And much lower if you use a long magic key. So in my case, kryptkat, the possibility of that happening equals 0 because I choose the magic key and obviously if the next line is numerical data I wouldn't use much numbers in it. Also this possibility that you mention is what I wanted to solve adding the newline after the magickey but it can be accomplished just as it's been said checking for the length not the \n.

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by beta3designs View Post
    Well, off course a better system is possible, but I think it's robust enough: the possibility of someone writing a phrase that starts with the magic key exists, but it is very low, even more if you think that this is a file format (the next line needn't be user input, in fact in my case it is numerical data), also the magic key is added just after the encryption is done... so I'm pretty sure that the possibility of the magic key being the same as the first encrypted blob is very very low. And much lower if you use a long magic key. So in my case, kryptkat, the possibility of that happening equals 0 because I choose the magic key and obviously if the next line is numerical data I wouldn't use much numbers in it. Also this possibility that you mention is what I wanted to solve adding the newline after the magickey but it can be accomplished just as it's been said checking for the length not the \n.
    You can reduce that even further by deliberately breaking the rules of grammar and using statistically infrequent characters... Q} would be one example... In proper english spelling Q is always followed by a U... Most people don't use curly braces in normal text.

  3. #18
    C <3er
    Join Date
    Jul 2011
    Posts
    46
    Off course, you are right
    But what I wanted to emphasize is that it is a file and it is used for a program it is not plain text so the next line or word right after the magic key is not necessarily user input and, in my case, it is in fact program data so it is impossible that it happens to be the same as the magic key so as to say that I don't think that a better system is needed, the system seems to me robust. And as far as I'm concerned and as you have confirmed I'm not the first to use it and many image formats use it.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by beta3designs View Post
    Off course, you are right
    But what I wanted to emphasize is that it is a file and it is used for a program it is not plain text so the next line or word right after the magic key is not necessarily user input and, in my case, it is in fact program data so it is impossible that it happens to be the same as the magic key so as to say that I don't think that a better system is needed, the system seems to me robust. And as far as I'm concerned and as you have confirmed I'm not the first to use it and many image formats use it.
    Never say "impossible"... as unlikely as it is, my example corresponds to an unsigned short int with a value of 0x7D51 or 32,081 on a little endian system.

    Just to give you some idea how screwy this can get... Here's what it takes to sort out what kind of file you are opening in the wonderful world of Unicode...

    Code:
    // open and translate file
    BOOL M3ULaunch(PWCHAR FileName)
      { PBYTE  rf;      // raw file data
        DWORD  br;      // bytes read
        // load the raw file
        { HANDLE pl;    // playlist file handle 
          DWORD  fs;    // file size
          // get path to file
          wcsncpy(FilePath,FileName,MAX_PATH);
          PathRemoveFileSpec(FilePath);
          wcscat(FilePath,L"\\");
          // open the file
          pl = CreateFile(FileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
          if (pl == INVALID_HANDLE_VALUE)
            Exception(GetLastError());
          fs = GetFileSize(pl,NULL);        
          rf = calloc(fs + 2, sizeof(BYTE));
          if (! ReadFile(pl, rf, fs, &br, NULL))
            Exception(GetLastError());
          CloseHandle(pl);  
          if (br != fs)
            Exception(0xE00640007); } 
        try                                   
         { DWORD bom = *(DWORD*)rf;
           if ((bom == 0x0000FEFF) || (bom == 0xFFFE0000))  // utf32le bom  
             Exception(0xE0640002);                         // utf32be bom  
           else if ((bom & 0xFFFF) == 0xFFFE)               // utf16be bom
             { FlipEndian(rf,br);
               CopyWChar((PWCHAR) rf + 1); }
           else if ((bom & 0xFFFF) == 0xFEFF)               // utf16le bom
             CopyWChar((PWCHAR) rf + 1);  
           else if ((bom & 0xFFFFFF) == 0xBFBBEF)           // utf8 bom
             CopyMByte(rf + 3, br - 3);
           else                                             // no known bom, probe the file
             { if (! memchr(rf, 0x00, br))                  // 8 bit text has no nulls
                 CopyMByte(rf,br);                          // ansi / utf8 no bom
               else 
                { PBYTE lf = memchr(rf,0x0A,br);            // lf is always present as 1 byte.
                  if (!lf) 
                    Exception(0xE0640003);
                  if ((!(*(DWORD*)(lf - 3) & 0x00FFFFFF)) ||    //utf32be no bom
                       (!(*(DWORD*)lf & 0xFFFFFF00)))           //utf32le no bom
                     Exception(0xE0640002);    
                  if ((lf - rf) & 1)                        // big endian? (lf at odd offset)
                    FlipEndian(rf,br);                      // utf16be no bom  
                  CopyWChar((PWCHAR) rf);  } } }            // utf16le no bom
         finally  
          { free(rf); }
        return 1; }

  5. #20
    C <3er
    Join Date
    Jul 2011
    Posts
    46
    Yeah shouldn't say impossible for sure, but I was just referring to my particular case where I think probabilities of this are veeery low, anyways nice to see some more complicated example code like this

  6. #21
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    @CommonTater
    [rant]
    Your indentation and brace placement practices drive me up the wall, man! >_<
    [/rant]
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by msh View Post
    @CommonTater
    [rant]
    Your indentation and brace placement practices drive me up the wall, man! >_<
    [/rant]
    Yes, and?

    FWIW... the common standards for indentation etc. drive me right up the wall, but you don't hear me crying about it.
    AndrewHunter likes this.

  8. #23
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by msh View Post
    @CommonTater
    [rant]
    Your indentation and brace placement practices drive me up the wall, man! >_<
    [/rant]
    I wouldn't worry about it too much; he is Canadian after all.......
    CommonTater likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #24
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by CommonTater View Post
    Yes, and?

    FWIW... the common standards for indentation etc. drive me right up the wall, but you don't hear me crying about it.
    Oh, I'm just ranting! Hence the tags. I'm well aware of your opinions on indentation standards. ::beer::
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    I wouldn't worry about it too much; he is Canadian after all.......
    HEY! I resemble that remark!
    AndrewHunter likes this.

  11. #26
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    point was you always have to leave a certain number of bits for the variable if it is encrypted or not. the code does not do that. if you are going to have a spot for a status tag it always has to be there. with the same number of bits. it is the format that tells where the "blob" data starts. then if it is the same as the "blob" it does not matter because you now are looking in two different areas.

    bbbbbbb[tag][blob]etc.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kryptkat View Post
    point was you always have to leave a certain number of bits for the variable if it is encrypted or not. the code does not do that. if you are going to have a spot for a status tag it always has to be there. with the same number of bits. it is the format that tells where the "blob" data starts. then if it is the same as the "blob" it does not matter because you now are looking in two different areas.

    bbbbbbb[tag][blob]etc.
    Take a look at the unicode sample I posted... No the tag does not always have to be there. Good software will know what to do if it isn't.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encrypted XML
    By darren78 in forum C# Programming
    Replies: 9
    Last Post: 09-03-2010, 12:44 PM
  2. an encrypted program
    By makonikor in forum C Programming
    Replies: 13
    Last Post: 07-14-2010, 02:35 PM
  3. Writing encrypted value to file.
    By mkthnx001 in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 01:42 PM
  4. Reading encrypted data from file
    By cctoombs in forum C Programming
    Replies: 2
    Last Post: 02-13-2005, 02:59 AM
  5. help with encrypting plz
    By wimpman in forum C Programming
    Replies: 9
    Last Post: 03-31-2004, 03:29 PM

Tags for this Thread


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