in reply to Perl Cipher & questions on semantics/layout optimisation.
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. |