The following will encrypt strings of up to 15 bytes in length into strings of 22 URL-safe bytes.

There is no salting and no chaining, so a lot of security is missing. Do not use this to hide any critical information. Do no use the key used here for anything else.

It is, however, as strong as any other solution in this thread, results in two fewer bytes than the other equally strong contender, and can encode arbitrary data (including nuls and spaces).

# Encrypter use Crypt::Blowfish (); use MIME::Base64 qw( encode_base64 ); my $key = ...; my $plaintext = ...; die("Text to encrypt must be less than 16 bytes long\n") if length($plaintext) >= 16; my $cipher = Crypt::Blowfish->new($key); my $padded_plaintext = pack('Ca15', length($plaintext), $plaintext); my $ciphertext1 = $cipher->encrypt(substr($plaintext, 0 +, 8)); my $ciphertext2 = $ciphertext1 ^ $cipher->encrypt(substr($plaintext, 8 +, 8)); my $ciphertext = $ciphertext1 . $ciphertext2; my $websafe_ciphertext = substr(encode_base64($ciphertext, ''), 0, -2) +;
# Decrypter use Crypt::Blowfish (); use MIME::Base64 qw( decode_base64 ); my $key = ...; my $websafe_ciphertext = ...; die("Text to decrypt must be exactly 22 bytes long\n") if length($plaintext) != 22; my $cipher = Crypt::Blowfish->new($key); my $ciphertext = decode_base64($websafe_ciphertext.'=='); my $ciphertext1 = substr($plaintext, 0, 8); my $ciphertext2 = substr($plaintext, 8, 8); my $padded_plaintext = $cipher->decrypt( $cyphertext1) . $cipher->decrypt($ciphertext1 ^ $cyphertext2); my $plaintext = unpack('C/a*', $padded_plaintext);

Untested.

Update: Now uses chaining.


In reply to Re: Short & Sweet Encryption? by ikegami
in thread Short & Sweet Encryption? by inblosam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.