in reply to Re: Optional Arguments..?
in thread Optional Arguments..?

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

Replies are listed 'Best First'.
Re^3: Optional Arguments..?
by poj (Abbot) on Jun 03, 2012 at 13:12 UTC
    OK, looks like arguments are text strings, not a filehandle or a hash ref. Can you show some lines of data from the passmgr.dat file.
    poj

      I don't really know how to copy and paste output from my terminal, but basically it goes like this (not real output)

      1,92,54,12,32,53,23,54,11,53,68,77

      However I have no error with encoding. I just can't.. figure out how to code it when it involved decoding data (an additional step from basic printing of my hash array)
        You can't store all the data you need like this ;
        # declare a hash of array to prepare storage of all # variables to be encoded together my $HoA{$sitename} = ["$loginid", "$password", "$url"]; open(FILEHANDLE, ">>passmgr.dat") or die("The file cannot be opened!"); my $encode = ST2614::encode($HoA, $ARGV[1]); print FILEHANDLE "$encode"; close FILEHANDLE;
        Consider using a tab delimiter record format like this ;
        sitename1 loginid1 encoded_password1 url1 sitename2 loginid2 encoded_password2 url2 sitename3 loginid3 encoded_password3 url3
        Then you can build a HoA from the file like this
        my $key = $ARGV[1] || 'secretkey'; my %HoA=(); open IN, '<','passmgr.dat' or die ("The file cannot be opened!"); while (<IN>){ chomp; my ($sitename,$id,$enc_password,$url) = split "\t",$_; my $password = ST2614::decode($enc_password, $key); $HoA{$sitename} = [$id,$password,$url]; # print "$sitename $id $password $url\n"; }
        and save it to the file like this
        open OUT, '>','passmgr.dat' or die ("The file cannot be opened!"); for my $sitename (sort keys %HoA){ my $id = $HoA{$sitename}[0]; my $enc_password = ST2614::encode($HoA{$sitename}[1], $key); my $url = $HoA{$sitename}[2]; print OUT join "\t",$sitename,$id,$enc_password,$url,"\n"; }
        poj