aspose file tools*
The moose likes Java in General and the fly likes Decryption of file is taking too much time. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Decryption of file is taking too much time." Watch "Decryption of file is taking too much time." New topic
Author

Decryption of file is taking too much time.

john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
I am facing a problem while decrypting a file.

The decryption process is too slow. The algorithm i am using is RSA public key encryption. Which is toooooo slow while decrypting the file.

I am doing the following steps ...

1) I read the encrypted data from the file
2) decrypt it and save it in a list
3) Then read the data from the list and write it back in the same file

"Step 3 is fine but decryption itself in step 2 is too slow."


public void Decryption(String fileName) {
try {
byte[] buffer = new byte[256];
ArrayList file = new ArrayList();
InputStream in = new FileInputStream(fileName);

while ((in.read(buffer)) != -1) {
file.add(dcipher.doFinal(buffer));
System.out.println(new String((byte[]) dcipher.doFinal(buffer),"UTF8"));
}

in.close();

FileWriter fw = new FileWriter(fileName);

for (int i = 0; i < file.size(); i++) {
fw.write(new String((byte[]) file.get(i),"UTF8"));
}
fw.close();

} catch (Exception e) {
e.printStackTrace();
}
}



Can somebody check the code and let me know if i am doing something wrong in this....
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
How big is the file? RSA is a slow algorithm.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

John, please UseCodeTags. You can edit your post to add them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

and wrapping a buffer around the input and output may make a significant difference, but it'll still be slow
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

john sal wrote:I am facing a problem while decrypting a file.

The decryption process is too slow. The algorithm i am using is RSA public key encryption. Which is toooooo slow while decrypting the file.


It looks to me like you have a far bigger problem than it just being too slow. The whole encryption/decryption process looks flawed. How badly flawed can only be ascertained after seeing the whole code.

P.S. It is not normal to use RSA directly on files. It is more normal to use a hybrid approach using RSA to encrypt some random bytes that are used to create a session key for use in a symmetric algorithm such as AES.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(And why is the decryption method named "doFinal"?)
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
(And why is the decryption method named "doFinal"?)

That's a method in the javax.crypto.Cipher class.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, okay. I don't like that name.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

David Newton wrote:Oh, okay. I don't like that name.


You are not the first and certainly won't be the last to voice your dislike of that method name. There are various overloaded update() methods which can be called repeatedly until one has finish encrypting/decrypting and then one calls one of the overloaded doFinal() methods. To my mind, if one is going to have doFinal() methods then one should have doUpdate() methods and not update() methods. But it ain't going to be changed now!
john sal
Ranch Hand

Joined: Jul 30, 2010
Posts: 92
I think there is one hybrid approach in which data is encrypted using symmetic algo while the key is encrypted using RSA ... Can somebody let me know what exactly that approach is?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

john sal wrote:I think there is one hybrid approach in which data is encrypted using symmetic algo while the key is encrypted using RSA ... Can somebody let me know what exactly that approach is?


One hybrid approach is given in section 13.6 of "Practical Cryptography" by Fergusen and Schneier published by Wiley.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Decryption of file is taking too much time.
 
Similar Threads
IGNORE THE PREVIOUS BASE64 QUESTION!
encrypt data
encrypt data in database
Need help in password Encryption and Decryption
"javax.crypto.BadPaddingException: pad block corrupted" using BouncyCastle and DESede: How to avoid?