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


Java XMLCipher.getEncryptedData方法代码示例

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


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

示例1: encryptElement

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的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: testEecryptToByteArray

import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@org.junit.Test
public void testEecryptToByteArray() throws Exception {
    if (!bcInstalled) {
        return;
    }
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);
    Key key = keygen.generateKey();

    Document document = document();

    XMLCipher cipher = XMLCipher.getInstance(XMLCipher.AES_128_GCM);
    cipher.init(XMLCipher.ENCRYPT_MODE, key);
    cipher.getEncryptedData();

    Document encrypted = cipher.doFinal(document, document);

    XMLCipher xmlCipher = XMLCipher.getInstance();
    xmlCipher.init(XMLCipher.DECRYPT_MODE, key);
    Element encryptedData = (Element) encrypted.getElementsByTagNameNS(EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_ENCRYPTEDDATA).item(0);

    xmlCipher.decryptToByteArray(encryptedData);
}
 
开发者ID:Legostaev,项目名称:xmlsec-gost,代码行数:24,代码来源:XMLCipherTest.java

示例3: testMultipleKeyInfoElements

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