oracle encryption with java

1

It is possible to obtain the same result of the encryption with oracle 10g (DBMS_OBFUSCATION_TOOLKIT.des3encrypt) and of a java version jdk1.6.

JAVA CLASS

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class TripleDes3 {
    private Cipher cipher = null;
    private SecretKey key = null;
    private byte[] bytes = null;
    private IvParameterSpec iv = null;

    public static void main(String[] args) throws Exception {
        try {
            String hexKey = "ASDFGHASDFGHASDFGHASDFGH";
            //TripleDes3 encryptor = new TripleDes3(new String(Hex.decodeHex(hexKey.toCharArray())));
            TripleDes3 encryptor = new TripleDes3(hexKey);
            String original = "EFD3OWNER";
            System.out.println("Oringal: \"" + original + "\"");

            String enc = encryptor.encrypt(original);
            System.out.println("Encrypted: \"" + enc.toUpperCase() + "\"");

            String dec = encryptor.decrypt(enc);
            System.out.println("Decrypted: \"" + dec.toUpperCase() + "\"");

            if (dec.equals(original)) {
                System.out.println("Encryption ==> Decryption Successful");
            }
        } catch (Exception e) {
            System.out.println("Error: " + e.toString());
        }
    }

    public TripleDes3(String encryptionKey) throws GeneralSecurityException, DecoderException {
        cipher = Cipher.getInstance("DESede/CBC/NoPadding");
        try {
            key = new SecretKeySpec(encryptionKey.getBytes("iso-8859-1"), "DESede");
            iv = new IvParameterSpec(Hex.decodeHex("0123456789abcdef".toCharArray()));

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String encrypt(String input) throws GeneralSecurityException, UnsupportedEncodingException {
        bytes = input.getBytes("iso-8859-1");
        bytes = Arrays.copyOf(bytes, ((bytes.length+7)/8)*8);
        return new String(Hex.encodeHex(encryptB(bytes)));
    }

    public String decrypt(String input) throws GeneralSecurityException, DecoderException, UnsupportedEncodingException {
        bytes = Hex.decodeHex(input.toCharArray());
        String decrypted = new String(decryptB(bytes), "iso-8859-1");
        if (decrypted.indexOf((char) 0) > 0) {
            decrypted = decrypted.substring(0, decrypted.indexOf((char) 0));
        }
        return decrypted;
    }

    public byte[] encryptB(byte[] bytes) throws GeneralSecurityException {
        cipher.init(Cipher.ENCRYPT_MODE, (Key) key, iv);
        return cipher.doFinal(bytes);
    }

    public byte[] decryptB(byte[] bytes) throws GeneralSecurityException {
        cipher.init(Cipher.DECRYPT_MODE, (Key) key, iv);
        return cipher.doFinal(bytes);
    }
}

ORACLE

CREATE OR REPLACE FUNCTION FN_DES3ENCRYPT(
  p_in_val   IN   VARCHAR2,
  p_key      IN   VARCHAR2,
  p_iv       IN   VARCHAR2:= NULL,
  p_which    IN   NUMBER := 0,
  l_enc_val  OUT RAW

 )
  RETURN VARCHAR2
  IS
     l_in_val    RAW (200);
     l_key       RAW(200);
     wv_crypt_raw RAW(2000);
   BEGIN
      IF p_which = 0

      THEN
         IF LENGTH (p_key) < 16
         THEN
            raise_application_error
                                  (-20001,
                                  'Key length less than 16 for two-pass scheme'
                                  );
        END IF;
      ELSIF p_which = 1
      THEN
         IF LENGTH (p_key) < 24
         THEN
            raise_application_error
                                (-20002,
                                 'Key length less than 24 for three-pass scheme'
                                );
         END IF;
      ELSE
         raise_application_error (-20003,
                                     'Incorrect value of which '
                                  || p_which
                                  || '; must be 0 or 1');
      END IF;

  l_in_val := UTL_RAW.CAST_TO_RAW(RPAD (p_in_val, (8 * ROUND (LENGTH (p_in_val) / 8, 0) + 8)));

  l_key:=UTL_RAW.CAST_TO_RAW(p_key);
  l_enc_val:=DBMS_OBFUSCATION_TOOLKIT.des3encrypt (input      => l_in_val,
                                               key        => l_key,
                                               which =>  p_which,
                                               iv            => p_iv
                                              );

   RETURN wv_crypt_raw;
END;
    
asked by Leonardo David Alvarado Farro 09.01.2018 в 00:14
source

0 answers