Re: Cross-platform encryption tool?

From: Dave Thompson (david.thompson1_at_worldnet.att.net)
Date: 08/22/05


Date: Mon, 22 Aug 2005 04:01:00 GMT

On 15 Aug 2005 18:39:38 -0700, tomstdenis@gmail.com wrote:
<snip>
> If you don't need security I highly recommend this multi-pass XOR
> algorithm
>
> void encrypt(char *data, int len)
> {
> int x;
> x = 0; start1: while ((data[x++] ^= 0x55) && (x < len)) goto start1;
> x ^= len;
>
> // engage second pass
> x = 0; start1: while ((data[x++] ^= 0x11) && (x < len)) goto start1;
> x ^= len;
>
> // finally smear the bits in a third pass
> x = 0; start1: while ((data[x++] ^= 0x44) && (x < len)) goto start1;
> x ^= len;
> }
>
Assuming C (and not some Tom-private language):

Doesn't compile because label is duplicated in function scope. If you
fix that in the apparently intended way, or just change to proper
while loops, doesn't work correctly (for extremely small values of
"work") for data containing byte values 55 or 44 (ASCII 'U' or 'D') or
0 (which can't happen in C strings, but OP didn't say C strings, and
this uses explicit length which usually is done for nonstrings).

Length and index of arbitrary objects/buffers are usually best done in
size_t; int isn't always large enough. And for typical modern
(all-bits) ciphers -- which this arguably isn't -- at least ciphertext
and often plaintext is more appropriately declared as _unsigned_ char
(pointer), although systems where this actually matters are rare.

And if len = 0 may clobber a byte (outside that specification)
although such a call is even less useful than len > 0.

Personally, I think I prefer:
static const int passes = 7; /* "security parameter" */
void encrypt /* or decrypt */ (unsigned char * buf, size_t len)
{
  int n = passes * 2; /* DOUBLE specified security */
  int seed = (int)time(NULL) ^ getpid();
  /* or some similar value for weird(er) systems */
  while( n-- ) {
    srand(seed);
    for( size_t i = 0; i < len; i++ ) buf[i] ^= rand();
  }
}
<VBG>

- David.Thompson1 at worldnet.att.net



Relevant Pages

  • Re: Boost process and C
    ... of int for sizes, rather than size_t. ... read-only and constant strings and detecting errors efficiently ... where Bstrlib fails to be able to deal with them due to its design ... If the DOS port hadn't been dropped then depending on the compiler we ...
    (comp.lang.c)
  • Re: an implementation of insertion sort, insert without move element
    ... where everything is type int, ... for operations between array elements, ... Original order of strings: ... int lencomp(const void *a, const void *b) ...
    (comp.lang.c)
  • Re: Boost process and C
    ... of int for sizes, rather than size_t. ... read-only and constant strings and detecting errors efficiently ... If the DOS port hadn't been dropped then depending on the compiler we ... And how do you deal with the fact that the language limits your sizes ...
    (comp.lang.c)
  • Re: Boost process and C
    ... of int for sizes, rather than size_t. ... read-only and constant strings and detecting errors efficiently ... Handling an RTF document that you will be writing to a variable length ... the scalar fields. ...
    (comp.lang.c)
  • Re: Boost process and C
    ... of int for sizes, rather than size_t. ... read-only and constant strings and detecting errors efficiently ... I do have good reason for doing this. ... If the DOS port hadn't been dropped then depending on the compiler we ...
    (comp.lang.c)