Monks, I've an Oracle database containing columns that were encrypted using DBMS_CRYPTO. Specifically, they were encrypted using:
SYS.DBMS_CRYPTO.ENCRYPT(request_des,request_src, SYS.DBMS_CRYPTO.DES_C +BC_PKCS5, raw_key);
I need to be able to decrypt these columns using Crypt::DES and Crypt::CBC from Perl. I am able to decrypt the data inside Oracle, like:
ecryption_type PLS_INTEGER := SYS.DBMS_CRYPTO.DES_CBC_PKCS5; v_key RAW(16) := null; PROCEDURE setkey (p_key IN VARCHAR2) IS BEGIN IF p_key IS not null THEN v_key := UTL_RAW.cast_to_raw(p_key); dbms_output.put_line(v_key); END IF; END setkey; FUNCTION decrypt( p_data IN RAW ) RETURN VARCHAR2 DETERMINISTIC IS l_decrypted RAW(32767); BEGIN l_decrypted := sys.DBMS_CRYPTO.decrypt + ( src => p_data, typ => ecryption_type, KEY => v_key ); RETURN UTL_I18N.RAW_TO_CHAR(l_decrypted,'AL32UTF8'); END decrypt; execute dbcrypt.setkey('mysecret'); select dbcrypt.decrypt(encrypted_column) from my_table where rownum < +10;

When I attempt to decrypt using seemingly comparable code in Perl, like:

#!/opt/local/perl/bin/perl -w use strict; use DBI; use Crypt::DES; use Crypt::CBC; # establish DB connection my $dbname = 'dbi:Oracle:my_db.world'; my $user = 'myuser'; my $password = 'mypass'; my $dbh = DBI->connect($dbname, $user, $password); if (!$dbh) { die "Error connecting to database; $DBI::errstr"; } $dbh->{LongReadLen} = 8192; my $sql = "select encrypted_column from my_table where rownum < 10"; my @responseList = map {$_->[0]} @{$dbh->selectall_arrayref($sql)}; my $cipher = Crypt::CBC->new(-key => 'mysecret', -cipher => 'Crypt::DES'); foreach my $cipherText (@responseList) { my $plaintext = $cipher->decrypt($cipherText); print "$plaintext\n"; }

Unfortunately, this Perl code complains:

Ciphertext does not begin with a valid header for 'salt' header mode
Any guesses as to why this error message occurs? Since we're reading an ecrypted BLOB, could there be a characterset incompatability that's messing up the header? Or is there some other really obvious thing I should be doing?

In reply to CBC:Crypt and Oracle by gregor-e

Title:
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, details, 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, summary, 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.