in reply to Playfair cipher

This response may be merely intuitive... AND wrong! (I am neither a cryptographer nor a particularly good mathematician.) Nonetheless, here goes...

It seems to me (YMMV . caveat above) that splitting duped chars by a variety of little used chars (or, maybe most frequently used chars) has certain merit... because it makes usage-frequency testing slightly more complex and may thus throw off any brute force using frequency testing.

#!/usr/bin/perl #!/usr/bin/perl use strict; use warnings; use 5.012; # See 910956.pl (re playfair cipher) for salt, key and encoding -- # as a follow on, modify encoding with possibility of multiple # (i,e. "varying" or "inconsistent") insertions between dup letters my @range = ("Q","V","X","Y","Z",); my $seen = ""; my $message = uc(<>); $message =~ s/[^A-Z0-9]//gi; # remove punct, etc $message =~ s/j/i/gi; $message =~ s/q/O/gi; # replace [Qq] with [Oo] $message =~ s/\s+//g; # remove spaces $message .= "X" if length($message)%2==1; # make length an even va +lue my @char = split('', $message); say "\n" . "-" x10 . "\n"; for my $char(@char) { if ($seen eq ($char)) { my $i = int(rand(4)); my $letter = $range[$i]; print $letter . $char; # recode as push to encoded arra +y for later printing } else { # as five char groups print ($char); } # recode: ditto $seen = $char; } say "\n";

The tested I/O includes (with apologies for the pre tags below)

Quoth the Raven, 'Nevermore!' ...or three (3) or four (4) words to that generally accepted effect.
----------
OUOTHTHERAVENXNEVERMOREORTHREXE3ORFOUR4WORDSTOTHATGENERALXLYACVCEPTEDEFYFECTX
^            ^         ^    ^^^^^    ^^^                ^^^^ ^^^     ^^^^   ^

# obviously, this has limitations:

The SECRET stash is located cinq feet below ground at 41.7N, 73.4W. Its key is X734ab#@17ffyj.
----------
THESECRETSTASHISLOCATEDCINOFEYETBELOWGROUNDAT417N734WITSKEYISX734AB17FVFYi
# meaning determinable from context          ^^^ ^^^
# but the new key is corrupted...                                ^^^ ^^^ ^^

Replies are listed 'Best First'.
Re^2: Playfair cipher
by jwkrahn (Abbot) on Jun 23, 2011 at 00:06 UTC
    my $message = uc(<>); $message =~ s/[^A-Z0-9]//gi; # remove punct, etc $message =~ s/j/i/gi; $message =~ s/q/O/gi; # replace [Qq] with [Oo] $message =~ s/\s+//g; # remove spaces
    • You UPPER case the contents of $message at the beginning so the use of the /i option seems strange.    Why not just use upper case letters in the pattern?
    • Your comment says "replace [Qq] with [Oo]" but that is not what your code is doing.
    • Your last substitution attempts to remove "spaces" which were already removed by your first substitution
    • And your code would be more efficient using transliteration instead of substitution:
    my $message = uc <>; $message =~ tr/A-Z0-9//cd; # remove punct, etc $message =~ tr/JQ/IO/;


    Update:

    my @range = ("Q","V","X","Y","Z",); ... my $i = int(rand(4)); my $letter = $range[$i];

    Your @range array contains five elements but you are only accessing the first four of them.

    That is usually written as:

    my @range = ("Q","V","X","Y","Z",); ... my $letter = $range[ rand @range ];

      Right on all counts!

      TY and ++

      Coder (yeah, /me) abandoned all good practice by writing a little code; getting another (allegedly) bright idea; cutting/tweaking/adding functions/routines/code without careful review of previous code (and then 'rinsing, repeating').

      The initial idea may or may not be worth anything; the procedures by which this code was written deserve to be castigated far more harshly than the gentle jwkrahn did. But, gentle reader, future development should attend carefully to those wise observations.

Re^2: Playfair cipher
by zek152 (Pilgrim) on Jun 23, 2011 at 13:03 UTC

    Thanks for your comment ww. I wanted to comment on the merit of using a group of "dummy" characters (as they are called) rather than a specific character. The reason for using a dummy character in the first place is two-fold. (1) to prevent the creation of a new rule for handling characters in the same square and (2) to prevent simpler cryptoanalysis (english is full of double letters "mm oo ee (ww? :) )" . The addition of a single dummy character satisfies both of these requirements.

    What your approach does is further confuse the relationship between duped chars and their encoding. This might make the statistical attack easier harder if the playfair cipher did not suffer from a much more significant weakness.

    This weakness is the fact that E(AB) = reverse(E(BA)) (where E = the encrypting function). This means that words like DEceivED and REceivER and DEpartED will maintain a AB(..)+BA pattern. This is how most statistical attacks of a playfair cipher begin.

    It is interesting to note that the fact that a double letter will never appear in the encrypted message actually weakens the cipher for the mere fact that a message with no double letters is extremely suggestive that the encryption is a playfair cipher.

    Thanks for your response. Your idea can be easily adapted to helping the weakness of playfair which is that anytime a AB(..)+BA pattern is found replace it with a AXB(..)+BA (or a AB(..)+BXA to make the statistical attack weaker.

    Update: Mistyped above. Text is struck out.