Skip to Main Content

Java Security

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!

How to Generate a key (for SecretKeySpec) out of a string literal.

843811Jun 9 2009 — edited Jun 11 2009
Hi,

I have a string literal representation of a HEX value key which I use to send encrypted data from C++ to Java (on Sockets).

I haven't managed to convert the string representation to a value that I can use with SecretKeySpec() constructor to create a working AES 128b key.

The last attempt was this:
String strKey = new String("E5E6E7E9EA392A2B2D256489012145E5");
byte[] byteKey = new BigInteger(strKey,16).toByteArray(); 
SecretKey skey = new SecretKeySpec(byteKey, "AES");
It didn't work because the byte array was of size 17 byte. (java.security.InvalidKeyException: Invalid AES key length: 17 bytes)

Is there an obvious straight forward way to this which I'm missing? (How would you transfer a key in HEX representation?)

Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 9 2009
Added on Jun 9 2009
5 comments
2,161 views