Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES Encryption Java/plsql

I need to implement the same encryption/decryption application on Java and plsql(DBMS_CRYPTO for Oracle 10g).

The both implementation are working fine but the pb here is that I am getting different output for the encryption of the same plain text. Below the both code used for the encryption/decryption process (Java and PLSQL).

I used the same encryption algorithm "AES/CBC/PKCS5Padding" for DBMS_CRYPTO and Java.The problem here is that I am getting different output for the encryption of the same plain text.

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
import org.apache.commons.codec.binary.Hex;

public class AESencrp {

    private static final String ALGO = "AES/CBC/PKCS5Padding";


    private static final byte[] keyValue = "MyEncryptionKey1".getBytes();
    private static Key key;
    private static Cipher decryptor;
    private static Cipher encryptor;


    public static  void init() throws Exception 
    {

        key = generateKey();
        encryptor = Cipher.getInstance(ALGO);
        IvParameterSpec iv = new IvParameterSpec(Hex.decodeHex("12345678901234567890123456789012".toCharArray()));   
        decryptor=Cipher.getInstance(ALGO);
        encryptor.init(Cipher.ENCRYPT_MODE, key,iv);
        decryptor.init(Cipher.DECRYPT_MODE, key,iv);
    }




public static String encrypt(String Data) throws Exception {
        byte[] encVal = encryptor.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

 public static String decrypt(String encryptedData) throws Exception {

        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = decryptor.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

 private static Key generateKey() throws Exception {
        if (key==null)
        key = new SecretKeySpec(keyValue, "AES");
        return key;
}

  public static void main(String[] args) throws Exception {

        init();
        String password = "mypassword";
        String passwordEnc = AESencrp.encrypt(password);
        String passwordDec = AESencrp.decrypt(passwordEnc);

        System.out.println("Plain Text : " + password);
        System.out.println("Encrypted Text : " + passwordEnc);
        System.out.println("Decrypted Text : " + passwordDec);
    }
    }
  • Java execution :

Plain Text : mypassword

Encrypted Text : +pvG30k4/KFkeim47tslFQ==

Decrypted Text : mypassword

CREATE OR REPLACE PACKAGE BODY SYSADM.enc_dec
AS
     encryption_type    PLS_INTEGER :=DBMS_CRYPTO.AES_CBC_PKCS5; 
     encryption_key     RAW (32) := UTL_RAW.cast_to_raw('MyEncryptionKey1');


     FUNCTION encrypt (p_plainText VARCHAR2) RETURN RAW DETERMINISTIC
     IS
        encrypted_raw      RAW (2000);
     BEGIN
        encrypted_raw := DBMS_CRYPTO.ENCRYPT
        (
           src => UTL_RAW.CAST_TO_RAW (p_plainText),
           typ => encryption_type,
           key => encryption_key,
           iv => hextoraw('12345678901234567890123456789012') 
        );
       RETURN encrypted_raw;
     END encrypt;
     FUNCTION decrypt (p_encryptedText RAW) RETURN VARCHAR2 DETERMINISTIC
     IS
        decrypted_raw      RAW (2000);
     BEGIN
        decrypted_raw := DBMS_CRYPTO.DECRYPT
        (
            src => p_encryptedText,
            typ => encryption_type,
            key => encryption_key,
            iv => hextoraw('12345678901234567890123456789012') 
        );
        RETURN (UTL_RAW.CAST_TO_VARCHAR2 (decrypted_raw));
     END decrypt;
END;
/
  • PlSQL Execution:

select enc_dec.encrypt('mypassword') encrypted from dual;

FA9BC6DF4938FCA1647A29B8EEDB2515

select enc_dec.decrypt(enc_dec.encrypt('mypassword')) decrypted from dual;

mypassword

  • Java -->+pvG30k4/KFkeim47tslFQ==

  • PLSQL -->FA9BC6DF4938FCA1647A29B8EEDB2515

Can you please advise why we have this difference between the two encrypted output? there is a programming language dependency for the AES encrytion??

like image 440
jamel Avatar asked Sep 12 '26 21:09

jamel


2 Answers

The +pvG30k4/KFkeim47tslFQ== is in Base64 representation, while the
FA9BC6DF4938FCA1647A29B8EEDB2515 is in HEX representation, but both strings represent the same value.

Decide on the single representation you will use and just convert one of the outputs to it.

like image 176
Oleg Estekhin Avatar answered Sep 14 '26 09:09

Oleg Estekhin


In case someone is interested about the completed code for the PL/SQL based on Java code of first post.

Encrpytion function:

CREATE OR REPLACE FUNCTION F_ENCRYPT (p_plainText VARCHAR2)
   RETURN VARCHAR2
AS
   encryption_type    PLS_INTEGER :=DBMS_CRYPTO.AES_CBC_PKCS5; 
   encryption_key     RAW (32) := UTL_RAW.cast_to_raw('MyEncryptionKey1');
   encrypted_raw      RAW (2000);
BEGIN
   encrypted_raw := DBMS_CRYPTO.ENCRYPT
       (
           src => UTL_RAW.CAST_TO_RAW (p_plainText),
           typ => encryption_type,
           key => encryption_key,
           iv => hextoraw('12345678901234567890123456789012') 
       ); 
       RETURN UTL_RAW.CAST_TO_VARCHAR2 (UTL_ENCODE.base64_encode (encrypted_raw));
END;

Decryption function:

CREATE OR REPLACE FUNCTION F_DECRYPT (p_input VARCHAR2)
   RETURN VARCHAR2
AS
  encryption_type    PLS_INTEGER :=DBMS_CRYPTO.AES_CBC_PKCS5; 
  encryption_key     RAW (32) := UTL_RAW.cast_to_raw('MyEncryptionKey1');
  decrypted_raw      RAW (2000);
BEGIN
  decrypted_raw := DBMS_CRYPTO.DECRYPT
     (
         src => UTL_ENCODE.base64_decode (UTL_RAW.CAST_TO_RAW (p_input)),
         typ => encryption_type,
         key => encryption_key,
         iv => hextoraw('12345678901234567890123456789012') 
     );
     RETURN (UTL_RAW.CAST_TO_VARCHAR2 (decrypted_raw));
END;

Validation:

SELECT 'mypassword' INPUT, 
    F_ENCRYPT('mypassword') ENCRYPTED_RESULT,
    F_DECRYPT(F_ENCRYPT('mypassword')) DECRYPT_RESULT 
FROM DUAL;

Validation Output:

"INPUT"       "ENCRYPTED_RESULT"          "DECRYPT_RESULT"
"mypassword"  "+pvG30k4/KFkeim47tslFQ=="  "mypassword"
like image 22
nikos-bob Avatar answered Sep 14 '26 10:09

nikos-bob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!