Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Optional Arguments..?

by poj (Curate)
on Jun 03, 2012 at 12:45 UTC ( #974140=note: print w/ replies, xml ) Need Help??


in reply to Optional Arguments..?

Could you please show the code for the encode, decode routines. The arguments you have in these statements don't look right to me.

my $decode = ST2614::decode(FILEHANDLE, $ARGV[1]); my $encode = ST2614::encode(HoA, $ARGV[1]);
poj


Comment on Re: Optional Arguments..?
Download Code
Re^2: Optional Arguments..?
by Watergun (Novice) on Jun 03, 2012 at 12:53 UTC
    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
      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)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://974140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-07-13 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (252 votes), past polls