tags:

views:

1383

answers:

1

I have a table with a RAW column for holding an encrypted string.

I have the PL/SQL code for encrypting from plain text into this field.

I wish to create a trigger containg the encryption code.

I wish to 'misuse' the RAW field to pass the plain text into the trigger. (I can't modify the schema, for example to add another column for the plain text field)

The client inserting the data is Python (cx_Oracle).

My question is how to best convert from a python string into HEX, then back to VARCHAR2 in the trigger so that the encryption code can be used without modification (encryption code expects VARCHAR2).

Here's an example:

create table BOB (name_enc raw(16));

In python. This is my initial attempt at encoding, I suspect I'll need something more portable for international character sets.

name_enc = 'some text'.encode('hex')

The trigger

create or replace trigger enc_bob before insert on BOB
for each row
DECLARE
    v_name varchar2(50);

BEGIN

    v_name := :new.name_enc;   <----  WHAT DO I NEED HERE TO CONVERT FROM HEX to VARCHAR?

    --
    -- encryption code that expects v_name to contain string 

END;

UPDATE

The suggestion for using Base64 worked for me

Python:

name_enc = base64.b64encode('some text')

PL/SQL:

v_name := utl_raw.cast_to_varchar2(UTL_ENCODE.BASE64_DECODE(:new.name_enc));
+2  A: 

Do you have to encode to hex?

I think there is a package (utl_encode) available for PL/SQL to decode Base64 for instance, you could use that?

Epcylon
thanks, this worked for me. see code above.
GHZ