/** * Encrypts a key to an EncryptedKey structure * * @param doc the Context document that will be used to general DOM * @param key Key to encrypt (will use previously set KEK to * perform encryption * @return the <code>EncryptedKey</code> * @throws XMLEncryptionException */ public EncryptedKey encryptKey(Document doc, Key key) throws XMLEncryptionException { return encryptKey(doc, key, null, null); }
EncryptedKey encryptedKey = keyCipher.encryptKey(doc, symmetricKey);
/** * <p> * Encrypt the Key to be transported * </p> * <p> * Data is encrypted with a SecretKey. Then the key needs to be transported to the other end where it is needed for * decryption. For the Key transport, the SecretKey is encrypted with the recipient's public key. At the receiving end, the * receiver can decrypt the Secret Key using his private key.s * </p> * * @param document * @param keyToBeEncrypted Symmetric Key (SecretKey) * @param keyUsedToEncryptSecretKey Asymmetric Key (Public Key) * @param keySize Length of the key * @return * @throws ProcessingException */ public static EncryptedKey encryptKey(Document document, SecretKey keyToBeEncrypted, PublicKey keyUsedToEncryptSecretKey, int keySize) throws ProcessingException { XMLCipher keyCipher = null; String pubKeyAlg = keyUsedToEncryptSecretKey.getAlgorithm(); try { String keyWrapAlgo = getXMLEncryptionURLForKeyUnwrap(pubKeyAlg, keySize); keyCipher = XMLCipher.getInstance(keyWrapAlgo); keyCipher.init(XMLCipher.WRAP_MODE, keyUsedToEncryptSecretKey); return keyCipher.encryptKey(document, keyToBeEncrypted); } catch (XMLEncryptionException e) { throw logger.processingError(e); } }
/** * <p> * Encrypt the Key to be transported * </p> * <p> * Data is encrypted with a SecretKey. Then the key needs to be transported to the other end where it is needed for * decryption. For the Key transport, the SecretKey is encrypted with the recipient's public key. At the receiving end, the * receiver can decrypt the Secret Key using his private key.s * </p> * * @param document * @param keyToBeEncrypted Symmetric Key (SecretKey) * @param keyUsedToEncryptSecretKey Asymmetric Key (Public Key) * @param keySize Length of the key * @return * @throws ProcessingException */ public static EncryptedKey encryptKey(Document document, SecretKey keyToBeEncrypted, PublicKey keyUsedToEncryptSecretKey, int keySize) throws ProcessingException { XMLCipher keyCipher = null; String pubKeyAlg = keyUsedToEncryptSecretKey.getAlgorithm(); try { String keyWrapAlgo = getXMLEncryptionURLForKeyUnwrap(pubKeyAlg, keySize); keyCipher = XMLCipher.getInstance(keyWrapAlgo); keyCipher.init(XMLCipher.WRAP_MODE, keyUsedToEncryptSecretKey); return keyCipher.encryptKey(document, keyToBeEncrypted); } catch (XMLEncryptionException e) { throw logger.processingError(e); } }
return keyCipher.encryptKey(document, keyToBeEncrypted); } catch (XMLEncryptionException e) { throw logger.processingError(e);
return keyCipher.encryptKey(document, keyToBeEncrypted); } catch (XMLEncryptionException e) { throw logger.processingError(e);
return keyCipher.encryptKey(document, keyToBeEncrypted);
return keyCipher.encryptKey(document, keyToBeEncrypted);
apacheEncryptedKey = xmlCipher.encryptKey(containingDocument, targetKey); postProcessApacheEncryptedKey(apacheEncryptedKey, targetKey, encryptionKey, encryptionAlgorithmURI, containingDocument);
apacheEncryptedKey = xmlCipher.encryptKey(containingDocument, targetKey); postProcessApacheEncryptedKey(apacheEncryptedKey, targetKey, encryptionKey, encryptionAlgorithmURI, containingDocument);
public Document encrypt(Document doc, EncryptionOptions options) throws SecurityException { try { org.w3c.dom.Document dom = fomToDom(doc, options); Key dek = options.getDataEncryptionKey(); Key kek = options.getKeyEncryptionKey(); String dalg = options.getDataCipherAlgorithm(); String kalg = options.getKeyCipherAlgorithm(); boolean includeki = options.includeKeyInfo(); EncryptedKey enckey = null; XMLCipher xmlCipher = XMLCipher.getInstance(dalg); xmlCipher.init(XMLCipher.ENCRYPT_MODE, dek); if (includeki && kek != null && dek != null) { XMLCipher keyCipher = XMLCipher.getInstance(kalg); keyCipher.init(XMLCipher.WRAP_MODE, kek); enckey = keyCipher.encryptKey(dom, dek); EncryptedData encdata = xmlCipher.getEncryptedData(); KeyInfo keyInfo = new KeyInfo(dom); keyInfo.add(enckey); encdata.setKeyInfo(keyInfo); } dom = xmlCipher.doFinal(dom, dom.getDocumentElement(), false); return domToFom(dom, options); } catch (Exception e) { throw new SecurityException(e); } }