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

I want to read a field of char(32) type, from an informix database table.And contents of this field, is in encrypted form.So it contains, new line characters also. I want to dump contents from this field(as it is) into a single variable and later use the same, for decrypting it.

Part of what i have done is , as follows , But it is'nt working as intended because of new line characters.

open (ki_file, ">ki_file.txt") or die ("\n\nCould not open file. $!\n\n"); # Open file to dump ki_token from table print "\n ki_file.txt opened for output\n"; undef $/; # Undefining end of record character $stmt = "SELECT ki from imsi_period where imsi_id=346 "; # ki is field, containing encrypted data $sth = $dbh->prepare($stmt); $sth->{ChopBlanks} = 1; $sth->execute(); $sth->bind_col(1,\$ki_token,{TYPE => SQL_CHAR}); while ($sth->fetch) { # print $ki_token; print ki_file ($ki_token); print "Ki_Token is written to ki_file.txt." } $/ = "\n"; # Restore end of record character for normal beha +viour later close (ki_file); # Close file ......
  • Comment on How to read encrypted data from informix database into a single variable
  • Download Code

Replies are listed 'Best First'.
Re: How to read encrypted data from informix database into a single variable
by jettero (Monsignor) on May 15, 2007 at 12:27 UTC
    I could be going it alone (in which case: sorry); but I don't seem to understand what doesn't work or what it is that you intend undef $/ to do. I don't think $/ applies in any way when using DBI, but I could be mistaken about that too.

    Do you want to somehow escape the newlines before writing them to ki_file.txt?

    You'd have to pick some standard way to do so if that's what you're intending. Personally, I'd probably choose to dump a hex or base64 representation to file:

    # note that part of your trouble could be your antiquated file-handle +representation (doubtful). open my $output_file, ">", "ki_file.txt" or die $!; # unpack can be a little arcane... print $output_file unpack("H*", $ki_token), "\n";

    The best document on unpack is actually pack. Please correct me if I've completely missed the point of the question.

    -Paul

      @jettero : I am working with archaic system and new to Perl :) Yes, i thing i will try to dump a hex and work on it. It sounds better. I will give it a shot.

      I have taken, hex unload to a file. It contains around 25,000 records. It is not possible to extract a single record (unique keys are such, that i cannot use them at runtime). So now i want to read , 32 bytes of this file ( the hex dump) at a time (in an scalar variable) and skip 2 bytes after - each 32 byte read. can you suggest me a way to do this!

        You generate the output file yourself, so I'd make sure to only write 32bytes per line — while simultaneously avoiding writing the two mystery bytes — and just read them back in that way. no?
        while(<$input>) { chomp; my $var = pack("H*", $_); die "holy smokes, my hex-dump file is bad" unless length $var == 3 +2; }

        -Paul

Re: How to read encrypted data from informix database into a single variable
by shmem (Chancellor) on May 15, 2007 at 12:39 UTC
    If you are on Windows, any "\n" in the token might be converted to "\r\n" on output; undefining $/ doesn't fix that. See binmode.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      No, i am on HP-UX. i think working with hex dump, would be an better option for my problem

      I have taken, hex unload to a file. It contains around 25,000 records. It is not possible to extract a single record (unique keys are such, that i cannot use them at runtime). So now i want to read , 32 bytes of this file ( the hex dump) at a time (in an scalar variable) and skip 2 bytes after - each 32 byte read. can you suggest me a way to do this!

        Well, I'd do something along these lines..
        open my $fh, '<', $file or die "Can't read '$file': $!\n"; while((my $len = read $fh, my $record, 32) == 32) { # do something with $record ... (read $fh, my $junk, 2) == 2 or last; }

        Oh wait... yes, use strict, use warnings :-)

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}