use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.
the class TestXWPFBugs method bug53475NoCSPName.
/**
* A word document that's encrypted with non-standard
* Encryption options, and no cspname section. See bug 53475
*/
@Test
public void bug53475NoCSPName() throws Exception {
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx");
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
// Check the encryption details
EncryptionInfo info = new EncryptionInfo(filesystem);
assertEquals(128, info.getHeader().getKeySize());
assertEquals(CipherAlgorithm.aes128, info.getHeader().getCipherAlgorithm());
assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithmEx());
// Check it can be decoded
Decryptor d = Decryptor.getInstance(info);
assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell"));
// Check we can read the word document in that
InputStream dataStream = d.getDataStream(filesystem);
OPCPackage opc = OPCPackage.open(dataStream);
XWPFDocument doc = new XWPFDocument(opc);
XWPFWordExtractor ex = new XWPFWordExtractor(doc);
String text = ex.getText();
assertNotNull(text);
assertEquals("This is password protected Word document.", text.trim());
ex.close();
filesystem.close();
}
use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.
the class TestXWPFBugs method bug53475_aes256.
/**
* A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),
* but the key can be 128/192/256 bits
*/
@Test
public void bug53475_aes256() throws Exception {
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);
// Check the encryption details
EncryptionInfo info = new EncryptionInfo(filesystem);
assertEquals(16, info.getHeader().getBlockSize());
assertEquals(256, info.getHeader().getKeySize());
assertEquals(CipherAlgorithm.aes256, info.getHeader().getCipherAlgorithm());
assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithmEx());
// Check it can be decoded
Decryptor d = Decryptor.getInstance(info);
assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass"));
// Check we can read the word document in that
InputStream dataStream = d.getDataStream(filesystem);
OPCPackage opc = OPCPackage.open(dataStream);
XWPFDocument doc = new XWPFDocument(opc);
XWPFWordExtractor ex = new XWPFWordExtractor(doc);
String text = ex.getText();
assertNotNull(text);
// I know ... a stupid typo, maybe next time ...
assertEquals("The is a password protected document.", text.trim());
ex.close();
filesystem.close();
}
use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.
the class TestBinaryRC4 method createKeyDigest.
@Test
public void createKeyDigest() throws GeneralSecurityException {
byte[] docIdData = readFromString("17 F6 D1 6B 09 B1 5F 7B 4C 9D 03 B4 81 B5 B4 4A");
byte[] expResult = readFromString("C2 D9 56 B2 6B");
EncryptionInfo ei = new EncryptionInfo(EncryptionMode.binaryRC4);
BinaryRC4EncryptionVerifier ver = (BinaryRC4EncryptionVerifier) ei.getVerifier();
ver.setSalt(docIdData);
SecretKey sk = BinaryRC4Decryptor.generateSecretKey("MoneyForNothing", ver);
assertArrayEquals("keyDigest mismatch", expResult, sk.getEncoded());
}
use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.
the class TestWorkbookProtection method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws Exception {
final String password = "abc123";
final String sheetName = "TestSheet1";
final String cellValue = "customZipEntrySource";
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet1 = workbook.createSheet(sheetName);
XSSFRow row1 = sheet1.createRow(1);
XSSFCell cell1 = row1.createCell(1);
cell1.setCellValue(cellValue);
File tf1 = TempFile.createTempFile("poitest", ".xlsx");
FileOutputStream fos1 = new FileOutputStream(tf1);
workbook.write(fos1);
IOUtils.closeQuietly(fos1);
POIFSFileSystem poiFileSystem = new POIFSFileSystem();
EncryptionInfo encryptionInfo = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = encryptionInfo.getEncryptor();
enc.confirmPassword(password);
FileInputStream fis = new FileInputStream(tf1);
OPCPackage opc = OPCPackage.open(fis);
IOUtils.closeQuietly(fis);
try {
OutputStream os = enc.getDataStream(poiFileSystem);
opc.save(os);
IOUtils.closeQuietly(os);
} finally {
IOUtils.closeQuietly(opc);
}
tf1.delete();
FileOutputStream fos2 = new FileOutputStream(tf1);
poiFileSystem.writeFilesystem(fos2);
IOUtils.closeQuietly(fos2);
workbook.close();
fis = new FileInputStream(tf1);
POIFSFileSystem poiFileSystem2 = new POIFSFileSystem(fis);
IOUtils.closeQuietly(fis);
EncryptionInfo encryptionInfo2 = new EncryptionInfo(poiFileSystem2);
Decryptor decryptor = encryptionInfo2.getDecryptor();
decryptor.verifyPassword(password);
XSSFWorkbook workbook2 = new XSSFWorkbook(decryptor.getDataStream(poiFileSystem2));
workbook2.close();
tf1.delete();
}
use of org.apache.poi.poifs.crypt.EncryptionInfo in project poi by apache.
the class BinaryRC4Encryptor method createEncryptionInfoEntry.
protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException {
DataSpaceMapUtils.addDefaultDataSpace(dir);
final EncryptionInfo info = getEncryptionInfo();
final BinaryRC4EncryptionHeader header = (BinaryRC4EncryptionHeader) info.getHeader();
final BinaryRC4EncryptionVerifier verifier = (BinaryRC4EncryptionVerifier) info.getVerifier();
EncryptionRecord er = new EncryptionRecord() {
@Override
public void write(LittleEndianByteArrayOutputStream bos) {
bos.writeShort(info.getVersionMajor());
bos.writeShort(info.getVersionMinor());
header.write(bos);
verifier.write(bos);
}
};
DataSpaceMapUtils.createEncryptionEntry(dir, "EncryptionInfo", er);
}
Aggregations