Skip to Main Content

Oracle Database Discussions

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

AES Algorithm error when trying to encrypt using stored Java class.

721762Sep 10 2009 — edited Sep 12 2009
Dear All,

We have a specific reuirement where in we cannot use DBMS_CRYPTO package to encrypt/decrypt data using AES Algorithm
So I am trying to use a stored Java class and I am getting "AES algorithm not available".
I am using Oracle 10gR2 standard edition.
Below is my code
1. Stored Java class
2. Stored function to access the above Java class.
3. Test anonymus PL/SQL to test above code.

Please help me finding the problem why I am getting "AES algorithm not available" error when I call stored Java class in Oracle.?

**** If I use "DES" algorithm, it works. Also the Java code works well if I execute it as normal Java class from Eclipse.
I verified the java.security file in jre/lib/security and I see that there is provider entry for SunJCE.
The jre version in Oracle is 1.4.2.

I appreciate your help.

Thanks,
Priyanka

----------------------------------------------------------------------------------
Step1: Stored java class to encrypt and decrypt data
-----------------------------------------------------------------------------------
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "EncryptUtil" AS
import java.security.Key;

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

public class EncryptUtil
{
public static String encrypt(String inStr)
{
String outStr = "Test data 123";
try
{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted =
cipher.doFinal(inStr.getBytes());
outStr =new String(encrypted);
}
catch (Exception e)
{
outStr = outStr + "exception thrown::" + e.getMessage();
e.printStackTrace();
}
return outStr;
}
}
----------------------------------------------------------------------------------------------------------------------
Step2: Stored function to access above stored java class.
----------------------------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION SF_ENCRYPTUTIL(
pKey1 VARCHAR2
) RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'EncryptUtil.encrypt(java.lang.String ) return java.lang.String';
/
----------------------------------------------------------------------------------------------------------------------
Step3: Test encryption and descryption
----------------------------------------------------------------------------------------------------------------------
DECLARE
outstr VARCHAR2(2000);
BEGIN
DBMS_OUTPUT.PUT_LINE('outstr-->' || SF_ENCRYPTUTIL('12345'));
END;
/
----------------------------------------------------------------------------------------------------------------------
Below code example using DBMS_CRYPTO. This works, but we do not want to use this.
----------------------------------------------------------------------------------------------------------------------
declare
l_in_val varchar2(2000) := 'Test data 123';
l_mod number := dbms_crypto.ENCRYPT_AES128
+ dbms_crypto.CHAIN_CBC
+ dbms_crypto.PAD_PKCS5;
l_enc raw (2000);
l_enc_key raw (2000);
l_dec raw (2000);

begin

l_enc := dbms_crypto.encrypt
(
UTL_I18N.STRING_TO_RAW (l_in_val, 'AL32UTF8'),
l_mod,
HEXTORAW('156ae12300ccfbeb48e43aa016febb36'),
HEXTORAW('001122230405060708090a0b0c0d0e0f')
);
dbms_output.put_line ('Encrypted='||l_enc);

end;
/
----------------------------------------------------------------------------------------------------------------------

Edited by: user5092433 on Sep 10, 2009 12:26 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2009
Added on Sep 10 2009
3 comments
1,458 views