当前位置: 首页>>代码示例>>Java>>正文


Java XMLCipher.encryptKey方法代码示例

本文整理汇总了Java中org.apache.xml.security.encryption.XMLCipher.encryptKey方法的典型用法代码示例。如果您正苦于以下问题:Java XMLCipher.encryptKey方法的具体用法?Java XMLCipher.encryptKey怎么用?Java XMLCipher.encryptKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.xml.security.encryption.XMLCipher的用法示例。


在下文中一共展示了XMLCipher.encryptKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testEncryptElement

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@Test
public void testEncryptElement() {
    // Arrange
    Key aesSymmetricKey = null;
    Key deSedeEncryptKey = null;
    EncryptedKey encryptedKey = null;
    Element rootElement = null;
    try {
        aesSymmetricKey = EncryptTool.generateDataEncryptionKey();
        deSedeEncryptKey = EncryptTool.generateKeyEncryptionKey();
        String algorithmURI = XMLCipher.TRIPLEDES_KeyWrap;
        XMLCipher keyCipher = XMLCipher.getInstance(algorithmURI);
        keyCipher.init(XMLCipher.WRAP_MODE, deSedeEncryptKey);
        encryptedKey = keyCipher.encryptKey(c32Document, aesSymmetricKey);
        rootElement = c32Document.getDocumentElement();

        String notEncrypted = documentXmlConverter
                .convertXmlDocToString(c32Document);

        // Act
        documentEncrypter.encryptElement(c32Document, aesSymmetricKey,
                encryptedKey, rootElement);
        String encrypted = documentXmlConverter
                .convertXmlDocToString(c32Document);

        // Assert
        assertNotEquals(notEncrypted, encrypted);
        assertTrue(XmlComparator.compareXMLs(testEncrypted, encrypted,
                Arrays.asList("CipherData")).similar());

    } catch (Exception e) {
        fail(e.getMessage().toString());
    }
}
 
开发者ID:bhits,项目名称:dss-api,代码行数:35,代码来源:DocumentEncrypterImplTest.java

示例2: testRSAOAEP11KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@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<String>();
    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);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:37,代码来源:KeyWrapEncryptionAlgorithmTest.java

示例3: testAES128KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@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<String>();
    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);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:41,代码来源:KeyWrapEncryptionAlgorithmTest.java

示例4: testAES256KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@org.junit.Test
public void testAES256KW() 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(256);
    SecretKey key = keygen.generateKey();

    // Set up the Key Wrapping Key
    XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_256_KeyWrap);
    keygen = KeyGenerator.getInstance("AES");
    keygen.init(256);
    SecretKey keyWrappingKey = keygen.generateKey();
    cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);

    List<String> localNames = new ArrayList<String>();
    localNames.add("PaymentInfo");

    String encryptionAlgorithm = XMLCipher.AES_256;

    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);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:41,代码来源:KeyWrapEncryptionAlgorithmTest.java

示例5: testTripleDESKW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@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<String>();
    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);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:39,代码来源:KeyWrapEncryptionAlgorithmTest.java

示例6: testRSAv15KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@org.junit.Test
public void testRSAv15KW() 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_v1dot5);
    cipher.init(XMLCipher.WRAP_MODE, rsaKeyPair.getPublic());
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);

    List<String> localNames = new ArrayList<String>();
    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);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:37,代码来源:KeyWrapEncryptionAlgorithmTest.java

