Encryption/Decryption  failure for pdf and MSWord files

Hi,
Is there anybody to help me to find out what is wrong with my class (listing below)? I am sucessfuly using this class to encrypt and decrypt txt, html files but for unknown reasons I am unable to use it for e.g. pdf files. The encrypion somehow works but any atempt to decrypt is a failure.
/* This class accepts an input file, encrypts/decrypts it using DES algorithm and
writes the encrypted/decrypted output to an output file. DES is used in Cipher
Block Chaining mode with PKCS5Padding padding scheme. Note that DES is a symmetric
block cipher that uses 64-bit keys for encryption. A password of length no less
than 8 is to be passed to the encryptFile/ decryptFile methods. This password is
used to generate the encryption key. All exception handling is to be done by
calling methods. These exceptions are thrown by encryptFile/ decryptFile methods.
The input buffer is 64 bytes, 8 times the key size.
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;
public class Crypto
public Crypto(FileInputStream inStream_, FileOutputStream outStream_)
fInputStream_ = inStream_;
fOutputStream_ = outStream_;
public void encryptFile(String password_) throws InvalidKeySpecException, InvalidKeyException,
InvalidAlgorithmParameterException, IllegalStateException, IOException, Exception
DataOutputStream dataOutStream_ = new DataOutputStream(fOutputStream_);
// key generation
SecretKey encryptKey_ = createEncryptionKey(password_);
// Cipher initialization
Cipher cipher_= Cipher.getInstance(cipherType);
cipher_.init(Cipher.ENCRYPT_MODE, encryptKey_);
// write initialization vector to output
byte[] initializationVector_ = cipher_.getIV();
dataOutStream_.writeInt(initializationVector_.length);
dataOutStream_.write(initializationVector_);
// start reading from input and writing encrypted data to output
while (true) {
inputLength_ = fInputStream_.read(input_);
if (inputLength_ ==-1) break;
byte[] output_ = cipher_.update(input_, inputOffset_, inputLength_);
if (output_ != null)
dataOutStream_.write(output_);
// finalize encryption and wrap up
byte[] output_ = cipher_.doFinal();
if (output_ != null)
dataOutStream_.write(output_);
fInputStream_.close();
dataOutStream_.flush();
dataOutStream_.close();
public void decryptFile(String password_) throws IllegalStateException, IOException, Exception
DataInputStream dataInStream_ = new DataInputStream(fInputStream_);
// key generation
SecretKey encryptKey_ = createEncryptionKey(password_);
// read initialization vector from input
int ivSize_ = dataInStream_.readInt();
byte[] initializationVector_ = new byte[ivSize_];
dataInStream_.readFully(initializationVector_);
IvParameterSpec ivParamSpec_= new IvParameterSpec(initializationVector_);
// Cipher initialization
Cipher cipher_= Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher_.init(Cipher.DECRYPT_MODE, encryptKey_, ivParamSpec_);
// start reading from input and writing decrypted data to output
while (true) {
inputLength_ = fInputStream_.read(input_);
if (inputLength_ ==-1) break;
byte[] output_ = cipher_.update(input_, inputOffset_, inputLength_);
if (output_ != null)
fOutputStream_.write(output_);
// finalize decryption and wrap up
byte[] output_ = cipher_.doFinal();
if (output_ != null)
fOutputStream_.write(output_);
fInputStream_.close();
fOutputStream_.flush();
fOutputStream_.close();
// the following method creates the encryption key using the supplied password
private SecretKey createEncryptionKey(String passwd_) throws InvalidKeySpecException,
InvalidKeyException, NoSuchAlgorithmException
byte[] encryptionKeyData_ = passwd_.getBytes();
DESKeySpec encryptionKeySpec_ = new DESKeySpec(encryptionKeyData_);
SecretKeyFactory keyFactory_ = SecretKeyFactory.getInstance(algorithm_);
SecretKey encryptionKey_ = keyFactory_.generateSecret(encryptionKeySpec_);
return encryptionKey_;
private FileInputStream fInputStream_;
private FileOutputStream fOutputStream_;
private final String algorithm_= "DES";
private final String cipherType= "DES/CBC/PKCS5Padding";
private byte[] input_ = new byte[64]; // The input buffer size is 64
private int inputLength_;
private final int inputOffset_= 0;
}

Please can u give me refined code for me///
at [email protected]
Hi,
I found at least one thing wrong. In the decrypt
method you are reading from 'fInputStream_' rather
than 'dataInStream'.
Worked for me on MSWord after changing this!
Roger
// start reading from input and writing decrypted
ted data to output
while (true) {
inputLength_ = fInputStream_.read(input_);
if (inputLength_ ==-1) break;
byte[] output_ = cipher_.update(input_,
input_, inputOffset_, inputLength_);
if (output_ != null)
fOutputStream_.write(output_);

Similar Messages

  • Bridge does not show dimensions for .PDF and .AI files

    Is there any way to see the dimensions in cm (or inch) for PDF and AI files in Bridge? I'm using Bridge CS5 4.1.0.54 as part of CS5.5 Design Premium, have set the preferences in Bridge to show them, but there are no values in the metadata fields for dimensions in inch/cm. I don't know if this behaviour is normal or if there's something wrong with my installation...
    TIA,
    Matthias

    May be 'dimension' is not the right word/translation. In my german version it says 'Abmessungen (in Zoll)' or 'Abmessungen (in cm)', which should mean something like 'dimension' or 'size'. DIN A4, A5, US Letter, Legal etc. pp. in cm or inch.

  • Performance, minimum bandwidth, thumbnail previews for PDF and InDesign Files with web content viewer

    Hi,
    We are considering using DPS with a web content viewer for large PDFs and/or InDesign files. The client using a web viewer might have very limited bandwidth (some as low as 256Kbps). We ask:
    1 - How well does the web viewer perform with very large PDFs and InDesign files  (500 MB+) with limited bandwidth? Does the file stream in chunks and the PDF shows as soon as the first page or two are available, with the rest loading in the background, or does the entire file come down in one part and then shown?
    2 - Can PDF and InDesign files support thumbnail previews? For example, one image per PDF page and user can scroll forward and back.
    Your thoughts and comments are greatly appreciated.
    Thanks!
    -Stephen

    Hi Stephen,
    The way the web viewer works is that it converts the PDF/InDesign files to PNGs along with additional HTML assets. Assets are downloaded as they’re ready for a given page (e.g. first, page 1 is filled with its assets as they become available, then page 2, etc), so a reader wouldn’t need to wait for the entire article to load. The next few pages are also loaded in the background, so the reader wouldn’t be likely to notice lag as they flipped through pages to the next page after reading the current one. So the lag will depend on the size of an individual page.
    For your question around thumbnails, the web viewer does not load a preview image so the reader would need to wait for the assets I described above to load.
    Hope this helps...
    Brian

  • Does Adobe have a free download for pdf and data files

    I cannot open pdf files AND data files with this expensive pc & Windows 7 HELP

    Don't know what are "data files", but you can open PDF files using the free Adobe Reader: Adobe - Adobe Reader download - All versions

  • App with hadwriting notes for .pdf and .doc files?

    Is there and app that aloud to makes HANDWRITING NOTES (with Stylus, using different colours, sizes, etc) in files .pdf or .doc???
    I really need something like Paperpad App, to make notes while reading.
    Thank you.

    So...there is no app for handwrite in a .doc or in a .pdf?????????

  • Add-on programs for PDF and Flash files

    Is there an PDF reader that can be loaded onto the 8130 to read PDF files online? How about a mobile version of Flash? I am finding out that there are alot of things that won't work on it that will work on my desktop which I understand. How about watching videos from YouTube?

    Flash can't be used in BlackBerry device...till date! May be in future device development will resolve the issue..Considering the public demand!
    BlackBerry devices are capable of viewing PDF files. Are you facing any difficulty in reading them. Can you please let know what type of error is being generated? 
    tanzim                                                                                  
    If your query is resolved then please click on “Accept as Solution”
    Click on the LIKE on the bottom right if the post deserves credit

  • Office, PDF and CAD files upload into the dms system and viewing them .....

    i have uploaded a excel file into the dms server
    i dont want others to save the doc , while viewing in the sap viewer.( made necessary setting)
    any setting required for me to do this???
    i am able to view the office files in sap viewer, can i have the same option for PDF also??? and the same to CAD files also?
    Regards
    surya

    Dear Surya,
    of course you can have the same behavior for PDF and CAD files. You just have to create a new workstation application for these filetypes in transaction DC30 and maintain the correct parameter so that these files are also viewed by the integrated ECL Viewer.
    For further information please see the following link:
    https://www.sdn.sap.com/irj/scn/wiki?path=/display/plm/2.Customizing%2528CA-DMS-EAI%2529
    Best regards,
    Christoph

  • Japanese/C​hinese Font for PDF and MS Office files

    Hi,
    I have both the BB9860 and the Playbook, always uses the Playbook to download and read email attachments.
    However, I realise that for  PDF and MS Office files, japanese/Chinese fonts are not supported.
    Is there any way for me to download the add-ons and install into my playbook?
    Thanks.
    Regards, Song Por

    PDF can display chinese, MS Word cannot display chinese.

  • Since I have connected my iPad with the MacBook through ICloud I have problems in Preview of opening PDF- and JPG-files. Does anyone have a solution for this?

    Since I have connected my iPad with the MacBook through ICloud I have problems in Preview of opening PDF- and JPG-files. Does anyone have a solution for this?

    I'm not sure I understand the connection between iCloud and this problem.  Is it simply a problem of coincidental timing (ie, the problem happened after you set up iCloud)?  If so, there's almost certainly no connection between these two events.
    Where are you getting the files you're trying to open, and what specifically happens when you try to open them?

  • Saving report as PDF and text file

    Currently, I saved my report as PDF. It opens in Acrobat reader automatically when the report is executed. Is there a way for me to save it as PDF and as a text file at the same time? (without having to open the file in Acrobat reader and saving it)

    You can add your PDF and TEXT file formats to a distribution list (Instead of GENERATE REPORT TO A FILE). To do this:
    1. Go to your Object Navigator and highlight your report.
    2. Right click on your report. Select Property Palette.
    3. Select Distribution
    4. You can add of of the formats that you want to print or save to the report to.
    5. You will have to add Distribution Id (any number starting with 1) where you want the file to be saved (c:\my_reports\inventory.pdf), the type of file: PDF and how many copies you want: 2
    6. Click OK after finishing...
    To make it work ... You would highlight your report from your Object Navigator and then choose FILE-DISTRIBUTE and this will save it to all the files your specified and/or printer.
    null

  • A working method to load local PDF and HTML files on iOS

    I had a lot of trouble getting this to work, and I'm hoping this post saves someone time. Some of the information that's been posted in other locations is either wrong, incomplete, or might only work on Android. By the time you read this message the information here may no longer be accurate, so here's the testing environment:
    Window 7
    Flash CS 5.5.0
    AIR 2.7.0.19530, which was compiled on June 28, 2011
    iPad 1, version 4.3.5 of iOS
    Let's get started.
    On iOS, you load external PDF and HTML files using the StageWebView class.
    On Windows, StageWebView works but the HTMLLoader class is a better choice if you're creating a desktop app.
    You can also load HTML files by reading in the file's text. The information in this post is only for loading external HTML files.
    StageWebView will not load a file that's in File.applicationDirectory. All files bundled in your app are placed in File.applicationDirectory, which means you'll have to copy any external file you wish to load with StageWebView to another directory.
    So where can you copy your file? File.applicationStorageDirectory won't work. File.documentsDirectory does work.
    Several people have recommended copying to a temporary file using File.createTempFile(). This works, but there's a catch: it seems that, like Windows, StageWebView relies on a file's extension when determining how to load it. When you create a temporary file on iOS using File.createTempFile(), the file will have no extension (and on Windows, File.createTempFile() creates a file with the extension .tmp, which is equally problematic).
    The solution to the file extension problem is to rename the temporary file by appending the original file's extension. AIR currently does not have a <file>.rename() function, so you'll have to do it using <tempFile>.moveTo().
    Here's some code I've successfully tested several times on both iOS and Windows. The file is copied to the temp directory. The file's extension is restored by just slapping the original file name to the end of the temp file.
            private function loadExternalFile():void
                var webView = new StageWebView();
                webView.stage = this.stage;
                webView.viewPort = new Rectangle( 0, 0, 1024, 555 );
                // Works with either html or pdf files.
                // These are stored in the root of the application directory.
                var fileName:String = "euei.pdf";
                //var fileName:String = "euei.htm";
                var sourceFile = File.applicationDirectory.resolvePath( fileName );
                var workingFile = File.createTempFile();
                try
                    sourceFile.copyTo( workingFile, true );
                    // You have to rename the temp file
                    var renamedTempFile:File = workingFile.resolvePath(workingFile.nativePath + fileName);
                    workingFile.moveTo(renamedTempFile, true);
                    webView.loadURL( renamedTempFile.url );
                catch (err:Error) { }

    I tried this with Flash CS5.5 and AIR 4.0 SDK. Any pdf loaded simply fills the viewPort with black. Also tested with a png version of the pdf and that displayed just fine.
    What's the purpose of copying to a temp work file? I found that webView.loadURL( sourceFile.url ); gave me the exact same results.
    Any ideas?
    Thanks!

  • How to read data from PDF and HTML  file

    I have got solution to read text form .txt file but did'nt get code for PDF and HTML.
    I dont want to convert PDF to txt.
    Please help me ...

    ah crap i could have guessed there would be a crosspost only the forum in where the crosspost is made is abit funny
    To OP: DO NOT CROSSPOST
    http://forum.java.sun.com/thread.jspa?threadID=5267875&tstart=0

  • Save as pdf and excel file

    Hi All,
    Can anyone tell me how to save data available in a node(model or value) as pdf and excel files?
    Thanks in advance.....
    Bhushan Reddy.

    Bhushan,
    check these links for saving as excel
    Excel Export - Funky Column Headers
    https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/webDynproJava-ExportingTableDataUsingOn-DemandStreams-SAPNW+7.0
    Exporting table data to MS-Excel Sheet(enhanced Web Dynpro Binary Cache)
    https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/webDynproJava-ExportingTableDataUsingOn-DemandStreams-SAPNW+7.0
    Handling FileUpload and FileDownload in NetWeaver Developer Studio(NWDS) 2004S
    New Web Dynpro Java Tutorials - Uploading and Downloading Files in SAP NetWeaver 7.0
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/00062266-3aa9-2910-d485-f1088c3a4d71
    Thanks
    Bala Duvvuri

  • PDFs and Word Files open on iMac but not Macbook Pro

    I own an iMac and a MacBook Pro. Recently, I've been having an issue with some PDFs and Word files that have been emailed to me from different contacts. All open—and can be seen using Quick Look—on the iMac. However, on the MacBook Pro it's a different story. When I try Quick Look all I see are the icons. When I try to open the PDFs in Preview (default app for me) I get an error message saying the file could not be opened and might be damaged. When I try to open the Word doc in Word, Pages or Bean I see all of the extraneous header information etc and the text is all mixed up in that formatting junk. I have repaired permissions, cleared out my user cache, restarted—pretty much everything I can think of. But no luck. Thought?

    Brilliant suggestion! I checked all the display settings on my iMac 27" and confirmed they were the same settings as my MacBook -- until I checked the Window 7 screen resolution. Because of its size, I had expanded the screen across most of the monitor. When I changed the resolution to one suggested or configured by Windows, Approach worked fine. I've now tested it on quite a few of the settings, and as long as I let Windows determine the best resolution (on my iMac 27" it seems to be 2043 x 1276), Approach works like it should. Thank you!

  • Where does workspace store the pdf and excel file ?

    Hi,
    I'm working on a Web Analysis project and I have created a dynamic web menu based on the workspace repository to improve the poor capacities of WA in terms of menu. I use the javascript API to control the WA applet from the menu.
    Now my problem is that my customer created a folder in workspace (which is translated into a sub menu of my dynamic menu), and that folder contains pdf and excel files. The function openFile which I use to open report in the applet doesn't work on pdf and excel files. So I've been looking for the files on the server, to know where they are stored, so that I could just modify the java class that generates the menu.
    I couldn't find those files on the server. Where are they ? Are they stored in the database as an xml string like the report s ? If yes then I might have a problem.
    Has anyone already done this ?
    Thanks for your help.
    Cyril

    Tomcat stores it in the work dir under $TOMCAT_HOME.
    There'll be directories like localhost%2f<urcontext>..
    As for as JSP changes being recognized, try refreshing ur browser....

Maybe you are looking for

  • My mac mini keeps freezing up on me at random

    Completely at random it gives me the turning beachball? I have tried to google the issue but anything i have found doesnt seem to work, i did see some suggestions from other people to run etrecheck so i did and here are the results. I should also add

  • Url change with servlet to jsp

    Hi, i guess i have a small problem- i am using this code to forward from a servlet to a jsp: protected void passProcessing(String url, HttpServletRequest req, HttpServletResponse res) {           RequestDispatcher rd = req.getRequestDispatcher(url);

  • Mirror and Extended Desktop - Panasonic 42" 720 image cropped

    I have my 15.4 macbook pro connected to a Panasonic 42" 720 HDTV via the mini displayport. In both mirror and extended desktop mode, the Panasonic image is cropped off on all four sides. I fooled around with all the resolution settings and nothing ch

  • Joining multiple tables in Oracle DB

    I have the following tables. 1. AddProject - projID - projName 2. AddLab - labID - labName 3. ProjLabAssociation - projID - labID 4. AddResearchArea - raID - raName 5. ProjRAAssociation - projID - raID AddProject is associated with both the tables-Ad

  • Adobe wont print after update 9

    Can't print after updating to Adobe reader 9. Using a B&W multi "old girl" Lanier 5235 and I tried uninstalling Adobe and reinstalling without help. I can print a PDF file via Preview and I csan print word files via word etc. etc using the diff. case