making files opposite from themselves (100% change)

Giorgos Keramidas keramida at ceid.upatras.gr
Mon Jul 5 14:31:53 PDT 2004


On 2004-07-05 13:55, Joe Schmoe <non_secure at yahoo.com> wrote:
> So the question is, how do I take a given file and make it 100%
> different from itself (but maintain its size and place on disk) ?
> I could just output /dev/zero to it, but that would leave unchanged
> all the bits that were aleady zero.

Use an algorithm similar to the one shown below as a Perl script, to
pick a certain percentage of the bytes within a file, and at those
offsets chosen by this algorithm, use XOR with 0xFF or a random value to
alter the value of only the given percentage of bytes.

  : #!/usr/bin/perl -w
  :
  : use strict;
  : my ($filesize, $percent, $k, $nparts, $partlen);
  :
  : die "usage: foo.pl FILESIZE PERCENT"
  :     unless ($#ARGV == 1);
  :
  : $filesize = $ARGV[0];
  : $percent = $ARGV[1] % 100;
  :
  : $nparts = int(($percent * $filesize) / 100);
  : $partlen = int($filesize / $nparts);
  :
  : srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
  :
  : print "offsets:";
  : for ($k = 0; $k < $nparts; $k++) {
  :     my $tmp = int(rand($partlen));
  :     my $nbyte = ($tmp + $k * $partlen);
  :     print " $nbyte"
  : }
  : print "\n";

> So how do I flip the bits of an entire file ?

That's even easier:

  : for each byte:
  :     xor(byte, 0xFF);

HTH,

Giorgos


More information about the freebsd-questions mailing list