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


Java EncryptedData.setKeyInfo方法代码示例

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


在下文中一共展示了EncryptedData.setKeyInfo方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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

示例3: 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

示例4: 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

示例5: 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.setKeyInfo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。