示例7: encryptDocument

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@Override
public String encryptDocument(Key deSedeEncryptKey, String document,
                              RuleExecutionContainer ruleExecutionContainer) {
    Document xmlDocument = null;
    String xmlString = null;
    boolean encryptDoc = false;

    for (final RuleExecutionResponse response : ruleExecutionContainer
            .getExecutionResponseList()) {
        if (response.getDocumentObligationPolicy().equals(
                ObligationPolicyDocument.ENCRYPT)) {
            encryptDoc = true;
            break;
        }
    }

    if (encryptDoc) {

        try {
            xmlDocument = documentXmlConverter.loadDocument(document);

/*
             * Get a key to be used for encrypting the element. Here we are
 * generating an AES key.
 */
            final Key aesSymmetricKey = EncryptTool
                    .generateDataEncryptionKey();

            final String algorithmURI = XMLCipher.TRIPLEDES_KeyWrap;

            final XMLCipher keyCipher = XMLCipher.getInstance(algorithmURI);
            keyCipher.init(XMLCipher.WRAP_MODE, deSedeEncryptKey);
            final EncryptedKey encryptedKey = keyCipher.encryptKey(
                    xmlDocument, aesSymmetricKey);

            // encrypt the contents of the document element
            final Element rootElement = xmlDocument.getDocumentElement();

            encryptElement(xmlDocument, aesSymmetricKey, encryptedKey,
                    rootElement);

            // Output encrypted doc to file
            // FileHelper.writeDocToFile(xmlDocument, "Encrypted_C32.xml");

            xmlString = documentXmlConverter
                    .convertXmlDocToString(xmlDocument);
        } catch (final Exception e) {
            logger.error(e.getMessage(), e);
            throw new DocumentSegmentationException(e.toString(), e);
        }
    }
    return xmlString;
}
 
开发者ID:bhits,项目名称:dss-api,代码行数:54,代码来源:DocumentEncrypterImpl.java

示例8: testAES128KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@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);

    // Encrypt using DOM
    List<String> localNames = new ArrayList<String>();
    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);

    // Convert Document to a Stream Reader
    javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(baos));
    final XMLStreamReader xmlStreamReader =
            xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));

    // Decrypt
    XMLSecurityProperties properties = new XMLSecurityProperties();
    properties.setDecryptionKey(keyWrappingKey);
    InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
    TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
    XMLStreamReader securityStreamReader =
            inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);

    document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);

    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:57,代码来源:KeyWrapEncryptionVerificationTest.java

示例9: testAES192KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@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);

    // Encrypt using DOM
    List<String> localNames = new ArrayList<String>();
    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);

    // Convert Document to a Stream Reader
    javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(baos));
    final XMLStreamReader xmlStreamReader =
            xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));

    // Decrypt
    XMLSecurityProperties properties = new XMLSecurityProperties();
    properties.setDecryptionKey(keyWrappingKey);
    InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
    TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
    XMLStreamReader securityStreamReader =
            inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);

    document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);

    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:57,代码来源:KeyWrapEncryptionVerificationTest.java

示例10: testAES256KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@Test
public void testAES256KW() 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(256);
    SecretKey key = keygen.generateKey();

    // Set up the Key Wrapping Key
    XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_256_KeyWrap);
    keygen = KeyGenerator.getInstance("AES");
    keygen.init(256);
    SecretKey keyWrappingKey = keygen.generateKey();
    cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);

    // Encrypt using DOM
    List<String> localNames = new ArrayList<String>();
    localNames.add("PaymentInfo");

    String encryptionAlgorithm = XMLCipher.AES_256;
    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);

    // Convert Document to a Stream Reader
    javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(baos));
    final XMLStreamReader xmlStreamReader =
            xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));

    // Decrypt
    XMLSecurityProperties properties = new XMLSecurityProperties();
    properties.setDecryptionKey(keyWrappingKey);
    InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
    TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
    XMLStreamReader securityStreamReader =
            inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);

    document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);

    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:57,代码来源:KeyWrapEncryptionVerificationTest.java

示例11: testTripleDESKW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@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);

    // Encrypt using DOM
    List<String> localNames = new ArrayList<String>();
    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);

    // Convert Document to a Stream Reader
    javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(baos));
    final XMLStreamReader xmlStreamReader =
            xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));

    // Decrypt
    XMLSecurityProperties properties = new XMLSecurityProperties();
    properties.setDecryptionKey(keyWrappingKey);
    InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
    TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
    XMLStreamReader securityStreamReader =
            inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);

    document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);

    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:55,代码来源:KeyWrapEncryptionVerificationTest.java

