本文整理汇总了Java中org.apache.xml.security.encryption.EncryptedData类的典型用法代码示例。如果您正苦于以下问题:Java EncryptedData类的具体用法?Java EncryptedData怎么用?Java EncryptedData使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EncryptedData类属于org.apache.xml.security.encryption包,在下文中一共展示了EncryptedData类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
示例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;
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
示例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"
);
}
}
示例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"
);
}
}
示例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");
}
}
示例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");
}
}
示例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"
);
}
}
示例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"
);
}
}