Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'm creating a password encryption program to store passwords. I store the values into a DB_File database which I then encrypt. The problem comes in then opening and decrypting that database. Normally to tie the database you use:
tie (%cryptohash, 'DB_File', undef, O_RDWR|O_CREAT, 0644, $DB_BTREE) or die "Database error: $!";
and then refer to %cryptohash like any normal hash and save it back to file when done. Problem is once the file is encrypted you can't do the tie UNTIL you've decrypted it first (because DB_File doesn't recognise the file format and the tie fails). So you have to decrypt first and then tie.

Now I could just decrypt the master file to a temporary file and then tie to that temporary file (in DB_FILE format) but if during execution the program dies you then have a decrypted copy just lying around. SO ... I'm trying to find a way to decrypt the file into memory and then tie to that piece of memory, instead of to a file. Of course the memory has to be a hash because that is what DB_File utilises and ties to.

But how do you read a file into a hash in memory and then tie to it? OR, if I read the file into an @array how do I then translate that into a valid DB_File format again which can be tied? Its this one little step that I'm stuck at.

Now in my testing I found out you can tie a %hash to a undef'ined file value, which is literally stored in memory which you can then save to file (see below code). This helps solve part of the puzzle(and is an interesting trick), but still not all of the puzzle. Keep in mind I DON'T want to be creating an decrypted verion of the data to file due to security issues.

Any ideas how to bridge this gap?
#!perl use DB_File; use Fcntl; # For the constants O_RDWR and O_CREAT use strict; use warnings; ## master file name my $cryptofile = "crypt.qap"; ## create Berkeley DB my %cryptohash; my %testhash; tie (%testhash, 'DB_File', undef, O_RDWR|O_CREAT, 0644, $DB_BTREE) or die "Database error: $!"; $testhash{'this'} = "hello"; print $testhash{'hello'}; tie (%cryptohash, 'DB_File', $cryptofile, O_RDWR|O_CREAT, 0644, $DB_BT +REE) or die "Database error: $!"; %cryptohash = %testhash; untie %cryptohash; untie %testhash;

The Funkster of Mirth
Programming these days takes more than a lone avenger with a compiler. - sam
RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

In reply to Opening an Encrypted DB_File database by crabbdean

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    marto sighs

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2018-03-22 09:37 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (273 votes). Check out past polls.