File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to select specificied parts of a file to decrypt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to select specificied parts of a file to decrypt" Watch "How to select specificied parts of a file to decrypt" New topic
Author

How to select specificied parts of a file to decrypt

Vimal Kumar Venugopal
Ranch Hand

Joined: Aug 20, 2011
Posts: 43

Hi JavaRanch,

My recent work involved encrypting sensitive information while logging. It was for a Interactive Voice Response (IVR) application where users would enter User IDs and Passwords and they needed to be masked. It will be decrypted in a UI by people who have access to key.

With a lot of help from Coderanch and by doing a bit of tweaking of the application files and log4j files, I was able to bring about encryption successfully.

This is how my log file would look:

TIME STAMP [DEBUG] Caller ANI 4447478375
TIME STAMP [DEBUG] DNIS 1540
TIME STAMP [INFO] Main menu message played to caller
TIME STAMP [DEBUG] Option 1 selected
TIME STAMP [INFO] Recharge menu message played to caller
TIME STAMP [INFO] Caller initiates DTMF feed for UserID
TIME STAMP [DEBUG] UserID entered: <Encrypted> `5¿JZ[üÈv€ ò`º*€• ÂÈ"W% \Q±(X
‹s[⚃3\0ƒ‡h?ƒ«~ú=ÒÂ#íø"QnGpØfDT
¡b@éBA>«†$™±5VIlJ*pX݃Rƒj
</Encrypted>
TIME STAMP [INFO] Caller initiates DTMF feed for Password
TIME STAMP [DEBUG] Password entered: <Encrypted> `5¿JZ[üÈv€ ò`º*€• ÂÈ"W% \Q±(X
‹s[⚃3\0ƒ‡h?ƒ«~ú=ÒÂ#íø"QnGpØfDT
¡b@éBA>«†$™±5VIlJ*pX݃Rƒj
</Encrypted>
TIME STAMP [INFO] Transaction successful
TIME STAMP [INFO] Caller disconnects

Now my problem comes up with debugging. I need to select the entire text between <Encrypted> </Encrypted> (I appended these tags while encryption, thinking that it might help me to locate the source text for decryption). I thought of selecting the text between these tags, pass it to a decryption and replace it with the decrypted text.

I tried a couple of options to select the text between the tags and neither of them were fruitful.
1) I tried using a SAX parser implementation, but got a couple of SAXParseExceptions since the log file does not have a well defined Tag structure that an XML file should have.
2) I tried using a Regex - Pattern and Matcher to locate the text, I could not make a match for the tags, since the encrypted text contains newlines and I could only feed one line at a time by the below expression.
pattern.Matcher(in.readLine());
As you can see, the encrypted text spans many lines and reading one line at a time, could not make a match for the tags.
3) One other option yet to be tried was to read the whole file, character by character and look for a match. But the process would affect the performance and it a tedious one to code too.

Please help me out as I am new to java.

Thanks,
Vimal.


"Value has a Value only if its value is valued!"
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
2) I tried using a Regex - Pattern and Matcher to locate the text, I could not make a match for the tags, since the encrypted text contains newlines and I could only feed one line at a time by the below expression.
pattern.Matcher(in.readLine());
As you can see, the encrypted text spans many lines and reading one line at a time, could not make a match for the tags.

Regexps have no problem dealing with multiline text; check the Pattern.compile(String, int) method and the Pattern.MULTILINE flag. The problem is that encrypted data is binary, not text, so retrieving it after it has been stored in a text file is problematic, to say the least. You should convert it to text (using something like base-64 encoding) before writing it to the log file; that would also result in a string that fits on a single line, thus handling the multi-line issue.

3) One other option yet to be tried was to read the whole file, character by character and look for a match. But the process would affect the performance and it a tedious one to code too.

It may be slower, but the relevant question is: would it be too slow? No way to tell until you try. And I sure hope you weren't serious about the last part of that sentence; it can't possibly be more than 10 lines of code you're talking about.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to select specificied parts of a file to decrypt