Java源码示例:org.apache.poi.hssf.record.crypto.Biff8EncryptionKey

示例1
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);
}
 
示例2
private void clearEncrypt03() {
    if (StringUtils.isEmpty(writeWorkbookHolder.getPassword())
        || !ExcelTypeEnum.XLS.equals(writeWorkbookHolder.getExcelType())) {
        return;
    }
    Biff8EncryptionKey.setCurrentUserPassword(null);
}
 
示例3
private void clearEncrypt03() {
    if (StringUtils.isEmpty(analysisContext.readWorkbookHolder().getPassword())
        || !ExcelTypeEnum.XLS.equals(analysisContext.readWorkbookHolder().getExcelType())) {
        return;
    }
    Biff8EncryptionKey.setCurrentUserPassword(null);
}
 
示例4
@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());

}
 
示例5
/**
 * 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);
        }
    }
}
 
示例6
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);
        }
    }
}
 
示例7
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();
		}
	}
}
 
示例8
/**
 * 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();
}