http://www.perlmonks.org?node_id=674525

undef$/;print<>^<>

In the spirit of the RSA algorithm in the shape of a dolphin, RC5 in six lines of Perl, and similar feats of encryption methods shrunk down to tattoo-worthy sizes, I present a one-time pad cipher golfed to 18 characters.

The arguments to this should be the pad file (acting as the key) and the message file. You should use a pad file the same length as the message file, or possibly longer. If you use one that's longer, you'll end up with unused pad data after your message.

Obviously don't use a pad that's shorter than the message. Also, it's called a one-time pad for a reason -- don't reuse the pad file for another message if you want the best security.

While public-key crypto has advantages there's also a place for shared-key ciphers, and this one should cost a lot less to put on a shirt or into the skin than some others. ;-)

Replies are listed 'Best First'.
Re: encryption golf (one time pad)
by ikegami (Patriarch) on Mar 17, 2008 at 09:10 UTC
    perl -0777pe"$_^=<>"

    Depending on how you count, it may not be shorter, but it certainly wins points for craziness. I wish I had a "v" there, then I'd be pointing in every direction (< ^ v >).

    Update: s/377/777/. Thanks Corion.

      Very nice! Using the command-line parameters was a big win.

      I was going for the shortest solution in a source file without counting the shebang line, but I certainly didn't specify that as a rule. I think it's good I didn't, because this is a nice solution.

      Mine's more aesthetically pleasing to my eyes, but that's entirely subjective and I'm probably biased anyway. I also think mine's easier to commit to memory, but that's subjective too. You might find completely different things easier to memorize. Neither has anything to do with golf, but I thought they were nice touches considering the tight controls on free speech and on encryption software in some places. I know if I'm putting it on a shirt or into my skin, nice looking solutions get at least part of the consideration.

        I was going for the shortest solution in a source file without counting the shebang line

        If so, then mine is 6 to your 18.

        #!/usr/bin/perl -0777p $_^=<>

        I think your method of counting isn't appropriate, though.

        Mine's more aesthetically pleasing to my eyes, but that's entirely subjective and I'm probably biased anyway

        No, I agree. I did say mine was crazy.

Re: encryption golf (one time pad)
by jwkrahn (Abbot) on Mar 17, 2008 at 09:21 UTC
    undef$/;print<>^<>

    One the the  <> readlines will read all of the data from all of the files on the command line and the other will return undef so you are xoring everything with nothing leaving the text unchanged.

    Update: OK. I missed the scalar context. I was wrong.

      You should test your claims before contradicting someone.
      >echo abc>corpus >echo def>key >perl -e"undef$/;print<>^<>" corpus key >enc >debug enc -rcx CX 0004 : -d100 l4 0AFB:0100 05 07 05 00 .... -q >perl -e"undef$/;print<>^<>" enc key >dec >type dec abc

      Updated test to include decrpytion.

        I did test and I don't get anything resembling crypto.

      Thanks for testing it. On which version of perl on which platform did you find this behavior? It's not consistent with my results on Mandriva Linux with the distro-supplied 5.8.8 nor with 5.10.0 from source nor with Strawberry 5.10.0 on XP.