I seem to have a problem converting the byte array to a string using normal java String class.
I read that the key uses a X.509 spec which is encoded in utf-8

I also wrote a hex string builder as well and the results are different.

KeyGenerator keyGen = KeyGenerator.getInstance("Blowfish", "SunJCE");
keyGen.init(256);
SecretKey key = keyGen.generateKey();
System.out.println(toHexString(key.getEncoded()));
System.out.println(new String(key.getEncoded(), "utf8"));

private static final char[] HEX_CHARS = {'0', '1', '2' ,'3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f'};

private String toHexString(byte[] bytes)
{
    StringBuilder sb = new StringBuilder();
    int high = 0;
    int low = 0;
    for(int i = 0; i < bytes.length; i++) {
        high = ((bytes[i] & 0xF0) >> 4);
        low = (bytes[i] & 0x0F);
        sb.append(HEX_CHARS[high]);
        sb.append(HEX_CHARS[low]);
    }
   
    return sb.toString();
}

The results look like this
4a63ea27b46504660cc3c6c2f22de1a35f3eabdc468cc6e64928b369c8ff1681
Jc�'�e f ����-�_>��F���I(�i�� �

Sorry to trouble you about this as this is not really Derby related.. but is that toHexString method ok to use? or is there an easier way to convert the bytes using String class, because I can't seem to get anywhere with it.

Thanks.
--
George H
george.dma@gmail.com


On Tue, Dec 1, 2009 at 10:41 AM, Francois Orsini <francois.orsini@gmail.com> wrote:
Hi George,

key.getEncoded() returns a byte array that is a textual representation of the external key in Hex.

Just Stringify the byte array and set it to the 'encryptedKey' JDBC connection URL attribute - that should work.

Cheers,

--Francois


On Mon, Nov 30, 2009 at 10:45 PM, George H <george.dma@gmail.com> wrote:
Hi,

I am trying to create an embedded derby db using an encryption key that I generate before hand. I've read the derby v10.5.3.0 ref guide and the developer guide and I still have one question that lingers in my mind.

In the JDBC url where I have to specify the encryption key attribute how do we get the key in string form? In the docs they use examples (Dev guide page 114 (PDF)) where the key looks like a letters and numbers and in most cases just numbers.

Now when I generate my key like

KeyGenerator keyGen = KeyGenerator.getInstance("AES")
keyGen.init(256);
SecretKey key = keyGen.generateKey();

I only have the method   key.getEncoded() that I can use which returns an array of bytes. I wonder how I can give this key to derby in the jdbc url ? Do I have to convert the byte array to a HEX string ? or to decimals? I don't know. If someone could help me out on this specific part it would be great.

Thanks
--
George H
george.dma@gmail.com