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!

Decryption problem while decrypting the encrypted xml

843811Jul 2 2009 — edited Jul 3 2009
Hello,

We had used the following code to decrypt,
public static Document decryptXMLDocument(Document document, Key key,
			X509Certificate x509Certificate) throws DocumentEncryptException {

		try {
			if (null == document) {
				Debug.print("Document is null");
			}
			

			XMLSignatureUtil.sign((PrivateKey) key, x509Certificate, document1);
			String namespaceURI = EncryptionConstants.EncryptionSpecNS;
			String localName = EncryptionConstants._TAG_ENCRYPTEDDATA;
			Element encryptedDataElement = (Element) document
					.getElementsByTagNameNS(namespaceURI, localName).item(0);
			
			XMLCipher xmlCipher = XMLCipher.getInstance();
			if (null == xmlCipher) {
				Debug.print("xmlCipher is null");
			}

			xmlCipher.init(XMLCipher.DECRYPT_MODE, null);
			if (null == key) {
				Debug.print("key is null");
			}

			xmlCipher.setKEK(key);

			document = xmlCipher.doFinal(document, encryptedDataElement);
			Debug.print("xmlCipher.setKEK(key);" + xmlCipher.getEncryptedKey());
			return document;
		} catch (Exception e) {
			throw new DocumentEncryptException(
					"Invalid Key used for decryption");
		}
	}
This code is working for all of the documents until now. Suddenly for some documents, it is returning the same encrypted document. It is not even raising any exception.

Note: Document is almost 1 mb in size.

It is encrypted using the following code,
public static Document encryptXMLDocument(Document document, Key key)
			throws DocumentEncryptException {

		try {
			Key symmetricKey = generateDataEncryptionKey();

			XMLCipher keyCipher = null;

			keyCipher = XMLCipher.getInstance(XMLCipher.RSA_v1dot5);

			keyCipher.init(XMLCipher.WRAP_MODE, key);
			EncryptedKey encryptedKey = keyCipher.encryptKey(document,
					symmetricKey);

			Element rootElement = document.getDocumentElement();

			XMLCipher xmlCipher = XMLCipher.getInstance(XMLCipher.AES_128);
			xmlCipher.init(XMLCipher.ENCRYPT_MODE, symmetricKey);

			// Setting keyinfo inside the encrypted data being prepared.
			EncryptedData encryptedDataElement = xmlCipher.getEncryptedData();
			KeyInfo keyInfo = new KeyInfo(document);
			keyInfo.add(encryptedKey);
			encryptedDataElement.setKeyInfo(keyInfo);

			document = xmlCipher.doFinal(document, rootElement, true);
		} catch (NoSuchAlgorithmException nsae) {
			throw new DocumentEncryptException("Invalid Algorithm : "
					+ nsae.getMessage());
		} catch (XMLEncryptionException xmlee) {
			xmlee.printStackTrace();
			throw new DocumentEncryptException(
					"Document could not be Encrypted : " + xmlee.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new DocumentEncryptException(
					"Document could not be Encrypted : " + e.getMessage());
		}
		return document;
	}
Could any one shed some light on this?

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 31 2009
Added on Jul 2 2009
15 comments
326 views