Hi,
For below code I am getting java.security.InvalidKeyException: Invalid AES key length: 8 bytes error.
public Message addUserTokens(SOAPEnvelope unsignedEnvelope)
throws Exception
{
// Get the message as document
Document doc = unsignedEnvelope.getAsDocument();
usrNameToken.build(doc, username, password);
usrNameToken.setId(idValue);
// Add an Id to it.
Element usrEle = (Element) (doc.getElementsByTagNameNS(
WSConstants.WSSE_NS, "UsernameToken").item(0));
Reference ref = new Reference(doc);
ref.setURI("#" + idValue);
ref.setValueType("UsernameToken");
SecurityTokenReference secRef = new SecurityTokenReference(doc);
secRef.setReference(ref);
// adding the namespace
WSSecurityUtil.setNamespace(secRef.getElement(), WSConstants.WSSE_NS,
WSConstants.WSSE_PREFIX);
serialize(doc, System.out);
WSEncryptBody wsEncrypt = new WSEncryptBody();
// Setting necessary parameters in WSEncryptBody.
wsEncrypt.setKeyIdentifierType(WSConstants.EMBED_SECURITY_TOKEN_REF);
wsEncrypt.setSecurityTokenReference(secRef);
wsEncrypt.setKey(key);
// Encrypt using the using the key
Document encDoc = wsEncrypt.build(doc, crypto);
----------------------------
org.apache.ws.security.WSSecurityException: Cannot encrypt/decrypt data; nested exception is:
org.apache.xml.security.encryption.XMLEncryptionException: Invalid AES key length: 8 bytes
Original Exception was java.security.InvalidKeyException: Invalid AES key length: 8 bytes
at org.apache.ws.security.message.WSEncryptBody.doEncryption(WSEncryptBody.java:545)
at org.apache.ws.security.message.WSEncryptBody.buildEmbedded(WSEncryptBody.java:607)
at org.apache.ws.security.message.WSEncryptBody.build(WSEncryptBody.java:298)
at com.paychex.ws.ana.handler.client.WSSecurityClient.addUserTokens(WSSecurityClient.java:318)
at com.paychex.ws.ana.handler.client.WSSecurityClient.main(WSSecurityClient.java:94)
Caused by: org.apache.xml.security.encryption.XMLEncryptionException: Invalid AES key length: 8 bytes
Original Exception was java.security.InvalidKeyException: Invalid AES key length: 8 bytes
at org.apache.xml.security.encryption.XMLCipher.encryptData(Unknown Source)
at org.apache.xml.security.encryption.XMLCipher.encryptElementContent(Unknown Source)
at org.apache.xml.security.encryption.XMLCipher.doFinal(Unknown Source)
at org.apache.ws.security.message.WSEncryptBody.doEncryption(WSEncryptBody.java:543)
... 4 more
Hi!
Lester is correct, there is a minimum key length of 128 bits (16 bytes).
I just want to add a link to an article which can give some more details on AES key lengths: http://en.wikipedia.org/wiki/Advanced_Encryption_Standard If you want to learn some more about cryptography use in Java, I would like to recommend the excellent book "Beginning Cryptography with Java".
Best wishes!