示例12: testRSAv15KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@Test
public void testRSAv15KW() 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_v1dot5);
    Key keyWrappingKey = rsaKeyPair.getPublic();
    cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);

    // Encrypt using DOM
    List<String> localNames = new ArrayList<String>();
    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);

    // Convert Document to a Stream Reader
    javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(baos));
    final XMLStreamReader xmlStreamReader =
            xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));

    // Decrypt
    XMLSecurityProperties properties = new XMLSecurityProperties();
    properties.setDecryptionKey(rsaKeyPair.getPrivate());
    InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
    TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
    XMLStreamReader securityStreamReader =
            inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);

    document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);

    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:54,代码来源:KeyWrapEncryptionVerificationTest.java

示例13: testRSAOAEPKW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@Test
public void testRSAOAEPKW() 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);
    Key keyWrappingKey = rsaKeyPair.getPublic();
    cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);

    // Encrypt using DOM
    List<String> localNames = new ArrayList<String>();
    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);

    // Convert Document to a Stream Reader
    javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(baos));
    final XMLStreamReader xmlStreamReader =
            xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));

    // Decrypt
    XMLSecurityProperties properties = new XMLSecurityProperties();
    properties.setDecryptionKey(rsaKeyPair.getPrivate());
    InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
    TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
    XMLStreamReader securityStreamReader =
            inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);

    document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);

    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:54,代码来源:KeyWrapEncryptionVerificationTest.java

示例14: testRSAOAEP11KW

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@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);
    Key keyWrappingKey = rsaKeyPair.getPublic();
    cipher.init(XMLCipher.WRAP_MODE, keyWrappingKey);
    EncryptedKey encryptedKey = cipher.encryptKey(document, key);

    // Encrypt using DOM
    List<String> localNames = new ArrayList<String>();
    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);

    // Convert Document to a Stream Reader
    javax.xml.transform.Transformer transformer = transformerFactory.newTransformer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(baos));
    final XMLStreamReader xmlStreamReader =
            xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray()));

    // Decrypt
    XMLSecurityProperties properties = new XMLSecurityProperties();
    properties.setDecryptionKey(rsaKeyPair.getPrivate());
    InboundXMLSec inboundXMLSec = XMLSec.getInboundWSSec(properties);
    TestSecurityEventListener securityEventListener = new TestSecurityEventListener();
    XMLStreamReader securityStreamReader =
            inboundXMLSec.processInMessage(xmlStreamReader, null, securityEventListener);

    document = StAX2DOM.readDoc(XMLUtils.createDocumentBuilder(false), securityStreamReader);

    // Check the CreditCard decrypted ok
    nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
    Assert.assertEquals(nodeList.getLength(), 1);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:54,代码来源:KeyWrapEncryptionVerificationTest.java

示例15: main

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
public static void main(String unused[]) throws Exception {

        Document document = createSampleDocument();

        /*
         * Get a key to be used for encrypting the element.
         * Here we are generating an AES key.
         */
        Key symmetricKey = GenerateDataEncryptionKey();

        /*
         * Get a key to be used for encrypting the symmetric key.
         * Here we are generating a DESede key.
         */
        Key kek = GenerateAndStoreKeyEncryptionKey();

        String algorithmURI = XMLCipher.TRIPLEDES_KeyWrap;

        XMLCipher keyCipher =
            XMLCipher.getInstance(algorithmURI);
        keyCipher.init(XMLCipher.WRAP_MODE, kek);
        EncryptedKey encryptedKey =
            keyCipher.encryptKey(document, symmetricKey);

        /*
         * Let us encrypt the contents of the document element.
         */
        Element rootElement = document.getDocumentElement();

        algorithmURI = XMLCipher.AES_128;

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

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

        /*
         * doFinal -
         * "true" below indicates that we want to encrypt element's content
         * and not the element itself. Also, the doFinal method would
         * modify the document by replacing the EncrypteData element
         * for the data to be encrypted.
         */
        xmlCipher.doFinal(document, rootElement, true);

        /*
         * Output the document containing the encrypted information into
         * a file.
         */
        outputDocToFile(document, "build/encryptedInfo.xml");
    }
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:59,代码来源:Encrypter.java


注:本文中的org.apache.xml.security.encryption.XMLCipher.encryptKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。