It was very difficult to pick a winner, as all entries were good.
And the winner is....
... red_Marvin with his entry in PERL.
Code:
#!/usr/bin/env perl # just tell bash that it is a perl program
use warnings; # yell if I do something bad
use strict; # be less permissive than default
print "Enter key please:\n"; # text output
my $line = <>; # define a scalar line, and read one line
# from stdin into it
my @k = split '', $line; # define an array and split $line into
# individual chars and store into it
print "Enter message please:\n";
my @m = split '', <>; # shorter version of the above
print "Result:\n";
# start a for loop, the loop variable is $_ by default, and it will iterate
# through the values 0 to the index of the last value in @m minus 1:
# if we have the array @a, then $#a will be the index of the last item in @a
# ( we included '\n' when we read from stdin before, but we don't want it in
# the cipher, so for a string 'FOO\n' => ('F', 'O', 'O', '\n') we want to
# get the chars at 0, 1, 2 == 0..$#m-1 !!
for (0..$#m-1) {
print chr(
(
(
ord( # ord get's the ascii value of a char
$m[$_]) # if we have the array @a $a[4] will get the item at position 4 in @a
+ord(
$k[$_%$#k]) # modulus with length of key, since keylength can be < messagelength
-130 # same as -65 -65 to make A<->0 B<->1 etc ('A' has 65 as ascii value)
) # now we have summed the numerical values of the message char and the right key char
% 26 # to keep the value from overflowing
) +65 # to convert back so that A<->65 B<->66 etc
);
}
print "\n";
Take care,
Rouslan
Bookmarks