Hello, here is the code.
#/usr/bin/perl -w # Demo Version - # AY2012/2013 ST2614 Assignment # PM to support Personal Password Manager package ST2614 ; our $VERSION = 1.0; sub getAscii { # This is a helper function to support encode() and decode() # #input a normal string # convert each char of the string to its corresponding ascii cod +e # return the list of the ascii codes # in one , delimited string my $plaintext= shift; my @asciilist = (); my @tmplist = split('', $plaintext); foreach $ch (@tmplist) { push (@asciilist, ord($ch)); # use ord to get ascii code } return join(",",@asciilist); } sub encode { # simple xor encoding # Argument 1 - A string in plain text - to be encoded # Argument 2 - A string in plain text, represent the key # return the xor result in ascii form my ($text , $key) = @_; $text = getAscii($text); $key = getAscii($key); my @textlist = split(',',$text); my @keyblk = split(',',$key); my $index = 0; my $max = @keyblk; my @encodedlist = (); foreach $chcode (@textlist) { # simple xor encoding # must use int() to force a numeric xor operation my $secret = int($chcode) ^ int($keyblk[$index]); push(@encodedlist,$secret); # store the result # recycle the key block $index = ($index+1) % $max; } return join(",",@encodedlist); } sub decode { # simple xor decoding # Argument 1 - encoded string in ascii list form # Argument 2 - A string in plain text, represent the key # return the xor result in plain text string form my ($secret , $key) = @_; $key = getAscii($key); # break the line into array and xor with the keyblock my @secretlist = split(',',$secret); my @keyblk = split(',',$key); my $index = 0; my $max = @keyblk; my $orgText = ""; foreach $chcode (@secretlist) { my $org = int($chcode) ^ int($keyblk[$index++]); # convert numeric ascii value back to character form # and append it to $orgText $orgText .= chr($org); $index = $index % $max; } return $orgText; } 1; # required to return 1 at the end of a pm file

In reply to Re^2: Optional Arguments..? by Watergun
in thread Optional Arguments..? by Watergun

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.