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

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: encryption
by Adam (Vicar) on Nov 07, 2000 at 03:58 UTC
    Why? What have you got so far? We generally don't like to do all the work for you, but rather help you with a specific problems in your code or with Perl. Also, this sounds suspiciously like homework, which we don't like to do either.

    On the other hand, your question may be real. In that case please give us more to work with.

    Disclaimer: While I may appear to be speaking for the Monastery as a whole, there may be Monks that do not agree with me. They are exempted from my statement. In truth I speak only for my self and for them that agree with me.

Re: encryption
by blogan (Monk) on Nov 07, 2000 at 04:44 UTC
    Yes, this code is extremely inefficient. If you hand it in for homework, your teach will laugh at you. However, if you're having trouble with the Caesar Cipher, it might help you.
    #!/usr/bin/perl $line = "I'z abg fnlvat lbh nfxrq n ubzrjbex dhrfgvba..."; for ($i = 0; $i < length $line ; $i++) { $char = substr $line, $i, 1; if ($char eq "a") { $char = "n"; } elsif ($char eq "b") { $char = "o"; } elsif ($char eq "c") { $char = "p"; } elsif ($char eq "d") { $char = "q"; } elsif ($char eq "e") { $char = "r"; } elsif ($char eq "f") { $char = "s"; } elsif ($char eq "g") { $char = "t"; } elsif ($char eq "h") { $char = "u"; } elsif ($char eq "i") { $char = "v"; } elsif ($char eq "j") { $char = "w"; } elsif ($char eq "k") { $char = "x"; } elsif ($char eq "l") { $char = "y"; } elsif ($char eq "m") { $char = "z"; } elsif ($char eq "n") { $char = "a"; } elsif ($char eq "o") { $char = "b"; } elsif ($char eq "p") { $char = "c"; } elsif ($char eq "q") { $char = "d"; } elsif ($char eq "r") { $char = "e"; } elsif ($char eq "s") { $char = "f"; } elsif ($char eq "t") { $char = "g"; } elsif ($char eq "u") { $char = "h"; } elsif ($char eq "v") { $char = "i"; } elsif ($char eq "w") { $char = "j"; } elsif ($char eq "x") { $char = "k"; } elsif ($char eq "y") { $char = "l"; } elsif ($char eq "z") { $char = "m"; } $decoded .= $char; } print $decoded, "\n";
      #!/usr/bin/perl -w use strict; # $_ = "V'z abg fnlvat lbh nfxrq n ubzrjbex dhrfgvba..."; $_ = "Ohg vg'f ernyyl n irel fvzcyr gnfx..."; my %ceaser; @ceaser{'A'..'Z','a'..'z',' ','.',"'"} = ('N'..'Z','A'..'M', 'n'..'z', 'a'..'m', ' ','.',"'"); my $decoded; $decoded .= $ceaser{$_} for split //; print $decoded, "\n";
        Slightly more compact:
        #!/usr/bin/perl -w use strict; $_ = "Ohg vg'f ernyyl n irel fvzcyr gnfx..."; tr/A-Za-z/N-ZA-Mn-za-m/; print;
        Minor change, but now you don't have to define what happens for characters that shouldn't be "decrypted".
        #!/usr/bin/perl -w use strict; $_ = "Ohg vg'f ernyyl n irel fvzcyr gnfx..."; my %caesar; @caesar{'A'..'Z','a'..'z'} = ('N'..'Z','A'..'M', 'n'..'z', 'a'..'m'); my $decoded; $decoded .= $caesar{$_} ? $caesar{$_} : $_ for split //; print $decoded, "\n";
Re: encryption (chi-squared test)
by gumby (Scribe) on Jun 20, 2002 at 12:40 UTC
    An alternative, of course, is to use a statistical method: use the chi-squared test (see Mathworld).

    Let F_a(x) be the frequency of some letter, x, in you're alphabet, and let F_m(x) be the frequency of some letter, x, in the message. Now, for all 0 <= s <= 25 calculate for each letter, x, in your alphabet the sum of the squares of F_m((x + s)(mod 26)) / F_a(x). The value of s for which this obtains a minimum is the shift used to encipher the message.

RE: encryption
by AgentM (Curate) on Nov 07, 2000 at 04:18 UTC
      But then I'm not implementing the algorithm. I realy want to know how it owrks.