Link to home
Start Free TrialLog in
Avatar of priyaaaank
priyaaaank

asked on

Decrypt PL/SQL DESEncrypted String in Java

Hi all,

I am  trying to decrypt a string in Java Code which was encrypted using DBMS_OBFUSCATION_TOOLKIT in Oracle PL/SQL procedure.

Byte Key used in Java code is of format:
SecretKeySpec key = new SecretKeySpec(new byte[] { 1, 2, 3, 4, 5, 6, 7,
                        8, 9, 10 }, "DESede")
IV is as follows in Java;
byte[] initVector = { 8, 7, 6, 5, 4, 3, 2, 1 };

The values decrypted/encrypted are not same. I know I am making some goof up while using byte arrays in Oracle. Could you please either point me to an explanation as to how byte array keys should be used in PL/SQL or what is the best way to go about the encryption/decryption.

Please do let me know if additional info is requiered.

Thanks
CREATE OR REPLACE PROCEDURE testharidecrypt AS
 
BEGIN
 
   DECLARE
 
      l_key_vc    VARCHAR2 (40)  := '123456789a'; //hexadecimal values Key
 
      l_iv_vc     VARCHAR2 (8)   := '87654321'; 
 
      str         VARCHAR (2000) := 'somelogin';//string to be decrypted
 
      crypt_raw   RAW (2000);
 
      crypt_str   VARCHAR (2000);
 
      l           INTEGER        := LENGTH (str);
 
      i           INTEGER;
 
      padblock    RAW (2000);
 
   BEGIN
 
      l_key_vc := HEXTORAW (l_key_vc);
 
      l_iv_vc := HEXTORAW (l_iv_vc);
 
      i := 8 - MOD (l, 8);
 
      DBMS_OUTPUT.put_line ('i: ' || i);
 
      padblock := UTL_RAW.cast_to_raw (str || RPAD (CHR (i), i, CHR (i)));
 
      DBMS_OUTPUT.put_line ('str: ' || str);
 
      DBMS_OUTPUT.put_line ('padblock: ' || padblock);
 
      DBMS_OBFUSCATION_TOOLKIT.des3encrypt
 
                               (input_string          => padblock,
 
                                key_string            => l_key_vc,
 
                                which                 => DBMS_OBFUSCATION_TOOLKIT.twokeymode,
 
                                iv_string             => l_iv_vc,
 
                                encrypted_string      => crypt_raw
 
                               );
 
--     crypt_str := UTL_RAW.cast_to_varchar2 (crypt_str);
 
      DBMS_OUTPUT.put_line ('Encrypted String:' || crypt_raw);
 
--      l := LENGTH (crypt_str);
 
  --    crypt_str := RPAD (crypt_str, l - ASCII (SUBSTR (crypt_str, l)));
 
      DBMS_OUTPUT.put_line ('Encrypted String:' || crypt_raw);
 
   END;
 
END;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of schwertner
schwertner
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial