First off: you absolutely must use strictures (use strict; use warnings;). You have a large number of errors in your code that strictures would have picked up. Aside from that the code doesn't look very Perlish in places and looks fairly newbieish in other places. The following code may be of interest:

use strict; use warnings; my $key = ""; while (length ($key) < 4) { print "Please Enter Your Desired Key (5 or more characters) \n --> + : "; $key = <>; chomp $key; } my @karray = split (//, $key); my @offset1; my @offset2; for my $ordChar1 (map {ord} @karray) { my $sum = 0; for my $ordChar2 (map {ord} @karray) { $sum += $ordChar1 + ord $ordChar2; push @offset1, ($ordChar1 + $ordChar2) % 256; } push @offset2, int ($sum / length $key) % 256; } 1 while prompt (); sub prompt { my ($mode, $offset1, $offset2) = @_; print "Encrypt or Decrypt (E/D)\n --> :"; my $inp = <>; chomp $inp; return 1 if $inp !~ s/^(e|d)$/lc $1/ie; print "Enter ", ($inp eq 'e' ? 'plain text' : 'cypher text'), "\n +--> : "; chomp (my $text = <>); my @chars = split (//, $text); if ($inp eq 'e') { print encrypt(\@chars, \@offset1, \@offset2); } else { print decrypt(\@chars, \@offset1, \@offset2); } return; } sub encrypt { my ($chars, $offset1, $offset2) = @_; my $outText; my $len = @$chars; for my $index (0 .. $len - 1) { $outText .= chr (( ord ($chars->[$index]) - ord ($offset1->[$index % $len * $len]) + ord ($offset2->[$index % $len]) ) % 256 - $index % 6); } return $outText; } sub decrypt { my ($chars, $offset1, $offset2) = @_; my $outText; my $len = @$chars; for my $index (0 .. $len - 1) { $outText .= chr (( ord ($chars->[$index]) + ord ($offset1->[$index % $len * $len]) - ord ($offset2->[$index % $len]) ) % 256 + $index % 6); } return $outText; }
True laziness is hard work

In reply to Re: Perl Cipher & questions on semantics/layout optimisation. by GrandFather
in thread Perl Cipher & questions on semantics/layout optimisation. by Pseudomander

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.