Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an encrypted file containing password information (via Xor encryption... and yes I know perils of using this sort of encryption).
I would like to be able to unencrypt the contents into a memory structure to manipulate the contents, rather than writing to a file.
The input file has 3 fields and is tab delimited.
serv login pass serv login pass serv login pass
Here's a snippet of the code I currently use to put the unencrypted contents into an array.
open(IN, $ARGV[2]) or die "Can't read $ARGV[2]: $!\n"; my ($in,@workarray); while( sysread(IN,$in,length($enckey)) ) { push @workarray,$in^substr($enckey,0,length($in)); }
The problem is that each element of the array does not finish on a delimiter boundary.
e.g.
print "Value is : $workarray[0]"; print "Value is : $workarray[1]"; print "Value is : $workarray[2]"; print "Value is : $workarray[3]"; print "Value is : $workarray[4]"; print "Value is : $workarray[5]";
gives me
Value is : SYB_SERV_DEV Value is : joe_blogg Value is : s nopasswor Value is : d SYB_SERV Value is : C_OTC joe_ Value is : bloggs nopa
When I was expecting
Value is : SYB_SERV_DEV Value is : joe_bloggs Value is : nopassword Value is : SYB_SERVC_OTC Value is : joe_bloggs Value is : nopassword
hence searching elements of the array is proving fruitless.
I'm sure there are better ways of doing this (I'm relatively new to perl). Any comments on how to clean this up would be useful.

Replies are listed 'Best First'.
Re: Manipulating an encrypted file
by tachyon (Chancellor) on Oct 28, 2004 at 09:55 UTC

    I suspect that you want something like:

    my ($in,$data); while( sysread(IN,$in,length($enckey)) ) { $data .= $in^$enckey; } my @recs = map{ [ split /\t/ ] }split /\n/, $data; for my $rec_ref(@recs) { printf "serv %s\nuser %s\npass %s\n", $rec_ref->[0], $rec_ref->[1], $rec_ref->[2]; # NB @$rec_ref i +s equiv }

    cheers

    tachyon

      Thanks for the reply. I tried this out and it seems to do the trick.
Re: Manipulating an encrypted file
by si_lence (Deacon) on Oct 28, 2004 at 09:48 UTC
    Hi, If you want to have all the values in one array you
    can use something like this:
    use strict; use warnings; my $path_to_infile= "<set path here>"; my @workarray; open (IN, "< $path_to_infile") or die $!; while (<IN>) { chomp; push (@workarray, split("\t", $_)); } print "Value is : $workarray[0]\n"; print "Value is : $workarray[1]\n"; print "Value is : $workarray[2]\n"; print "Value is : $workarray[3]\n"; print "Value is : $workarray[4]\n"; print "Value is : $workarray[5]\n";
    This works only if you still have the tabs and newlines
    in your in-file
    si_lence
Re: Manipulating an encrypted file
by claree0 (Hermit) on Oct 28, 2004 at 09:39 UTC
    Quick and dirty answer: instead of pushing the unencrypted chunks onto an array, concatenate them onto a string. Assuming that your encrypt/decrypt preserved the non-printable chars (\n and tab) you should then be able to simply print out the final string.
Re: Manipulating an encrypted file
by Random_Walk (Prior) on Oct 28, 2004 at 10:32 UTC

    I guess your encryption is producing newline characters in the crypted data and your IFS ($/) is set to the default of newline. you will have to sysread a fixed length if your records are fixed length. If you can not make them fixed length you will have to use a new IFS which can never appear in your crypt text, or alter your crypt so it can not generate the IFS

    Cheers,
    R.