本文整理汇总了Java中org.apache.xml.security.encryption.XMLCipher.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java XMLCipher.getInstance方法的具体用法?Java XMLCipher.getInstance怎么用?Java XMLCipher.getInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.xml.security.encryption.XMLCipher
的用法示例。
在下文中一共展示了XMLCipher.getInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testEncryptedKeyWithRecipient
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@org.junit.Test
public void testEncryptedKeyWithRecipient() throws Exception {
String filename =
"src/test/resources/org/apache/xml/security/encryption/encryptedKey.xml";
if (basedir != null && !"".equals(basedir)) {
filename = basedir + "/" + filename;
}
File f = new File(filename);
DocumentBuilder builder = XMLUtils.createDocumentBuilder(false);
Document document = builder.parse(f);
XMLCipher keyCipher = XMLCipher.getInstance();
keyCipher.init(XMLCipher.UNWRAP_MODE, null);
NodeList ekList =
document.getElementsByTagNameNS(
EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_ENCRYPTEDKEY
);
for (int i = 0; i < ekList.getLength(); i++) {
EncryptedKey ek =
keyCipher.loadEncryptedKey(document, (Element) ekList.item(i));
assertNotNull(ek.getRecipient());
}
}
示例2: 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());
}
}
示例3: encryptSymmetric
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
private void encryptSymmetric(Exchange exchange, Document document, OutputStream stream) throws Exception {
Key keyEncryptionKey;
Key dataEncryptionKey;
if (xmlCipherAlgorithm.equals(XMLCipher.TRIPLEDES)) {
keyEncryptionKey = generateKeyEncryptionKey("DESede");
dataEncryptionKey = generateDataEncryptionKey();
} else if (xmlCipherAlgorithm.equals(XMLCipher.SEED_128)) {
keyEncryptionKey = generateKeyEncryptionKey("SEED");
dataEncryptionKey = generateDataEncryptionKey();
} else if (xmlCipherAlgorithm.contains("camellia")) {
keyEncryptionKey = generateKeyEncryptionKey("CAMELLIA");
dataEncryptionKey = generateDataEncryptionKey();
} else {
keyEncryptionKey = generateKeyEncryptionKey("AES");
dataEncryptionKey = generateDataEncryptionKey();
}
XMLCipher keyCipher = XMLCipher.getInstance(generateXmlCipherAlgorithmKeyWrap());
keyCipher.init(XMLCipher.WRAP_MODE, keyEncryptionKey);
encrypt(exchange, document, stream, dataEncryptionKey, keyCipher, keyEncryptionKey);
}
示例4: itemEncryptedKey
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
/**
* Method itemEncryptedKey
*
* @param i
* @return the asked EncryptedKey element, null if the index is too big
* @throws XMLSecurityException
*/
public EncryptedKey itemEncryptedKey(int i) throws XMLSecurityException {
if (encryptedKeys != null) {
return encryptedKeys.get(i);
}
Element e =
XMLUtils.selectXencNode(
getFirstChild(), EncryptionConstants._TAG_ENCRYPTEDKEY, i);
if (e != null) {
XMLCipher cipher = XMLCipher.getInstance();
cipher.init(XMLCipher.UNWRAP_MODE, null);
return cipher.loadEncryptedKey(e);
}
return null;
}
示例5: 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);
}
示例6: 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);
}
示例7: 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);
}
示例8: 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);
}
示例9: 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"))));
}
示例10: encrypt
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
private void encrypt(Exchange exchange, Document document, OutputStream stream, Key dataEncryptionKey,
XMLCipher keyCipher, Key keyEncryptionKey) throws Exception {
XMLCipher xmlCipher = XMLCipher.getInstance(xmlCipherAlgorithm);
xmlCipher.init(XMLCipher.ENCRYPT_MODE, dataEncryptionKey);
if (secureTag.equalsIgnoreCase("")) {
embedKeyInfoInEncryptedData(document, keyCipher, xmlCipher, dataEncryptionKey, keyEncryptionKey);
document = xmlCipher.doFinal(document, document.getDocumentElement());
} else {
XPathBuilder xpathBuilder = new XPathBuilder(secureTag);
xpathBuilder.setNamespaceContext(getNamespaceContext());
NodeList nodeList = xpathBuilder.evaluate(exchange, NodeList.class);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
document = node.getOwnerDocument();
embedKeyInfoInEncryptedData(node.getOwnerDocument(), keyCipher, xmlCipher,
dataEncryptionKey, keyEncryptionKey);
Document temp = xmlCipher.doFinal(node.getOwnerDocument(), (Element) node, getSecureTagContents());
document.importNode(temp.getDocumentElement().cloneNode(true), true);
}
}
try {
DOMSource source = new DOMSource(document);
InputStream sis = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, source);
IOHelper.copy(sis, stream);
} finally {
stream.close();
}
}
示例11: 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);
}
示例12: encryptAsymmetric
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
/**
* Configure the public key for the asymmetric key wrap algorithm, create the key cipher, and delegate
* to common encryption method.
*
* The method first checks the exchange for a declared key alias, and will fall back to the
* statically-defined instance variable if no value is found in the exchange. This allows different
* aliases / keys to be used for multiple-recipient messaging integration patterns such as CBR
* or recipient list.
*/
private void encryptAsymmetric(Exchange exchange, Document document, OutputStream stream) throws Exception {
String exchangeRecipientAlias = getRecipientKeyAlias(exchange);
if (null == exchangeRecipientAlias) {
throw new IllegalStateException("The recipient's key alias must be defined for asymmetric key encryption.");
}
if (trustStore == null && null != this.keyOrTrustStoreParameters) {
trustStore = keyOrTrustStoreParameters.createKeyStore();
trustStorePassword = keyOrTrustStoreParameters.getPassword();
}
if (null == trustStore) {
throw new IllegalStateException("A trust store must be defined for asymmetric key encryption.");
}
String password =
this.keyPassword != null ? this.keyPassword : this.trustStorePassword;
Key keyEncryptionKey = getPublicKey(this.trustStore, exchangeRecipientAlias, password);
if (null == keyEncryptionKey) {
throw new IllegalStateException("No key for the alias [ " + exchangeRecipientAlias
+ " ] exists in " + "the configured trust store.");
}
Key dataEncryptionKey = generateDataEncryptionKey();
XMLCipher keyCipher;
if (null != this.getKeyCipherAlgorithm()) {
keyCipher = XMLCipher.getInstance(this.getKeyCipherAlgorithm(), null, digestAlgorithm);
} else {
keyCipher = XMLCipher.getInstance(XMLCipher.RSA_OAEP, null, digestAlgorithm);
}
keyCipher.init(XMLCipher.WRAP_MODE, keyEncryptionKey);
encrypt(exchange, document, stream, dataEncryptionKey, keyCipher, keyEncryptionKey);
}
示例13: testCamellia192KW
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@org.junit.Test
public void testCamellia192KW() throws Exception {
if (!bcInstalled) {
return;
}
// 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("Camellia");
keygen.init(192);
SecretKey key = keygen.generateKey();
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.CAMELLIA_192_KeyWrap);
keygen = KeyGenerator.getInstance("Camellia");
keygen.init(192);
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.CAMELLIA_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);
document = decrypt(document, keyWrappingKey);
// Check the CreditCard decrypted ok
nodeList = document.getElementsByTagNameNS("urn:example:po", "CreditCard");
Assert.assertEquals(nodeList.getLength(), 1);
}
示例14: testSEED128KW
import org.apache.xml.security.encryption.XMLCipher; //导入方法依赖的package包/类
@org.junit.Test
public void testSEED128KW() throws Exception {
if (!bcInstalled) {
return;
}
// 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("SEED");
keygen.init(128);
SecretKey key = keygen.generateKey();
// Set up the Key Wrapping Key
XMLCipher cipher = XMLCipher.getInstance(XMLCipher.SEED_128_KeyWrap);
keygen = KeyGenerator.getInstance("SEED");
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.SEED_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);
}
示例15: 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);
}