I am implementing AES Algorithm 128 bit key. After encryption, the first 16 bytes of encrypted data will be stored in a .docx file. After that the .docx file will be blocked.
XWPFDocument document = new XWPFDocument() ;
FileOutputStream out = new FileOutputStream(filename,true);//filename is .docx word document
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(ress1);//ress1 is a String datatype
document.write(out);
As per what I understand from your comment, you want to encrypt your word file. You can achieve that using following code snippet:
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword(<your_password>);
OPCPackage opc = OPCPackage.open(new File(<file_path>), PackageAccess.READ_WRITE); //opening package for encryption
OutputStream os = enc.getDataStream(fs); //perform encryption
opc.save(os); //save package
opc.close();
FileOutputStream fos = new FileOutputStream("file_path");
fs.writeFilesystem(fos); //write the file back to file system
fos.close();
Related
I have an InputStream. I am trying to read the InputStream and write it to a pdf file using iText.
I tried the following :
FileOutputStream fileout = new FileOutputStream(file);
Document document = new Document();
PdfWriter.getInstance(document, fileout);
InputStream is=null;
is = myInfo.getInputStream();
String result = IOUtils.toString(is, "UTF-8");
Paragraph paragraph=new Paragraph();
paragraph.add(result);
document.add(paragraph);
is.close();
document.close();
Here the output file contains so many unwanted characters, some XML tags etc. The output pdf file is dumped with a lot of things which are non-readable.
And I am able to write it to pdf file using the following code:
OutputStream ostream = new FileOutputStream("c:\\test\\newfile1.pdf");
byte[] data = new byte[4096];
int r = 0;
while((r = is.read(data, 0, data.length)) != -1)
{
ostream.write(data, 0, r);
}
ostream.flush();
ostream.close();
The above code helps me to write the inputstream to the pdf file. But I want to do the same thing with itext.
I am using iText for the first time and confused how to use it properly. Could someone please help me with this? Thanks.
Solution :
I changed the inputstream to bytearraay.
PdfReader pdfreader;
pdfreader = new PdfReader(myInfo.getByteArray());
PdfStamper pdfStamper = new PdfStamper(pdfreader, fileout);
pdfStamper.close();
pdfreader.close();
In this way, I am able to read the inputsteam and write to pdf using itext. Thanks everyone.
I want to protect a .doc file with password using apache poi. I am getting this error while running code. please help me
Exception in thread "main"
org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The
supplied data appears to be in the OLE2 Format. You are calling the
part of POI that deals with OOXML (Office Open XML) Documents. You
need to call a different part of POI to process this data (eg HSSF
instead of XSSF) at
org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:179)
at
org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper.java:237)
at
org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:134)
at
org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:117)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:257)
POIFSFileSystem fs=new POIFSFileSystem();
EncryptionInfo info=new EncryptionInfo(EncryptionMode.agile);
Encryptor enc=info.getEncryptor();
enc.confirmPassword("user");
OPCPackage opc=OPCPackage.open("D:/Amar.doc", PackageAccess.READ_WRITE);
OutputStream os=enc.getDataStream(fs);
opc.save(os);
opc.close();
FileOutputStream stream=new FileOutputStream("D:/ao.doc");
fs.writeFilesystem(stream);
stream.close();
System.out.println("running");
I checked and reference to Apache POI documentation, it says that the password encryption for .doc file support since version 3.17, so I give it a try.
It must use HWPFDocument instead to open your doc file.
Then you need to set password by:
Biff8EncryptionKey.setCurrentUserPassword(password);
Complete method:
public static void encryptDocFile(File inputDocFile, File outputDocFile, String password) {
try {
FileInputStream fileInput = new FileInputStream(inputDocFile);
BufferedInputStream bufferInput = new BufferedInputStream(fileInput);
POIFSFileSystem poiFileSystem = new POIFSFileSystem(bufferInput);
// Setting password
Biff8EncryptionKey.setCurrentUserPassword(password);
HWPFDocument wordDoc = new HWPFDocument(poiFileSystem);
FileOutputStream fileOut = new FileOutputStream(outputDocFile);
wordDoc.write(fileOut);
bufferInput.close();
fileOut.close();
wordDoc.close();
System.out.println("Encrypted successfully");
} catch (IOException e) {
System.out.println("Failed to encrypt doc file");
e.printStackTrace();
}
}
Or you can checkout the complete code here:
If you have further question or feedback, please let me know. Thank you
If I got the OutputStream from the response , can you write the contents of the OutputStream to the response which will be sent back to the browser?
In my scenario i want to set a password to Excel file while downloading the sheet.
So that wrote the code like that.
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=TestFile.xls");
XSSFWorkbook wb = new XSSFWorkbook();
Sheet s = wb.createSheet("Demo");
Row row1 = s.createRow(0);
Row row2 = s.createRow(1);
// create cells in the row
Cell row1col1 = row1.createCell(0);
Cell row1col2 = row1.createCell(1);
Cell row1col3 = row2.createCell(0);
Cell row1col4 = row2.createCell(1);
// add data to the cells
row1col1.setCellValue("City Name");
row1col2.setCellValue("University");
row1col3.setCellValue("Hyderabad");
row1col4.setCellValue("JNTU");
// Add password protection and encrypt the file
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
// set the password
enc.confirmPassword("123");
// encrypt the file
OPCPackage opc = wb.getPackage();
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
fs.writeFilesystem(response.getOutputStream());
Here password was set to the excel sheet but the data was not able to open while getting the below error.
please check and provide your answers why am not getting the data. Thanks in advance.
Apache POI uses HSSFWorkbook object for creating xls files and XSSFWorkbook object for xlsx files. Please check what object you used for creating xls.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename="TestFile.xlsx");
File fileVal = new File("TestFile.xlsx");
XSSFWorkbook wb = new XSSFWorkbook();
Sheet s = wb.createSheet("Demo");
//Add data to your sheet
// write the excel to a file
try {
FileOutputStream fileOut = new FileOutputStream(fileVal);
wb.write(fileOut);
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
// Add password protection and encrypt the file
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
// set the password
enc.confirmPassword("123");
// encrypt the file
OPCPackage opc = OPCPackage.open(fileVal, PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
// save the file back to the filesystem
FileOutputStream fos = new FileOutputStream(fileVal);
fs.writeFilesystem(response.getOutputStream());
fos.close();
I am using HWPFDocument to modify some doc file. However, when I try to save a new doc file with anchor image, the image will become broken. Does any method that can handle this case? Here are some my codes.
File file = new File("testdoc.doc");
FileInputStream fis = new FileInputStream(file);
POIFSFileSystem poifs = new POIFSFileSystem(fis);
HWPFDocument doc = new HWPFDocument(poifs);
FileOutputStream out = new FileOutputStream("testtt.doc");
doc.write(out);
out.close();
doc.close();
I do not modify anything of the doc file but the anchor image still become broken.
I do see various options to enforce protection but none with a password. How would I do that?
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
document.enforceCommentsProtection();
document.enforceFillingFormsProtection();
document.enforceReadonlyProtection();
document.enforceTrackedChangesProtection();
document.enforceUpdateFields();
document.removeProtectionEnforcement();
You can give password protection by using apache poi
http://www.quicklyjava.com/create-password-protected-excel-using-apache-poi/
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("xxxxx");
OPCPackage opc = OPCPackage.open(new File("c:/test/sample.xlsx"),PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
FileOutputStream fos = new FileOutputStream("c:/test/sample.xlsx");
fs.writeFilesystem(fos);
fos.close();