Package org.apache.xml.security.encryption

Examples of org.apache.xml.security.encryption.XMLCipher$Factory


            log.error("Error marshalling EncryptedData for decryption", e);
            throw e;
        }
        Element targetElement = encryptedData.getDOM();

        XMLCipher xmlCipher;
        try {
            if (getJCAProviderName() != null) {
                xmlCipher = XMLCipher.getProviderInstance(getJCAProviderName());
            } else {
                xmlCipher = XMLCipher.getInstance();
            }
            xmlCipher.init(XMLCipher.DECRYPT_MODE, dataEncKey);
        } catch (XMLEncryptionException e) {
            log.error("Error initialzing cipher instance on data decryption", e);
            throw new DecryptionException("Error initialzing cipher instance on data decryption", e);
        }

        byte[] bytes = null;
        try {
            bytes = xmlCipher.decryptToByteArray(targetElement);
        } catch (XMLEncryptionException e) {
            log.error("Error decrypting the encrypted data element", e);
            throw new DecryptionException("Error decrypting the encrypted data element", e);
        }
        if (bytes == null) {
View Full Code Here


       
        preProcessEncryptedKey(encryptedKey, algorithm, kek);
       
        Element targetElement = encryptedKey.getDOM();

        XMLCipher xmlCipher;
        try {
            if (getJCAProviderName() != null) {
                xmlCipher = XMLCipher.getProviderInstance(getJCAProviderName());
            } else {
                xmlCipher = XMLCipher.getInstance();
            }
            xmlCipher.init(XMLCipher.UNWRAP_MODE, kek);
        } catch (XMLEncryptionException e) {
            log.error("Error initialzing cipher instance on key decryption", e);
            throw new DecryptionException("Error initialzing cipher instance on key decryption", e);
        }

        org.apache.xml.security.encryption.EncryptedKey encKey;
        try {
            encKey = xmlCipher.loadEncryptedKey(targetElement.getOwnerDocument(), targetElement);
        } catch (XMLEncryptionException e) {
            log.error("Error when loading library native encrypted key representation", e);
            throw new DecryptionException("Error when loading library native encrypted key representation", e);
        }

        Key key = null;
        try {
            key = xmlCipher.decryptKey(encKey, algorithm);
        } catch (XMLEncryptionException e) {
            log.error("Error decrypting encrypted key", e);
            throw new DecryptionException("Error decrypting encrypted key", e);
        }
        if (key == null) {
View Full Code Here

            log.error("Encryption key for key encryption was null");
            throw new EncryptionException("Encryption key was null");
        }

        log.debug("Encrypting encryption key with algorithm: {}", encryptionAlgorithmURI);
        XMLCipher xmlCipher;
        try {
            if (getJCAProviderName() != null) {
                xmlCipher = XMLCipher.getProviderInstance(encryptionAlgorithmURI, getJCAProviderName());
            } else {
                xmlCipher = XMLCipher.getInstance(encryptionAlgorithmURI);
            }
            xmlCipher.init(XMLCipher.WRAP_MODE, encryptionKey);
        } catch (XMLEncryptionException e) {
            log.error("Error initializing cipher instance on key encryption", e);
            throw new EncryptionException("Error initializing cipher instance on key encryption", e);
        }

        org.apache.xml.security.encryption.EncryptedKey apacheEncryptedKey;
        try {
            apacheEncryptedKey = xmlCipher.encryptKey(containingDocument, targetKey);
            postProcessApacheEncryptedKey(apacheEncryptedKey, targetKey, encryptionKey,
                    encryptionAlgorithmURI, containingDocument);
        } catch (XMLEncryptionException e) {
            log.error("Error encrypting element on key encryption", e);
            throw new EncryptionException("Error encrypting element on key encryption", e);
        }

        EncryptedKey encryptedKey;
        try {
            Element encKeyElement = xmlCipher.martial(containingDocument, apacheEncryptedKey);
            encryptedKey = (EncryptedKey) encryptedKeyUnmarshaller.unmarshall(encKeyElement);
        } catch (UnmarshallingException e) {
            log.error("Error unmarshalling EncryptedKey element", e);
            throw new EncryptionException("Error unmarshalling EncryptedKey element");
        }
View Full Code Here

        checkAndMarshall(xmlObject);

        Element targetElement = xmlObject.getDOM();
        Document ownerDocument = targetElement.getOwnerDocument();

        XMLCipher xmlCipher;
        try {
            if (getJCAProviderName() != null) {
                xmlCipher = XMLCipher.getProviderInstance(encryptionAlgorithmURI, getJCAProviderName());
            } else {
                xmlCipher = XMLCipher.getInstance(encryptionAlgorithmURI);
            }
            xmlCipher.init(XMLCipher.ENCRYPT_MODE, encryptionKey);
        } catch (XMLEncryptionException e) {
            log.error("Error initializing cipher instance on XMLObject encryption", e);
            throw new EncryptionException("Error initializing cipher instance", e);
        }

        org.apache.xml.security.encryption.EncryptedData apacheEncryptedData;
        try {
            apacheEncryptedData = xmlCipher.encryptData(ownerDocument, targetElement, encryptContentMode);
        } catch (Exception e) {
            log.error("Error encrypting XMLObject", e);
            throw new EncryptionException("Error encrypting XMLObject", e);
        }

        EncryptedData encryptedData;
        try {
            Element encDataElement = xmlCipher.martial(ownerDocument, apacheEncryptedData);
            encryptedData = (EncryptedData) encryptedDataUnmarshaller.unmarshall(encDataElement);
        } catch (UnmarshallingException e) {
            log.error("Error unmarshalling EncryptedData element", e);
            throw new EncryptionException("Error unmarshalling EncryptedData element", e);
        }
View Full Code Here

         int keySize) throws Exception
   {
      if(keyToBeEncrypted == null)
         throw new IllegalArgumentException("secret key is null");
     
      XMLCipher keyCipher = null;
      String pubKeyAlg = keyUsedToEncryptSecretKey.getAlgorithm();
     
      String keyWrapAlgo = getXMLEncryptionURLForKeyUnwrap(pubKeyAlg, keySize);
      keyCipher = XMLCipher.getInstance(keyWrapAlgo);
        
      keyCipher.init(XMLCipher.WRAP_MODE, keyUsedToEncryptSecretKey);
      return keyCipher.encryptKey(document, keyToBeEncrypted);
   }
View Full Code Here

    */
   public static Document encrypt(Document document, SecretKey secretKey, PublicKey publicKey, int keySize)
   throws Exception
   {
      //Encrypt
      XMLCipher cipher = XMLCipher.getInstance(algorithms.get("aes-128").xmlSecName);
      cipher.init(XMLCipher.ENCRYPT_MODE, secretKey);

      //Encrypted Key
      EncryptedKey ekey = XMLEncryptionUtil.encryptKey(document, secretKey, publicKey, keySize);
      //Encrypted Data
      String encryptionAlgorithm = XMLEncryptionUtil.getXMLEncryptionURL(secretKey.getAlgorithm(), keySize);
      //Encrypt the Document
      cipher = XMLCipher.getInstance(encryptionAlgorithm);
      cipher.init(XMLCipher.ENCRYPT_MODE, secretKey);

      Document encryptedDoc =  cipher.doFinal(document, document.getDocumentElement());
      Element encryptedDocRootElement = encryptedDoc.getDocumentElement();
      // The EncryptedKey element is added
      Element encryptedKeyElement =  cipher.martial(document, ekey);

      // Outer ds:KeyInfo Element to hold the EncryptionKey
      Element sigElement = encryptedDoc.createElementNS(XMLSIG_NS, DS_KEY_INFO);
      sigElement.setAttributeNS(XMLNS, "xmlns:ds",  XMLSIG_NS);
      sigElement.appendChild(encryptedKeyElement);
View Full Code Here

      if(dataEL == null)
         throw new IllegalStateException("Encrypted Data not found");
      if(keyEL == null)
         throw new IllegalStateException("Encrypted Key not found");
     
      XMLCipher cipher =  XMLCipher.getInstance();
      cipher.init(XMLCipher.DECRYPT_MODE, null);
      EncryptedData encryptedData =  cipher.loadEncryptedData(encryptedDoc, (Element)dataEL)
      EncryptedKey encryptedKey =  cipher.loadEncryptedKey(encryptedDoc, (Element)keyEL);
     
      Document decryptedDoc = null;
     
      if (encryptedData != null && encryptedKey != null)
      {
         String encAlgoURL = encryptedData.getEncryptionMethod().getAlgorithm();
         XMLCipher keyCipher =  XMLCipher.getInstance();
         keyCipher.init(XMLCipher.UNWRAP_MODE, privateKey);
         Key encryptionKey =  keyCipher.decryptKey( encryptedKey, encAlgoURL );
         cipher =  XMLCipher.getInstance()
         cipher.init(XMLCipher.DECRYPT_MODE, encryptionKey);
         decryptedDoc = cipher.doFinal(encryptedDoc, (Element)dataEL);
      }
      return decryptedDoc;
View Full Code Here

        System.out.println("ki is null");
      }
      EncryptedKey ek = encryptedData.getKeyInfo().itemEncryptedKey(0);

      if (ek != null) {
        XMLCipher keyCipher = XMLCipher.getInstance();
        keyCipher.init(XMLCipher.UNWRAP_MODE, kek);
        key = keyCipher.decryptKey(ek, encryptedData.getEncryptionMethod().getAlgorithm());
      }

      // Create a new cipher just to be paranoid
      XMLCipher cipher3 = XMLCipher.getInstance();
      cipher3.init(XMLCipher.DECRYPT_MODE, key);
            dd = cipher3.doFinal(ed, ee);

            target = toString(dd);

      Assert.assertEquals(source, target);
    }
View Full Code Here

      encryptedElement.appendChild(d.createTextNode(tstBase64EncodedString));
      docElement.appendChild(encryptedElement);
      // dump(d);

      // Now the decrypt, with a brand new cipher
      XMLCipher cipherDecrypt = XMLCipher.getInstance();
      Key key =
        new SecretKeySpec("abcdefghijklmnop".getBytes("ASCII"), "AES");

      cipherDecrypt.init(XMLCipher.DECRYPT_MODE, key);
      byte[] decryptBytes = cipherDecrypt.decryptToByteArray(ee);

      Assert.assertEquals(new String(decryptBytes, "ASCII"),
                new String("A test encrypted secret"));
    }
    else {
View Full Code Here

                              EncryptionConstants._TAG_ENCRYPTEDKEY);

      if (isEncryptedKey) {
      log.debug("Passed an Encrypted Key");
      try {
        XMLCipher cipher = XMLCipher.getInstance();
        cipher.init(XMLCipher.UNWRAP_MODE, _kek);
        EncryptedKey ek = cipher.loadEncryptedKey(element);
        _key = cipher.decryptKey(ek, _algorithm);
      }
      catch (Exception e) {}
      }
   
      return (_key != null);
View Full Code Here

TOP

Related Classes of org.apache.xml.security.encryption.XMLCipher$Factory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.