Utilizing The Sun Java AES encryption from JDK 1.4.2 Sdk Within The Database (Doc ID 356123.1)

Last updated on JULY 17, 2017

Applies to:

Oracle Server - Enterprise Edition - Version 10.1.0.2 to 10.2.0.2 [Release 10.1 to 10.2]
Information in this document applies to any platform.

Symptoms

Java encryption of AES is not working with the 10.1.0.x or 10.2.0.x database releases.


CODE  (run in sql*plus)
-------------------
  set define ^;

  Create or replace and resolve java source named "AESTest" as
     import java.security.*;
       import javax.crypto.*;
       import javax.crypto.spec.*;
       import java.io.*;
   
       /**
       * This program generates a AES key, retrieves its raw bytes, and
       * then reinstantiates a AES key from the key bytes.
       * The reinstantiated key is used to initialize a AES cipher for
       * encryption and decryption.
       */
   
       public class AESTest {
   
         /**
         * Turns array of bytes into string
         *
         * @param buf  Array of bytes to convert to hex string
         * @return  Generated hex string
         */
         public static String asHex (byte buf[]) {
          StringBuffer strbuf = new StringBuffer(buf.length * 2);
          int i;
   
          for (i = 0; i < buf.length; i++) {
           if (((int) buf[i] & 0xff) < 0x10)
          strbuf.append("0");
   
           strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
          }
   
          return strbuf.toString();
         }
   
         public static void testAsHex() throws Exception {
   
           String message="This is just an example";
   
           // Get the KeyGenerator
   
           KeyGenerator kgen = KeyGenerator.getInstance("AES");
           kgen.init(128); // 192 and 256 bits may not be available
   
   
           // Generate the secret key specs.
           SecretKey skey = kgen.generateKey();
           byte[] raw = skey.getEncoded();
   
           SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
   
   
           // Instantiate the cipher
   
           Cipher cipher = Cipher.getInstance("AES");
   
           cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
   
           byte[] encrypted =
             cipher.doFinal("This is just an example".getBytes() );
           System.out.println("encrypted string: " + asHex(encrypted));
   
           cipher.init(Cipher.DECRYPT_MODE, skeySpec);
           byte[] original =
             cipher.doFinal(encrypted);
           String originalString = new String(original);
           System.out.println("Original string: " +
             originalString + " " + asHex(original));
         }
       }
    /
   
   
    WRAPPER  (run in sql*plus)
    ----------------------
    CREATE OR REPLACE PROCEDURE testAsHex AUTHID CURRENT_USER AS LANGUAGE JAVA NAME
    'AESTest.testAsHex()';
   
   
    OUTPUT
    ------------------------
    SQL> exec testashex()
    result to String is sun.net.www.protocol.jar.URLJarFile@e8955a6a
    result to String is sun.net.www.protocol.jar.URLJarFile@e2d09bfb
    result to String is sun.net.www.protocol.jar.URLJarFile@efb139e3
    java.security.NoSuchAlgorithmException: Algorithm AES not available
            at javax.crypto.SunJCE_b.a(DashoA6275)
            at javax.crypto.KeyGenerator.getInstance(DashoA6275)
            at AESTest.testAsHex(AESTest:41)
    BEGIN testashex(); END;
   
    *
    ERROR at line 1:
    ORA-29532: Java call terminated by uncaught Java exception:
    java.security.NoSuchAlgorithmException: Algorithm AES not available
    ORA-06512: at "TESTCASE.TESTASHEX", line 1
    ORA-06512: at line 1

Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms