Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Decryption of file is taking too much time.

 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How big is the file? RSA is a slow algorithm.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, please UseCodeTags. You can edit your post to add them.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and wrapping a buffer around the input and output may make a significant difference, but it'll still be slow
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(And why is the decryption method named "doFinal"?)
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(And why is the decryption method named "doFinal"?)

That's a method in the javax.crypto.Cipher class.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, okay. I don't like that name.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic