A basic perl script that encrypts a message with a given key using the playfair cipher. Instead of having a 2 dimensional array I use a hash and have a 2 way mapping of coordinate to character "0,0"->"a" and character to coordinate "a"->"0,0". This allowed me to easily use the same datastructure to lookup a character's position and find the character at a given coordinate.

#!path-to-perl use warnings; use strict; die "usage: playfair <'key'> <'message'>" unless(@ARGV == 2); my $key = lc($ARGV[0]); my $message = lc($ARGV[1]); my @alphabet = qw(a b c d e f g h i k l m n o p q r s t u v w x y z); my %square_hash = (); my $x; my $y; #replace all 'j's with 'i's $key =~ s/j/i/g; #build a key with unique letters (no repeats) while($key =~ s/(.)(.*)\1/$1$2/) {} print "Reduced key: $key\n"; #form the mapping my @keys = split(//,$key); $x = 0; $y = 0; for my $char (@keys) { $square_hash{$x . "," . $y} = $char; $square_hash{$char} = $x . "," . $y; $x++; if($x==5) { $x=0;$y++;} } #put in the rest of the letters for my $char (@alphabet) { next if exists($square_hash{$char}); $square_hash{$x . "," . $y} = $char; $square_hash{$char} = $x . "," . $y; $x++; if($x==5) { $x=0; $y++;} } print "5x5 playfair square:\n +-----------+\n"; for $y (0..4) { print " | "; for $x (0..4) { print $square_hash{$x . "," . $y} . " "; } print "|\n"; } print " +-----------+\n"; #change all j's to i's $message =~ s/j/i/gi; #remove spaces from the message $message =~ s/\s+//g; #insert an X in between identical letters $message =~ s/(.)(\1)/$1x$2/g; $message =~ s/(.)(\1)/$1x$2/g; #make it even length $message .= "x" if length($message)%2==1; print "Formatted message: $message\n"; #now encrypt everything my $encrypted = ""; while($message =~ s/^(.)(.)//) { my @xy1 = split(/,/,$square_hash{$1}); my @xy2 = split(/,/,$square_hash{$2}); #3 rules. 1: same row. if($xy1[0] == $xy2[0]) { #same col. move down 1 $xy1[1] = ($xy1[1]+1)%5; $xy2[1] = ($xy2[1]+1)%5; } elsif($xy1[1] == $xy2[1]) { #same row move right 1 $xy1[0] = ($xy1[0]+1)%5; $xy2[0] = ($xy2[0]+1)%5; } else { #same corners. chose element on other column same row. my $tempcol = $xy2[0]; $xy2[0] = $xy1[0]; $xy1[0] = $tempcol; } $encrypted .= $square_hash{join(",",@xy1)}; $encrypted .= $square_hash{join(",",@xy2)}; } $encrypted = uc($encrypted); print "Encrypted message E:\n$encrypted\n"; #OUTPUT # .\playfair "playfairexample" "hide the gold in the tree stump" # Reduced key: playfirexm # 5x5 playfair square: # +-----------+ # | p l a y f | # | i r e x m | # | b c d g h | # | k n o q s | # | t u v w z | # +-----------+ # Formatted message: hidethegoldinthetrexestump # Encrypted message E: # BMODZBXDNABEKUDMUIXMMOUVIF

The playfair cipher should not be used in a modern encryption scheme. It is solely a classic cipher. It is vulnerable to frequency analysis (digraph rather than monograph) yet is still quite secure if short messages are encrypted.

I hope to post a better-than-bruteforce decryption algorithm shortly.

I welcome any and all comments regarding ways I could improve my Perl scripting.


In reply to Playfair cipher by zek152

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.