本文整理汇总了Java中org.apache.xml.security.encryption.XMLCipher.doFinal方法的典型用法代码示例。如果您正苦于以下问题:Java XMLCipher.doFinal方法的具体用法?Java XMLCipher.doFinal怎么用?Java XMLCipher.doFinal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.xml.security.encryption.XMLCipher
的用法示例。
在下文中一共展示了XMLCipher.doFinal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
示例2: decryptUsingDOM
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
/**
* Decrypt the document using DOM API and run some tests on the decrypted Document.
*/
private Document decryptUsingDOM(
String algorithm,
SecretKey secretKey,
Key wrappingKey,
Document document
) throws Exception {
XMLCipher cipher = XMLCipher.getInstance(algorithm);
cipher.init(XMLCipher.DECRYPT_MODE, secretKey);
if (wrappingKey != null) {
cipher.setKEK(wrappingKey);
}
NodeList nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Element ee = (Element) nodeList.item(0);
return cipher.doFinal(document, ee);
}
示例3: decryptUsingDOM
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
/**
* Decrypt the document using DOM API and run some tests on the decrypted Document.
*/
private Document decryptUsingDOM(
String algorithm,
SecretKey secretKey,
Key wrappingKey,
Document document
) throws Exception {
XMLCipher cipher = XMLCipher.getInstance(algorithm);
cipher.init(XMLCipher.DECRYPT_MODE, secretKey);
if (wrappingKey != null) {
cipher.setKEK(wrappingKey);
}
NodeList nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Element ee = (Element)nodeList.item(0);
return cipher.doFinal(document, ee);
}
示例4: 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);
}
示例5: decryptElement
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的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;
}
示例6: decryptDocument
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
private void decryptDocument(Document docSource, KeyResolverSpi internalResolver) throws Exception
{
Document document = (Document)docSource.cloneNode(true);
Element rootElement = document.getDocumentElement();
Element encryptedDataElement = (Element)rootElement.getFirstChild();
XMLCipher decryptCipher = XMLCipher.getInstance();
decryptCipher.init(XMLCipher.DECRYPT_MODE, null);
if (internalResolver != null) {
decryptCipher.registerInternalKeyResolver(internalResolver);
}
decryptCipher.doFinal(document, encryptedDataElement);
Element decryptedElement = (Element) rootElement.getFirstChild();
assertEquals("elem", decryptedElement.getLocalName());
}
示例7: doDOMEncryptionOutbound
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
protected void doDOMEncryptionOutbound(File file, int tagCount) throws Exception {
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(file);
XMLCipher cipher = XMLCipher.getInstance("http://www.w3.org/2001/04/xmlenc#aes256-cbc");
cipher.init(XMLCipher.ENCRYPT_MODE, encryptionSymKey);
document = cipher.doFinal(document, document.getDocumentElement());
XMLUtils.outputDOM(document, new BufferedOutputStream(new FileOutputStream(new File(getTmpFilePath(), "encryption-dom-" + tagCount + ".xml"))));
}
示例8: doDOMDecryptionInbound
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
protected void doDOMDecryptionInbound(File file, int tagCount) throws Exception {
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(file);
XMLCipher cipher = XMLCipher.getInstance("http://www.w3.org/2001/04/xmlenc#aes256-cbc");
cipher.init(XMLCipher.DECRYPT_MODE, encryptionSymKey);
cipher.doFinal(document, document.getDocumentElement());
}
示例9: decryptUsingDOM
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的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);
}
示例10: 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);
}
示例11: decryptElement
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的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;
}
示例12: decrypt
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的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);
}
示例13: encrypt
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
private void encrypt(
String algorithm,
Document document,
List<String> localNames,
Key encryptingKey
) throws Exception {
XMLCipher cipher = XMLCipher.getInstance(algorithm);
cipher.init(XMLCipher.ENCRYPT_MODE, encryptingKey);
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
xpath.setNamespaceContext(new DSNamespaceContext());
for (String localName : localNames) {
String expression = "//*[local-name()='" + localName + "']";
Element elementToEncrypt =
(Element) xpath.evaluate(expression, document, XPathConstants.NODE);
Assert.assertNotNull(elementToEncrypt);
document = cipher.doFinal(document, elementToEncrypt, false);
}
NodeList nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Assert.assertTrue(nodeList.getLength() > 0);
}
示例14: decrypt
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
private Document decrypt(
String algorithm,
Document document,
Key key
) throws Exception {
XMLCipher cipher = XMLCipher.getInstance(algorithm);
cipher.init(XMLCipher.DECRYPT_MODE, key);
NodeList nodeList = document.getElementsByTagNameNS(
XMLSecurityConstants.TAG_xenc_EncryptedData.getNamespaceURI(),
XMLSecurityConstants.TAG_xenc_EncryptedData.getLocalPart()
);
Element ee = (Element)nodeList.item(0);
return cipher.doFinal(document, ee);
}
示例15: main
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
public static void main(String unused[]) throws Exception {
Document document = loadEncryptionDocument();
Element encryptedDataElement =
(Element) document.getElementsByTagNameNS(
EncryptionConstants.EncryptionSpecNS,
EncryptionConstants._TAG_ENCRYPTEDDATA).item(0);
/*
* Load the key to be used for decrypting the xml data
* encryption key.
*/
Key kek = loadKeyEncryptionKey();
String providerName = "BC";
XMLCipher xmlCipher =
XMLCipher.getInstance();
/*
* The key to be used for decrypting xml data would be obtained
* from the keyinfo of the EncrypteData using the kek.
*/
xmlCipher.init(XMLCipher.DECRYPT_MODE, null);
xmlCipher.setKEK(kek);
/*
* The following doFinal call replaces the encrypted data with
* decrypted contents in the document.
*/
xmlCipher.doFinal(document, encryptedDataElement);
outputDocToFile(document, "build/decryptedInfo.xml");
}