1

I was given this key in hex String: "140b41b22a29beb4061bda66b6747e14" and requested to decrypt an one block cipher with an AES implementing a ECB.

As we know, the key must be 16 bytes long. But the given key contains elements which correspond to characters larger than one byte (e.g. 0xb2 whose char value is ² corresponding to 2 bytes).

In fact, if I convert the hex String key into a key String I obtain " A²*)¾´Úf¶t~ ", then if I apply the method key.getBytes().length what I get is that the key is 21 bytes long.

My question is: is there any way to encrypt a 16 bytes long ciphertext with an AES given this key in Java?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
mielgosaez
  • 11
  • 1

1 Answers1

1

Your key looks correctly sized - just don't think of it as a string with meaningful characters. Instead, use a hex conversion method to convert it into a 16 byte array and use that as the key.

E.g.

String keyString = "140b41b22a29beb4061bda66b6747e14";
byte[] keyBytes = DatatypeConverter.parseHexBinary(keyString);
SecretKey key = new SecretKeySpec(keyBytes, "AES");
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • We're still in full sync regarding points Duncan, quite amazing. PS I'm trying to have [tag:cipher] be a synonym for [tag:encryption]. I don't know how this exactly works, but if you find out how to vote on it it would be welcome... – Maarten Bodewes Jan 30 '14 at 00:32
  • @owlstead Yup, I've voted by going to http://stackoverflow.com/tags/encryption/synonyms and hitting the up-vote button. Good call. Race you to 20k :-) A little strange that [tag:decryption] and [tag:decrypt] are synonyms of [tag:encryption] already... – Duncan Jones Jan 30 '14 at 08:02
  • @owlstead FYI - I opened a topic on that latter issue: http://meta.stackexchange.com/questions/217994/decryption-tag-should-not-be-synonymous-with-encryption. I'd welcome your thoughts (not just asking for blind support here). – Duncan Jones Jan 30 '14 at 08:37