views:

97

answers:

4

This is the hex format of a secret key used for AES encryption

00010203050607080A0B0C0D0F101112

Can i generate the original SecretKey format or byte array from this?

If so how?

A: 

Maybe this would be of help:

http://forums.sun.com/thread.jspa?messageID=2661419#2661419

William
A: 

A SecretKey is an interface. What type was the implementation? J2SE has two implementing classes, KerberosKey and SecretKeySpec, both which have constructors with a byte array as a parameter.

Ben S
i am using the SecretKeySpec
rover12
A: 

What is the original SecretKey format?
Are you aware byte[] contains signed bytes valued from -128 to 127? Would it hurt if you tried this:

byte[] key = {
  0x00,0x01,0x02,0x03,0x05,0x06,0x07,0x08,
  0x0A,0x0B,0x0C,0x0D,0x0F,0x10,0x11,0x12
};

Note: If you have values like 0x80 - 0xFF you'll need to cast them as (byte)0x80 - (byte)0xFF to avoid a warning about the range.

dlamblin
+1  A: 

You can use Apache Commons Codec to perform the hex decoding,

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html#decodeHex%28char%5B%5D)

If you don't want use any libraries, you can also do this,

byte[] bytes = new BigInteger("7F" + str, 16).toByteArray();
SecretKeySpec key = new SecretKeySpec(bytes, 1, bytes.length-1, "AES");

You have to add an extra byte 0x7F to prevent BigInteger from stripping leading zeros or adding signed bytes.

ZZ Coder