• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java EncryptedData类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



EncryptedData类属于org.apache.xml.security.encryption包,在下文中一共展示了EncryptedData类的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: encryptElement

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
/**
   * Encrypt element.
   *
   * @param xmlDocument         the xml document
   * @param encryptSymmetricKey the encrypt symmetric key
   * @param encryptedKey        the encrypted key
   * @param element             the element
   * @throws XMLEncryptionException the xML encryption exception
   * @throws Exception              the exception
   */
  void encryptElement(Document xmlDocument, Key encryptSymmetricKey,
                      EncryptedKey encryptedKey, Element element)
          throws Exception {

      final String algorithmURI = XMLCipher.AES_128;

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

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

      xmlCipher.doFinal(xmlDocument, element, true);
  }
 
开发者ID:bhits,项目名称:dss-api,代码行数:30,代码来源:DocumentEncrypterImpl.java


示例2: decryptElement

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
/**
 * Method decryptElement
 *
 * Take a key, encryption type and a document, find an encrypted element
 * decrypt it and return the resulting document
 *
 * @param filename File to decrypt from
 * @param key The Key to use for decryption
 */
private Document decryptElement(Document doc, Key rsaKey, X509Certificate rsaCert) throws Exception {
    // Create the XMLCipher element
    XMLCipher cipher = XMLCipher.getInstance();

    // Need to pre-load the Encrypted Data so we can get the key info
    Element ee =
        (Element) doc.getElementsByTagNameNS(
            "http://www.w3.org/2001/04/xmlenc#", "EncryptedData"
        ).item(0);
    cipher.init(XMLCipher.DECRYPT_MODE, null);
    EncryptedData encryptedData = cipher.loadEncryptedData(doc, ee);

    KeyInfo ki = encryptedData.getKeyInfo();
    EncryptedKey encryptedKey = ki.itemEncryptedKey(0);
    KeyInfo kiek = encryptedKey.getKeyInfo();
    X509Data certData = kiek.itemX509Data(0);
    XMLX509Certificate xcert = certData.itemCertificate(0);
    X509Certificate cert = xcert.getX509Certificate();
    assertTrue(rsaCert.equals(cert));

    XMLCipher cipher2 = XMLCipher.getInstance();
    cipher2.init(XMLCipher.UNWRAP_MODE, rsaKey);
    Key key =
        cipher2.decryptKey(
            encryptedKey, encryptedData.getEncryptionMethod().getAlgorithm()
        );

    cipher.init(XMLCipher.DECRYPT_MODE, key);
    Document dd = cipher.doFinal(doc, ee);

    return dd;
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:42,代码来源:XMLEncryption11Test.java


示例3: encryptElement

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
/**
 * Encrypt element.
 * 
 * @param xmlDocument
 *            the xml document
 * @param encryptSymmetricKey
 *            the encrypt symmetric key
 * @param encryptedKey
 *            the encrypted key
 * @param element
 *            the element
 * @throws XMLEncryptionException
 *             the xML encryption exception
 * @throws Exception
 *             the exception
 */
void encryptElement(Document xmlDocument, Key encryptSymmetricKey,
		EncryptedKey encryptedKey, Element element)
		throws XMLEncryptionException, Exception {

	String algorithmURI = XMLCipher.AES_128;

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

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

	xmlCipher.doFinal(xmlDocument, element, true);
}
 
开发者ID:tlin-fei,项目名称:ds4p,代码行数:36,代码来源:DocumentEncrypterImpl.java


示例4: decryptUsingDOM

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
private Document decryptUsingDOM(
    Document document,
    Key keyWrappingKey
) throws Exception {
    NodeList nodeList =
        document.getElementsByTagNameNS(
            XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
            XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
        );
    Element ee = (Element)nodeList.item(0);

    // Need to pre-load the Encrypted Data so we can get the key info
    XMLCipher cipher = XMLCipher.getInstance();
    cipher.init(XMLCipher.DECRYPT_MODE, null);
    EncryptedData encryptedData = cipher.loadEncryptedData(document, ee);

    XMLCipher kwCipher = XMLCipher.getInstance();
    kwCipher.init(XMLCipher.UNWRAP_MODE, keyWrappingKey);
    KeyInfo ki = encryptedData.getKeyInfo();
    EncryptedKey encryptedKey = ki.itemEncryptedKey(0);
    Key symmetricKey =
        kwCipher.decryptKey(
            encryptedKey, encryptedData.getEncryptionMethod().getAlgorithm()
        );

    cipher.init(XMLCipher.DECRYPT_MODE, symmetricKey);
    return cipher.doFinal(document, ee);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:29,代码来源:KeyWrapEncryptionCreationTest.java


示例5: testMultipleKeyInfoElements

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
/**
 * See SANTUARIO-301:
 * https://issues.apache.org/jira/browse/SANTUARIO-301
 */
@org.junit.Test
public void testMultipleKeyInfoElements() throws Exception {
    if (!haveISOPadding) {
        log.warn("Test testMultipleKeyInfoElements skipped as necessary algorithms not available");
        return;
    }

    Document doc = db.parse(new ByteArrayInputStream(MULTIPLE_USER_DATA.getBytes("UTF8")));
    NodeList dataToEncrypt = doc.getElementsByTagName("user");

    XMLCipher dataCipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
    dataCipher.init(XMLCipher.ENCRYPT_MODE, secretKey);

    KeyInfo keyInfo = new KeyInfo(doc);
    keyInfo.addKeyName("mykey");

    EncryptedData encryptedData = dataCipher.getEncryptedData();
    encryptedData.setKeyInfo(keyInfo);

    for (int i = 0; i < dataToEncrypt.getLength(); i++) {
        dataCipher.doFinal(doc,(Element) dataToEncrypt.item(i), true);
    }

    NodeList keyInfoList = doc.getElementsByTagNameNS(Constants.SignatureSpecNS, "KeyInfo");
    assertEquals(keyInfoList.getLength(), 2);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:31,代码来源:EncryptContentTest.java


示例6: decryptElement

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
/**
 * Method decryptElement
 *
 * Take a key, encryption type and a file, find an encrypted element
 * decrypt it and return the resulting document
 *
 * @param filename File to decrypt from
 */
private Document decryptElement(String filename) throws Exception {
    XMLCipher cipher;

    // Parse the document in question

    String basedir = System.getProperty("basedir");
    if (basedir != null && !"".equals(basedir)) {
        filename = basedir + "/" + filename;
    }
    File f = new File(filename);

    DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
    Document doc = db.parse(new java.io.FileInputStream(f));

    // Now we have the document, lets build the XMLCipher element
    Element ee = null;

    // Create the XMLCipher element
    cipher = XMLCipher.getInstance();

    // Need to pre-load the Encrypted Data so we can get the key info
    ee = (Element) doc.getElementsByTagName("EncryptedData").item(0);
    cipher.init(XMLCipher.DECRYPT_MODE, null);
    EncryptedData encryptedData = cipher.loadEncryptedData(doc, ee);

    Key key = findKey(encryptedData);
    cipher.init(XMLCipher.DECRYPT_MODE, key);
    Document dd = cipher.doFinal(doc, ee);

    return dd;
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:40,代码来源:BaltimoreEncTest.java


示例7: decryptData

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
/**
 * Method decryptData
 *
 * Take a file, find an encrypted element decrypt it and return the
 * resulting byte array
 *
 * @param filename File to decrypt from
 */
private byte[] decryptData(String filename) throws Exception {

    XMLCipher cipher;

    // Parse the document in question
    String basedir = System.getProperty("basedir");
    if (basedir != null && !"".equals(basedir)) {
        filename = basedir + "/" + filename;
    }
    File f = new File(filename);

    DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
    Document doc = db.parse(new java.io.FileInputStream(f));

    // Now we have the document, lets build the XMLCipher element
    Element ee = null;

    // Create the XMLCipher element
    cipher = XMLCipher.getInstance();

    // Need to pre-load the Encrypted Data so we can get the key info
    ee = (Element) doc.getElementsByTagName("EncryptedData").item(0);
    cipher.init(XMLCipher.DECRYPT_MODE, null);
    EncryptedData encryptedData = cipher.loadEncryptedData(doc, ee);

    Key key = findKey(encryptedData);
    cipher.init(XMLCipher.DECRYPT_MODE, key);

    return cipher.decryptToByteArray(ee);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:39,代码来源:BaltimoreEncTest.java


示例8: decrypt

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
private Document decrypt(
    Document document,
    Key keyWrappingKey
) throws Exception {
    NodeList nodeList = document.getElementsByTagNameNS(
            XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
            XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
        );
    Element ee = (Element)nodeList.item(0);

    // Need to pre-load the Encrypted Data so we can get the key info
    XMLCipher cipher = XMLCipher.getInstance();
    cipher.init(XMLCipher.DECRYPT_MODE, null);
    EncryptedData encryptedData = cipher.loadEncryptedData(document, ee);

    XMLCipher kwCipher = XMLCipher.getInstance();
    kwCipher.init(XMLCipher.UNWRAP_MODE, keyWrappingKey);
    KeyInfo ki = encryptedData.getKeyInfo();
    EncryptedKey encryptedKey = ki.itemEncryptedKey(0);
    Key symmetricKey =
        kwCipher.decryptKey(
            encryptedKey, encryptedData.getEncryptionMethod().getAlgorithm()
        );

    cipher.init(XMLCipher.DECRYPT_MODE, symmetricKey);
    return cipher.doFinal(document, ee);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:28,代码来源:KeyWrapEncryptionAlgorithmTest.java


示例9: decryptElementDOM

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
/**
 * Decrypt using DOM API
 */
private Document decryptElementDOM(Document doc, Key rsaKey) throws Exception {

    // Create the XMLCipher element
    XMLCipher cipher = XMLCipher.getInstance();

    // Need to pre-load the Encrypted Data so we can get the key info
    Element ee =
            (Element) doc.getElementsByTagNameNS(
                    "http://www.w3.org/2001/04/xmlenc#", "EncryptedData"
            ).item(0);
    cipher.init(XMLCipher.DECRYPT_MODE, null);
    EncryptedData encryptedData = cipher.loadEncryptedData(doc, ee);

    KeyInfo ki = encryptedData.getKeyInfo();
    EncryptedKey encryptedKey = ki.itemEncryptedKey(0);

    XMLCipher cipher2 = XMLCipher.getInstance();
    cipher2.init(XMLCipher.UNWRAP_MODE, rsaKey);
    Key key =
            cipher2.decryptKey(
                    encryptedKey, encryptedData.getEncryptionMethod().getAlgorithm()
            );

    cipher.init(XMLCipher.DECRYPT_MODE, key);
    Document dd = cipher.doFinal(doc, ee);

    return dd;
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:32,代码来源:XMLEncryption11Test.java


示例10: testTripleDesElementCipher

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
@org.junit.Test
public void testTripleDesElementCipher() throws Exception {
    Document d = document(); // source
    Document ed = null;      // target
    Document dd = null;      // target
    Element e = (Element) d.getElementsByTagName(element()).item(index());
    Element ee = null;

    String source = null;
    String target = null;

    if (haveISOPadding) {
        source = toString(d);

        // prepare for encryption
        byte[] passPhrase = "24 Bytes per DESede key!".getBytes();
        DESedeKeySpec keySpec = new DESedeKeySpec(passPhrase);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyFactory.generateSecret(keySpec);

        // encrypt
        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
        cipher.init(XMLCipher.ENCRYPT_MODE, key);
        ed = cipher.doFinal(d, e);

        //decrypt
        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
        cipher.init(XMLCipher.DECRYPT_MODE, key);
        ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
        EncryptedData encryptedData = cipher.loadEncryptedData(ed, ee);
        String algorithm = encryptedData.getEncryptionMethod().getAlgorithm();
        assertEquals(XMLCipher.TRIPLEDES, algorithm);
        dd = cipher.doFinal(ed, ee);

        target = toString(dd);
        assertEquals(source, target);
    } else {
        log.warn(
            "Test testTripleDesElementCipher skipped as necessary algorithms not available"
        );
    }
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:43,代码来源:XMLCipherTest.java


示例11: testAes128ElementCipher

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
@org.junit.Test
public void testAes128ElementCipher() throws Exception {
    byte[] bits128 = {
                      (byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
                      (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
                      (byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x1B,
                      (byte) 0x1C, (byte) 0x1D, (byte) 0x1E, (byte) 0x1F};
    Key key = new SecretKeySpec(bits128, "AES");

    Document d = document(); // source
    Document ed = null;      // target
    Document dd = null;      // target
    Element e = (Element) d.getElementsByTagName(element()).item(index());
    Element ee = null;

    String source = null;
    String target = null;

    if (haveISOPadding) {
        source = toString(d);

        // encrypt
        cipher = XMLCipher.getInstance(XMLCipher.AES_128);
        cipher.init(XMLCipher.ENCRYPT_MODE, key);
        ed = cipher.doFinal(d, e);

        //decrypt
        cipher = XMLCipher.getInstance(XMLCipher.AES_128);
        cipher.init(XMLCipher.DECRYPT_MODE, key);
        ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
        EncryptedData encryptedData = cipher.loadEncryptedData(ed, ee);
        String algorithm = encryptedData.getEncryptionMethod().getAlgorithm();
        assertEquals(XMLCipher.AES_128, algorithm);
        dd = cipher.doFinal(ed, ee);

        target = toString(dd);
        assertEquals(source, target);
    } else {
        log.warn(
            "Test testAes128ElementCipher skipped as necessary algorithms not available"
        );
    }
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:44,代码来源:XMLCipherTest.java


示例12: testAes192ElementCipher

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
@org.junit.Test
public void testAes192ElementCipher() throws Exception {
    byte[] bits192 = {
                      (byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
                      (byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
                      (byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
                      (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
                      (byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x1B,
                      (byte) 0x1C, (byte) 0x1D, (byte) 0x1E, (byte) 0x1F};
    Key key = new SecretKeySpec(bits192, "AES");

    Document d = document(); // source
    Document ed = null;      // target
    Document dd = null;      // target
    Element e = (Element) d.getElementsByTagName(element()).item(index());
    Element ee = null;

    String source = null;
    String target = null;

    if (haveISOPadding) {
        source = toString(d);

        // encrypt
        cipher = XMLCipher.getInstance(XMLCipher.AES_192);
        cipher.init(XMLCipher.ENCRYPT_MODE, key);
        ed = cipher.doFinal(d, e);

        //decrypt
        cipher = XMLCipher.getInstance(XMLCipher.AES_192);
        cipher.init(XMLCipher.DECRYPT_MODE, key);
        ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
        EncryptedData encryptedData = cipher.loadEncryptedData(ed, ee);
        String algorithm = encryptedData.getEncryptionMethod().getAlgorithm();
        assertEquals(XMLCipher.AES_192, algorithm);
        dd = cipher.doFinal(ed, ee);

        target = toString(dd);
        assertEquals(source, target);
    } else {
        log.warn("Test testAes192ElementCipher skipped as necessary algorithms not available");
    }
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:44,代码来源:XMLCipherTest.java


示例13: testAes265ElementCipher

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
@org.junit.Test
public void testAes265ElementCipher() throws Exception {
    byte[] bits256 = {
                      (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
                      (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
                      (byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
                      (byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
                      (byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
                      (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
                      (byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x1B,
                      (byte) 0x1C, (byte) 0x1D, (byte) 0x1E, (byte) 0x1F};
    Key key = new SecretKeySpec(bits256, "AES");

    Document d = document(); // source
    Document ed = null;      // target
    Document dd = null;      // target
    Element e = (Element) d.getElementsByTagName(element()).item(index());
    Element ee = null;

    String source = null;
    String target = null;

    if (haveISOPadding) {
        source = toString(d);

        // encrypt
        cipher = XMLCipher.getInstance(XMLCipher.AES_256);
        cipher.init(XMLCipher.ENCRYPT_MODE, key);
        ed = cipher.doFinal(d, e);

        //decrypt
        cipher = XMLCipher.getInstance(XMLCipher.AES_256);
        cipher.init(XMLCipher.DECRYPT_MODE, key);
        ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
        EncryptedData encryptedData = cipher.loadEncryptedData(ed, ee);
        String algorithm = encryptedData.getEncryptionMethod().getAlgorithm();
        assertEquals(XMLCipher.AES_256, algorithm);
        dd = cipher.doFinal(ed, ee);

        target = toString(dd);
        assertEquals(source, target);
    } else {
        log.warn("Test testAes265ElementCipher skipped as necessary algorithms not available");
    }
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:46,代码来源:XMLCipherTest.java


示例14: testEncryptionProperties

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
@org.junit.Test
public void testEncryptionProperties() throws Exception {
    Document d = document(); // source
    Document ed = null;      // target
    Document dd = null;      // target
    Element e = d.getDocumentElement();
    Element ee = null;

    String source = null;
    String target = null;

    if (haveISOPadding) {
        source = toString(d);

        // prepare for encryption
        byte[] passPhrase = "24 Bytes per DESede key!".getBytes();
        DESedeKeySpec keySpec = new DESedeKeySpec(passPhrase);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyFactory.generateSecret(keySpec);

        // encrypt
        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
        cipher.init(XMLCipher.ENCRYPT_MODE, key);

        // Add EncryptionProperties
        Element elem = d.createElement("CustomInformation");
        elem.setTextContent("Some text content");

        EncryptionProperties eps = cipher.createEncryptionProperties();
        EncryptionProperty ep = cipher.createEncryptionProperty();
        ep.addEncryptionInformation(elem);
        ep.setId("_124124");
        ep.setTarget("http://localhost/");
        ep.setAttribute("xml:lang", "en");
        eps.addEncryptionProperty(ep);

        EncryptedData encData = cipher.getEncryptedData();
        encData.setEncryptionProperties(eps);

        ed = cipher.doFinal(d, e);
        // XMLUtils.outputDOM(ed, System.out);

        //decrypt
        cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
        cipher.init(XMLCipher.DECRYPT_MODE, key);
        ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
        dd = cipher.doFinal(ed, ee);

        target = toString(dd);
        assertEquals(source, target);
    } else {
        log.warn(
            "Test testTripleDesDocumentCipher skipped as "
            + "necessary algorithms not available"
        );
    }
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:58,代码来源:XMLCipherTest.java


示例15: testSameDocumentCipherReference

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
@org.junit.Test
public void testSameDocumentCipherReference() throws Exception {

    if (haveISOPadding) {
        DocumentBuilder db = XMLUtils.createDocumentBuilder(false);

        Document d = db.newDocument();

        Element docElement = d.createElement("EncryptedDoc");
        d.appendChild(docElement);

        // Create the XMLCipher object
        cipher = XMLCipher.getInstance();

        EncryptedData ed =
            cipher.createEncryptedData(CipherData.REFERENCE_TYPE,
                                       "#CipherTextId");
        EncryptionMethod em =
            cipher.createEncryptionMethod(XMLCipher.AES_128);

        ed.setEncryptionMethod(em);

        org.apache.xml.security.encryption.Transforms xencTransforms =
            cipher.createTransforms(d);
        ed.getCipherData().getCipherReference().setTransforms(xencTransforms);
        org.apache.xml.security.transforms.Transforms dsTransforms =
            xencTransforms.getDSTransforms();

        // An XPath transform
        XPathContainer xpc = new XPathContainer(d);
        xpc.setXPath("self::text()[parent::CipherText[@Id=\"CipherTextId\"]]");
        dsTransforms.addTransform(
            org.apache.xml.security.transforms.Transforms.TRANSFORM_XPATH,
            xpc.getElementPlusReturns()
        );

        // Add a Base64 Transforms
        dsTransforms.addTransform(
            org.apache.xml.security.transforms.Transforms.TRANSFORM_BASE64_DECODE
        );

        Element ee = cipher.martial(d, ed);

        docElement.appendChild(ee);

        // Add the cipher text
        Element encryptedElement = d.createElement("CipherText");
        encryptedElement.setAttributeNS(null, "Id", "CipherTextId");
        encryptedElement.setIdAttributeNS(null, "Id", true);
        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);

        assertEquals("A test encrypted secret",
                    new String(decryptBytes, "ASCII"));
    } else {
        log.warn(
            "Test testSameDocumentCipherReference skipped as "
            + "necessary algorithms not available"
        );
    }
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:70,代码来源:XMLCipherTest.java


示例16: testSerializedData

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
@org.junit.Test
public void testSerializedData() throws Exception {
    if (!haveISOPadding) {
        log.warn("Test testSerializedData skipped as necessary algorithms not available");
        return;
    }

    byte[] bits128 = {
                      (byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
                      (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17,
                      (byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x1B,
                      (byte) 0x1C, (byte) 0x1D, (byte) 0x1E, (byte) 0x1F};
    Key key = new SecretKeySpec(bits128, "AES");

    Document d = document(); // source
    Element e = (Element) d.getElementsByTagName(element()).item(index());

    // encrypt
    cipher = XMLCipher.getInstance(XMLCipher.AES_128);
    cipher.init(XMLCipher.ENCRYPT_MODE, key);

    // serialize element ...
    Canonicalizer canon =
        Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    canon.setWriter(baos);
    canon.notReset();
    canon.canonicalizeSubtree(e);
    baos.close();
    String before = baos.toString("UTF-8");

    byte[] serialized = baos.toByteArray();
    EncryptedData encryptedData =
        cipher.encryptData(
            d, EncryptionConstants.TYPE_ELEMENT, new ByteArrayInputStream(serialized)
        );

    //decrypt
    XMLCipher dcipher = XMLCipher.getInstance(XMLCipher.AES_128);
    dcipher.init(XMLCipher.DECRYPT_MODE, key);
    String algorithm = encryptedData.getEncryptionMethod().getAlgorithm();
    assertEquals(XMLCipher.AES_128, algorithm);
    byte[] bytes = dcipher.decryptToByteArray(dcipher.martial(encryptedData));
    String after = new String(bytes, "UTF-8");
    assertEquals(before, after);

    // test with null type
    encryptedData = cipher.encryptData(d, null, new ByteArrayInputStream(serialized));
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:50,代码来源:XMLCipherTest.java


示例17: findKey

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
/**
 * Method findKey
 *
 * Given an encryptedData structure, return the key that will decrypt
 * it
 *
 * @param encryptedData EncryptedData to get key for
 */
private Key findKey(EncryptedData encryptedData) throws Exception {
    KeyInfo ki = encryptedData.getKeyInfo();

    Key key = null;
    Key kek = null;

    if (ki == null) {
        return null;
    }

    // First check for a known key name
    KeyName keyName = ki.itemKeyName(0);
    if (keyName != null) {
        return mapKeyName(keyName.getKeyName());
    }

    // Decrypt any encryptedKey structures
    EncryptedKey encryptedKey = ki.itemEncryptedKey(0);
    if (encryptedKey == null) {
        return null;
    }

    KeyInfo kiek = encryptedKey.getKeyInfo();
    if (kiek == null) {
        return null;
    }

    KeyName kekKeyName = kiek.itemKeyName(0);
    if (kekKeyName != null) {
        kek = mapKeyName(kekKeyName.getKeyName());
    } else {
        X509Data certData = kiek.itemX509Data(0);
        XMLX509Certificate xcert = certData.itemCertificate(0);
        X509Certificate cert = xcert.getX509Certificate();

        if (cert != null && cert.getSerialNumber().toString().equals(rsaCertSerialNumber)) {
            kek = rsaKey;
        }
    }
    if (kek != null) {
        XMLCipher cipher = XMLCipher.getInstance();
        cipher.init(XMLCipher.UNWRAP_MODE, kek);
        key =
            cipher.decryptKey(
                encryptedKey, encryptedData.getEncryptionMethod().getAlgorithm()
            );
    }

    return key;
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:59,代码来源:BaltimoreEncTest.java


示例18: main

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的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


示例19: doEncryption

import org.apache.xml.security.encryption.EncryptedData; //导入依赖的package包/类
private Vector doEncryption(Document doc,
                            SecretKey encryptKey,
                            KeyInfo keyInfo)
        throws WSSecurityException {
    /*
     * First step: set the encryption encoding namespace in the SOAP:Envelope
     */
    Element envelope = doc.getDocumentElement();
    envelope.setAttributeNS(WSConstants.XMLNS_NS,
            "xmlns:" + WSConstants.ENC_PREFIX,
            WSConstants.ENC_NS);

    SOAPConstants soapConstants = WSSecurityUtil.getSOAPConstants(envelope);

    XMLCipher xmlCipher = null;
    try {
        xmlCipher = XMLCipher.getInstance(symEncAlgo);
    } catch (XMLEncryptionException e3) {
        throw new WSSecurityException(WSSecurityException.UNSUPPORTED_ALGORITHM, null, null, e3);
    }

    // if no encryption parts set - use the default
    if (parts == null) {
        parts = new Vector();
        WSEncryptionPart encP =
                new WSEncryptionPart(soapConstants.getBodyQName().getLocalPart(),
                        soapConstants.getEnvelopeURI(),
                        "Content");
        parts.add(encP);
    }

    Vector encDataRefs = new Vector();

    for (int part = 0; part < parts.size(); part++) {
        WSEncryptionPart encPart = (WSEncryptionPart) parts.get(part);
        String elemName = encPart.getName();
        String nmSpace = encPart.getNamespace();
        String modifier = encPart.getEncModifier();
        /*
         * Third step: get the data to encrypt.
         */
        Element body =
                (Element) WSSecurityUtil.findElement(envelope,
                        elemName,
                        nmSpace);
        if (body == null) {
            throw new WSSecurityException(WSSecurityException.FAILURE,
                    "noEncElement",
                    new Object[]{"{" + nmSpace + "}" + elemName});
        }

        boolean content = modifier.equals("Content") ? true : false;
        String xencEncryptedDataId = wssConfig.getIdAllocator().createId("EncDataId-", body);

        /*
         * Forth step: encrypt data, and set neccessary attributes in
         * xenc:EncryptedData
         */
        try {
            xmlCipher.init(XMLCipher.ENCRYPT_MODE, encryptKey);
            EncryptedData encData = xmlCipher.getEncryptedData();
            encData.setId(xencEncryptedDataId);
            encData.setKeyInfo(keyInfo);
            xmlCipher.doFinal(doc, body, content);
        } catch (Exception e2) {
            throw new WSSecurityException(WSSecurityException.FAILED_ENCRYPTION, null, null, e2);
        }
        encDataRefs.add(new String("#" + xencEncryptedDataId));
    }
    return encDataRefs;
}
 
开发者ID:wso2,项目名称:wso2-wss4j,代码行数:72,代码来源:WSEncryptBody.java



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java DOMStreamReader类代码示例发布时间:2022-05-23
下一篇:
Java RuntimeModelerException类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap