Search in sources :

Example 46 with EncryptedKey

use of org.apache.xml.security.encryption.EncryptedKey in project santuario-java by apache.

the class XMLEncryption11Test method createEncryptedKey.

/**
 * Create an EncryptedKey object using the given parameters.
 */
private EncryptedKey createEncryptedKey(Document doc, X509Certificate rsaCert, Key sessionKey, String encryptionMethod, String digestMethod, String mgfAlgorithm, byte[] oaepParams) throws Exception {
    // Create the XMLCipher element
    XMLCipher cipher = XMLCipher.getInstance(encryptionMethod, null, digestMethod);
    cipher.init(XMLCipher.WRAP_MODE, rsaCert.getPublicKey());
    EncryptedKey encryptedKey = cipher.encryptKey(doc, sessionKey, mgfAlgorithm, oaepParams);
    KeyInfo builderKeyInfo = encryptedKey.getKeyInfo();
    if (builderKeyInfo == null) {
        builderKeyInfo = new KeyInfo(doc);
        encryptedKey.setKeyInfo(builderKeyInfo);
    }
    X509Data x509Data = new X509Data(doc);
    x509Data.addCertificate(rsaCert);
    builderKeyInfo.add(x509Data);
    return encryptedKey;
}
Also used : EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) KeyInfo(org.apache.xml.security.keys.KeyInfo) XMLCipher(org.apache.xml.security.encryption.XMLCipher) X509Data(org.apache.xml.security.keys.content.X509Data)

Example 47 with EncryptedKey

use of org.apache.xml.security.encryption.EncryptedKey in project santuario-java by apache.

the class KeyWrapEncryptionAlgorithmTest method testAES192KW.

@org.junit.Test
public void testAES192KW() throws Exception {
    // Read in plaintext document
    InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
    DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
    Document document = builder.parse(sourceDocument);
    // Set up the Key
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(192);
    SecretKey key = keygen.generateKey();
    // Set up the Key Wrapping Key
    XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_192_KeyWrap);
    keygen = KeyGenerator.getInstance("AES");
    keygen.init(192);
    SecretKey keyWrappingKey = keygen.generateKey();
    cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);
    List<String> localNames = new ArrayList<>();
    localNames.add("PaymentInfo");
    String encryptionAlgorithm = XMLCipher.AES_192;
    encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
    // Check the CreditCard encrypted ok
    NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 0);
    // XMLUtils.outputDOM(document, System.out);
    document = decrypt(document, keyWrappingKey);
    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
Also used : SecretKey(javax.crypto.SecretKey) DocumentBuilder(javax.xml.parsers.DocumentBuilder) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) XMLCipher(org.apache.xml.security.encryption.XMLCipher) Document(org.w3c.dom.Document) KeyGenerator(javax.crypto.KeyGenerator)

Example 48 with EncryptedKey

use of org.apache.xml.security.encryption.EncryptedKey in project santuario-java by apache.

the class KeyWrapEncryptionAlgorithmTest method testRSAOAEP11KW.

@org.junit.Test
public void testRSAOAEP11KW() throws Exception {
    // Read in plaintext document
    InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
    DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
    Document document = builder.parse(sourceDocument);
    // Set up the Key
    KeyGenerator keygen = KeyGenerator.getInstance("DESede");
    SecretKey key = keygen.generateKey();
    // Set up the Key Wrapping Key
    XMLCipher cipher = XMLCipher.getInstance(XMLCipher.RSA_OAEP_11);
    cipher.init(XMLCipher.WRAP_MODE, rsaKeyPair.getPublic());
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);
    List<String> localNames = new ArrayList<>();
    localNames.add("PaymentInfo");
    String encryptionAlgorithm = XMLCipher.TRIPLEDES;
    encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
    // Check the CreditCard encrypted ok
    NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 0);
    // XMLUtils.outputDOM(document, System.out);
    document = decrypt(document, rsaKeyPair.getPrivate());
    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
Also used : SecretKey(javax.crypto.SecretKey) DocumentBuilder(javax.xml.parsers.DocumentBuilder) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) XMLCipher(org.apache.xml.security.encryption.XMLCipher) Document(org.w3c.dom.Document) KeyGenerator(javax.crypto.KeyGenerator)

Example 49 with EncryptedKey

use of org.apache.xml.security.encryption.EncryptedKey in project santuario-java by apache.

the class KeyWrapEncryptionAlgorithmTest method testTripleDESKW.

@org.junit.Test
public void testTripleDESKW() throws Exception {
    // Read in plaintext document
    InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
    DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
    Document document = builder.parse(sourceDocument);
    // Set up the Key
    KeyGenerator keygen = KeyGenerator.getInstance("DESede");
    SecretKey key = keygen.generateKey();
    // Set up the Key Wrapping Key
    XMLCipher cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES_KeyWrap);
    keygen = KeyGenerator.getInstance("DESede");
    SecretKey keyWrappingKey = keygen.generateKey();
    cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);
    List<String> localNames = new ArrayList<>();
    localNames.add("PaymentInfo");
    String encryptionAlgorithm = XMLCipher.TRIPLEDES;
    encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
    // Check the CreditCard encrypted ok
    NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 0);
    // XMLUtils.outputDOM(document, System.out);
    document = decrypt(document, keyWrappingKey);
    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
Also used : SecretKey(javax.crypto.SecretKey) DocumentBuilder(javax.xml.parsers.DocumentBuilder) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) XMLCipher(org.apache.xml.security.encryption.XMLCipher) Document(org.w3c.dom.Document) KeyGenerator(javax.crypto.KeyGenerator)

Example 50 with EncryptedKey

use of org.apache.xml.security.encryption.EncryptedKey in project santuario-java by apache.

the class KeyWrapEncryptionAlgorithmTest method testAES128KW.

@org.junit.Test
public void testAES128KW() throws Exception {
    // Read in plaintext document
    InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("ie/baltimore/merlin-examples/merlin-xmlenc-five/plaintext.xml");
    DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
    Document document = builder.parse(sourceDocument);
    // Set up the Key
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);
    SecretKey key = keygen.generateKey();
    // Set up the Key Wrapping Key
    XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_128_KeyWrap);
    keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);
    SecretKey keyWrappingKey = keygen.generateKey();
    cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);
    List<String> localNames = new ArrayList<>();
    localNames.add("PaymentInfo");
    String encryptionAlgorithm = XMLCipher.AES_128;
    encrypt(encryptedKey, encryptionAlgorithm, document, localNames, key);
    // Check the CreditCard encrypted ok
    NodeList nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 0);
    // XMLUtils.outputDOM(document, System.out);
    document = decrypt(document, keyWrappingKey);
    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
Also used : SecretKey(javax.crypto.SecretKey) DocumentBuilder(javax.xml.parsers.DocumentBuilder) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) XMLCipher(org.apache.xml.security.encryption.XMLCipher) Document(org.w3c.dom.Document) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

EncryptedKey (org.apache.xml.security.encryption.EncryptedKey)54 XMLCipher (org.apache.xml.security.encryption.XMLCipher)44 Document (org.w3c.dom.Document)43 SecretKey (javax.crypto.SecretKey)38 NodeList (org.w3c.dom.NodeList)35 DocumentBuilder (javax.xml.parsers.DocumentBuilder)28 KeyGenerator (javax.crypto.KeyGenerator)27 InputStream (java.io.InputStream)23 ArrayList (java.util.ArrayList)23 Key (java.security.Key)21 Element (org.w3c.dom.Element)20 EncryptedData (org.apache.xml.security.encryption.EncryptedData)18 PrivateKey (java.security.PrivateKey)16 KeyInfo (org.apache.xml.security.keys.KeyInfo)16 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 XMLStreamReader (javax.xml.stream.XMLStreamReader)12 DOMSource (javax.xml.transform.dom.DOMSource)12 StreamResult (javax.xml.transform.stream.StreamResult)12 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)12