1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
Document document = null;
try {
document = readDocument(file.getFileName());
} catch (Exception e) {
//gestion de l'exception
return;
}
/*
* Get a key to be used for encrypting the element. Here we are generating an AES key.
*/
Key symmetricKey = null;
try {
symmetricKey = GenerateDataEncryptionKey();
} catch (Exception e) {
//gestion de l'exception
return;
}
try {
String algorithmURI = XMLCipher.RSA_v1dot5;
XMLCipher keyCipher = XMLCipher.getInstance(algorithmURI);
keyCipher.init(XMLCipher.WRAP_MODE, publicKey);
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);
xmlCipher.doFinal(document, rootElement, true);
} catch (Exception e) {
//gestion de l'excption
return;
} |
Partager