/* 08/31/04 PDM - pseudo code for simple Java wrapper of native C code There are certainly much fancier ways to wrap the native code, i.e. to support exceptions from the C code, to allow encryption / decryption of parts of a byte[], etc. This is just a suggestion for a simple java implementation designed to take a minimum of implementation time. This template allows the native code to return integer error codes, which may be handled as the implementor sees fit. See JNI Notes for help with integrating this file with the native C code. */ public class AESWrapper { /* a simple 256 bit test key */ private final static String TEST_KEY = "12345678901234567890123456789012"; /***************************************************** Native methods, implemented in C ******************************************************/ private native int aes_init(byte[] key, int nbits); private native int aes_encrypt(byte[] input, byte[] output); private native int aes_decrypt(byte[] input, byte[] output); /* Load the shared library that implements the native methods This will attempt to load libaes.so from the java.library.path which should work by default as long as libaes.so resides in the path specified by the environment variable LD_LIBRARY_PATH. If that does not seem to work, one can specify -Djava.library.path="..." on the command line to explicitly tell the JRE where to look for libraries. */ static { System.loadLibrary("aes"); } /* A test program that will encrypt and then decrypt all parameters */ public static void main(String[] args) { AESWrapper aes = new AESWrapper(); byte[] keyBytes = TEST_KEY.getBytes(); aes.init(keyBytes, keyBytes.length); for( int ii = 0 ; ii < args.length ; ii++ ) { String decryption = new String(aes.decrypt(aes.encrypt(args[ii].getBytes()))); System.out.println(args[ii] + " -> " + decryption); } } // create an uninitialized instance, init() must be called public AESWrapper() {} /*=================================================== Java wrappers for native functions ====================================================*/ public byte[] encrypt(byte[] input) { // if necessary create padded copy of input with length multiple of 16 // create output buffer of matching size // repeatedly invoke aes_encrypt on 16 byte segments // accumulating into output buffer // return output buffer return null; } public byte[] decrypt(byte[] input) { // ensure that input is length 16 // create output buffer of matching size // repeatedly invoke aes_decrypt on 16 byte segments // accumulating into output buffer // return output buffer return null; } public int init(byte[] key, int nbytes) { // invoke aes_init(key, nbytes * 8) return 0; } }