org.apache.poi.hssf.record.crypto.Biff8EncryptionKey Java Examples

The following examples show how to use org.apache.poi.hssf.record.crypto.Biff8EncryptionKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecordFactoryInputStream.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public RecordInputStream createDecryptingStream(InputStream original) {
	FilePassRecord fpr = _filePassRec;
	String userPassword = Biff8EncryptionKey.getCurrentUserPassword();
	if (userPassword == null) {
	    userPassword = Decryptor.DEFAULT_PASSWORD;
	}

	EncryptionInfo info = fpr.getEncryptionInfo();
          try {
              if (!info.getDecryptor().verifyPassword(userPassword)) {
                  throw new EncryptedDocumentException(
                          (Decryptor.DEFAULT_PASSWORD.equals(userPassword) ? "Default" : "Supplied")
                          + " password is invalid for salt/verifier/verifierHash");
              }
          } catch (GeneralSecurityException e) {
              throw new EncryptedDocumentException(e);
          }

	return new RecordInputStream(original, info, _initialRecordsSize);
}
 
Example #2
Source File: WriteContextImpl.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
private void clearEncrypt03() {
    if (StringUtils.isEmpty(writeWorkbookHolder.getPassword())
        || !ExcelTypeEnum.XLS.equals(writeWorkbookHolder.getExcelType())) {
        return;
    }
    Biff8EncryptionKey.setCurrentUserPassword(null);
}
 
Example #3
Source File: ExcelAnalyserImpl.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
private void clearEncrypt03() {
    if (StringUtils.isEmpty(analysisContext.readWorkbookHolder().getPassword())
        || !ExcelTypeEnum.XLS.equals(analysisContext.readWorkbookHolder().getExcelType())) {
        return;
    }
    Biff8EncryptionKey.setCurrentUserPassword(null);
}
 
Example #4
Source File: Poi3Test.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
@Test
public void Encryption2() throws Exception {
    Biff8EncryptionKey.setCurrentUserPassword("123456");
    POIFSFileSystem fs = new POIFSFileSystem(new File("d:/test/simple03.xls"), true);
    HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true);
    Biff8EncryptionKey.setCurrentUserPassword(null);
    System.out.println(hwb.getSheetAt(0).getSheetName());

}
 
Example #5
Source File: SlideShowFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a SlideShow from the given NPOIFSFileSystem, which may
 * be password protected
 *
 * @param fs The {@link NPOIFSFileSystem} to read the document from
 * @param password The password that should be used or null if no password is necessary.
 *
 * @return The created SlideShow
 *
 * @throws IOException if an error occurs while reading the data
 */
public static SlideShow<?,?> create(final NPOIFSFileSystem fs, String password) throws IOException {
    DirectoryNode root = fs.getRoot();

    // Encrypted OOXML files go inside OLE2 containers, is this one?
    if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
        InputStream stream = null;
        try {
            stream = DocumentFactoryHelper.getDecryptedStream(fs, password);

            return createXSLFSlideShow(stream);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

    // If we get here, it isn't an encrypted PPTX file
    // So, treat it as a regular HSLF PPT one
    boolean passwordSet = false;
    if (password != null) {
        Biff8EncryptionKey.setCurrentUserPassword(password);
        passwordSet = true;
    }
    try {
        return createHSLFSlideShow(fs);
    } finally {
        if (passwordSet) {
            Biff8EncryptionKey.setCurrentUserPassword(null);
        }
    }
}
 
Example #6
Source File: HSSFWorkbook.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void updateEncryptionInfo() {
    // make sure, that we've read all the streams ...
    readProperties();
    FilePassRecord fpr = (FilePassRecord)workbook.findFirstRecordBySid(FilePassRecord.sid);

    String password = Biff8EncryptionKey.getCurrentUserPassword();
    WorkbookRecordList wrl = workbook.getWorkbookRecordList();
    if (password == null) {
        if (fpr != null) {
            // need to remove password data
            wrl.remove(fpr);
        }
    } else {
        // create password record
        if (fpr == null) {
            fpr = new FilePassRecord(EncryptionMode.cryptoAPI);
            wrl.add(1, fpr);
        }

        // check if the password has been changed
        EncryptionInfo ei = fpr.getEncryptionInfo();
        EncryptionVerifier ver = ei.getVerifier();
        byte encVer[] = ver.getEncryptedVerifier();
        Decryptor dec = ei.getDecryptor();
        Encryptor enc = ei.getEncryptor();
        try {
            if (encVer == null || !dec.verifyPassword(password)) {
                enc.confirmPassword(password);
            } else {
                byte verifier[] = dec.getVerifier();
                byte salt[] = ver.getSalt();
                enc.confirmPassword(password, null, null, verifier, salt, null);
            }
        } catch (GeneralSecurityException e) {
            throw new EncryptedDocumentException("can't validate/update encryption setting", e);
        }
    }
}
 
Example #7
Source File: MSExcelWriter.java    From hadoopoffice with Apache License 2.0 5 votes vote down vote up
private void finalizeWriteEncryptedHSSF() throws IOException {
	LOG.debug("encrypting HSSFWorkbook");
	Biff8EncryptionKey.setCurrentUserPassword(this.howc.getPassword());
	try {
		this.currentWorkbook.write(this.oStream);
	} finally {
		Biff8EncryptionKey.setCurrentUserPassword(null);
		if (this.oStream!=null) {
			this.oStream.close();
		}
	}
}
 
Example #8
Source File: SecurityUtil.java    From SQLiteToExcel with Apache License 2.0 5 votes vote down vote up
/**
 * Encrypt a file, support .xls file only
 *
 * @param file
 * @param encryptKey
 * @throws Exception
 */
public static void EncryptFile(File file, String encryptKey) throws Exception {
    FileInputStream fileInput = new FileInputStream(file.getPath());
    BufferedInputStream bufferInput = new BufferedInputStream(fileInput);
    POIFSFileSystem poiFileSystem = new POIFSFileSystem(bufferInput);
    Biff8EncryptionKey.setCurrentUserPassword(encryptKey);
    HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem, true);
    FileOutputStream fileOut = new FileOutputStream(file.getPath());
    workbook.writeProtectWorkbook(Biff8EncryptionKey.getCurrentUserPassword(), "");
    workbook.write(fileOut);
    bufferInput.close();
    fileOut.close();
}