views:

1751

answers:

1

Hello,

I have problem with encoding data in Oracle database. I want to xor string with another string (mask) and then encode it with base64.

In Python this looks like:

def encode_str(s, mask):
    xor_mask = mask
    while len(xor_mask) < len(s):
     xor_mask += mask
    chrs2 = []
    for i in range(len(s)):
     chrs2.append(chr(ord(s[i]) ^ ord(xor_mask[i])))
    s2 = ''.join(chrs2)
    return base64.b64encode(s2)
    #return binascii.hexlify(s2).lower()

In PL/SQL I got:

create or replace function ht_encode(str in varchar2, mask in varchar2) return varchar2 as
  xor_mask varchar2(2000);
  result_s varchar2(2000);
  i integer;
  xx integer;
  x char(10);
  ch1 char(10);
  ch2 char(10);
  chrx varchar2(10);
begin
  result_s := '';
  xor_mask := mask;
  while length(xor_mask) < length(str) loop
    xor_mask := xor_mask || mask;
  end loop;
  for i in 1..length(str) loop
    ch1 := substr(str, i, 1);
    ch2 := substr(xor_mask, i, 1);
    xx := BITXOR(ascii(ch1), ascii(ch2));
    x := xx;
    chrx := rawtohex(x);
    --result_s := result_s || ':' || chrx;
    --result_s := result_s || chrx;
    -- HELP ME HERE!
  end loop;
  --return lower(utl_encode.base64_encode(result_s));
  --return result_s || ' | ' || rawtohex(result_s);
  -- HELP ME HERE!
  return result_s;
end;

(bitxor comes from http://forums.oracle.com/forums/thread.jspa?threadID=496773 )

I don't know how to create "binary" string and then encode it to hex or preferable to base64.

+2  A: 
SELECT  UTL_RAW.cast_to_varchar2(UTL_ENCODE.base64_encode(UTL_RAW.bit_xor(UTL_RAW.cast_to_raw('text'), UTL_RAW.cast_to_raw('mask'))))
FROM    dual
Quassnoi
It works, thanks!
Michał Niklas