xmlcipher.java
来自「JAVA 所有包」· Java 代码 · 共 1,715 行 · 第 1/5 页
JAVA
1,715 行
* <b>Note:</b> This should only be used in cases where the context * document has been passed in via a call to doFinal. * * @param encryptedKey EncryptedKey object to martial * @return the DOM <code>Element</code> representing the passed in * object */ public Element martial(EncryptedKey encryptedKey) { return (_factory.toElement (encryptedKey)); } /** * Martial an EncryptedData * * Takes an EncryptedData object and returns a DOM Element that * represents the appropriate <code>EncryptedData</code> * * @param context The document that will own the returned nodes * @param encryptedData EncryptedData object to martial * @return the DOM <code>Element</code> representing the passed in * object */ public Element martial(Document context, EncryptedData encryptedData) { _contextDocument = context; return (_factory.toElement (encryptedData)); } /** * Martial an EncryptedKey * * Takes an EncryptedKey object and returns a DOM Element that * represents the appropriate <code>EncryptedKey</code> * * @param context The document that will own the created nodes * @param encryptedKey EncryptedKey object to martial * @return the DOM <code>Element</code> representing the passed in * object */ public Element martial(Document context, EncryptedKey encryptedKey) { _contextDocument = context; return (_factory.toElement (encryptedKey)); } /** * Encrypts an <code>Element</code> and replaces it with its encrypted * counterpart in the context <code>Document</code>, that is, the * <code>Document</code> specified when one calls * {@link #getInstance(String) getInstance}. * * @param element the <code>Element</code> to encrypt. * @return the context <code>Document</code> with the encrypted * <code>Element</code> having replaced the source <code>Element</code>. * @throws Exception */ private Document encryptElement(Element element) throws Exception{ if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypting element..."); if(null == element) logger.log(java.util.logging.Level.SEVERE, "Element unexpectedly null..."); if(_cipherMode != ENCRYPT_MODE) if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in ENCRYPT_MODE..."); if (_algorithm == null) { throw new XMLEncryptionException("XMLCipher instance without transformation specified"); } encryptData(_contextDocument, element, false); Element encryptedElement = _factory.toElement(_ed); Node sourceParent = element.getParentNode(); sourceParent.replaceChild(encryptedElement, element); return (_contextDocument); } /** * Encrypts a <code>NodeList</code> (the contents of an * <code>Element</code>) and replaces its parent <code>Element</code>'s * content with this the resulting <code>EncryptedType</code> within the * context <code>Document</code>, that is, the <code>Document</code> * specified when one calls * {@link #getInstance(String) getInstance}. * * @param element the <code>NodeList</code> to encrypt. * @return the context <code>Document</code> with the encrypted * <code>NodeList</code> having replaced the content of the source * <code>Element</code>. * @throws Exception */ private Document encryptElementContent(Element element) throws /* XMLEncryption */Exception { if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypting element content..."); if(null == element) logger.log(java.util.logging.Level.SEVERE, "Element unexpectedly null..."); if(_cipherMode != ENCRYPT_MODE) if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in ENCRYPT_MODE..."); if (_algorithm == null) { throw new XMLEncryptionException("XMLCipher instance without transformation specified"); } encryptData(_contextDocument, element, true); Element encryptedElement = _factory.toElement(_ed); removeContent(element); element.appendChild(encryptedElement); return (_contextDocument); } /** * Process a DOM <code>Document</code> node. The processing depends on the * initialization parameters of {@link #init(int, Key) init()}. * * @param context the context <code>Document</code>. * @param source the <code>Document</code> to be encrypted or decrypted. * @return the processed <code>Document</code>. * @throws Exception to indicate any exceptional conditions. */ public Document doFinal(Document context, Document source) throws /* XMLEncryption */Exception { if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Processing source document..."); if(null == context) logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); if(null == source) logger.log(java.util.logging.Level.SEVERE, "Source document unexpectedly null..."); _contextDocument = context; Document result = null; switch (_cipherMode) { case DECRYPT_MODE: result = decryptElement(source.getDocumentElement()); break; case ENCRYPT_MODE: result = encryptElement(source.getDocumentElement()); break; case UNWRAP_MODE: break; case WRAP_MODE: break; default: throw new XMLEncryptionException( "empty", new IllegalStateException()); } return (result); } /** * Process a DOM <code>Element</code> node. The processing depends on the * initialization parameters of {@link #init(int, Key) init()}. * * @param context the context <code>Document</code>. * @param element the <code>Element</code> to be encrypted. * @return the processed <code>Document</code>. * @throws Exception to indicate any exceptional conditions. */ public Document doFinal(Document context, Element element) throws /* XMLEncryption */Exception { if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Processing source element..."); if(null == context) logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); if(null == element) logger.log(java.util.logging.Level.SEVERE, "Source element unexpectedly null..."); _contextDocument = context; Document result = null; switch (_cipherMode) { case DECRYPT_MODE: result = decryptElement(element); break; case ENCRYPT_MODE: result = encryptElement(element); break; case UNWRAP_MODE: break; case WRAP_MODE: break; default: throw new XMLEncryptionException( "empty", new IllegalStateException()); } return (result); } /** * Process the contents of a DOM <code>Element</code> node. The processing * depends on the initialization parameters of * {@link #init(int, Key) init()}. * * @param context the context <code>Document</code>. * @param element the <code>Element</code> which contents is to be * encrypted. * @param content * @return the processed <code>Document</code>. * @throws Exception to indicate any exceptional conditions. */ public Document doFinal(Document context, Element element, boolean content) throws /* XMLEncryption*/ Exception { if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Processing source element..."); if(null == context) logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); if(null == element) logger.log(java.util.logging.Level.SEVERE, "Source element unexpectedly null..."); _contextDocument = context; Document result = null; switch (_cipherMode) { case DECRYPT_MODE: if (content) { result = decryptElementContent(element); } else { result = decryptElement(element); } break; case ENCRYPT_MODE: if (content) { result = encryptElementContent(element); } else { result = encryptElement(element); } break; case UNWRAP_MODE: break; case WRAP_MODE: break; default: throw new XMLEncryptionException( "empty", new IllegalStateException()); } return (result); } /** * Returns an <code>EncryptedData</code> interface. Use this operation if * you want to have full control over the contents of the * <code>EncryptedData</code> structure. * * this does not change the source document in any way. * * @param context the context <code>Document</code>. * @param element the <code>Element</code> that will be encrypted. * @return the <code>EncryptedData</code> * @throws Exception */ public EncryptedData encryptData(Document context, Element element) throws /* XMLEncryption */Exception { return encryptData(context, element, false); } /** * Returns an <code>EncryptedData</code> interface. Use this operation if * you want to have full control over the contents of the * <code>EncryptedData</code> structure. * * this does not change the source document in any way. * * @param context the context <code>Document</code>. * @param element the <code>Element</code> that will be encrypted. * @param contentMode <code>true</code> to encrypt element's content only, * <code>false</code> otherwise * @return the <code>EncryptedData</code> * @throws Exception */ public EncryptedData encryptData(Document context, Element element, boolean contentMode) throws /* XMLEncryption */ Exception { if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypting element..."); if (null == context) logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); if (null == element) logger.log(java.util.logging.Level.SEVERE, "Element unexpectedly null..."); if (_cipherMode != ENCRYPT_MODE) if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in ENCRYPT_MODE..."); _contextDocument = context; if (_algorithm == null) { throw new XMLEncryptionException("XMLCipher instance without transformation specified"); } String serializedOctets = null; if (contentMode) { NodeList children = element.getChildNodes(); if ((null != children)) { serializedOctets = _serializer.serialize(children); } else { Object exArgs[] = { "Element has no content." }; throw new XMLEncryptionException("empty", exArgs); } } else { serializedOctets = _serializer.serialize(element); } if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Serialized octets:\n" + serializedOctets); byte[] encryptedBytes = null; // Now create the working cipher if none was created already Cipher c; if (_contextCipher == null) { String jceAlgorithm = JCEMapper.translateURItoJCEID(_algorithm); if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "alg = " + jceAlgorithm); try { if (_requestedJCEProvider == null) c = Cipher.getInstance(jceAlgorithm); else c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider); } catch (NoSuchAlgorithmException nsae) { throw new XMLEncryptionException("empty", nsae); } catch (NoSuchProviderException nspre) { throw new XMLEncryptionException("empty", nspre); } catch (NoSuchPaddingException nspae) { throw new XMLEncryptionException("empty", nspae); } } else { c = _contextCipher; } // Now perform the encryption try { // Should internally generate an IV // todo - allow user to set an IV c.init(_cipherMode, _key); } catch (InvalidKeyException ike) { throw new XMLEncryptionException("empty", ike); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?