• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Cryptography with Password

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I´m publishing a new Class I wrote for encryption/decryption of files using password for symmetric key derivation.
I´d like to get feedback, contributions and suggestions from the community.

IMPORTANT: If a java.security.InvalidKeyException: Illegal key size or default parameters is thrown you must install Java Unlimited Strength Jurisdiction Policy File for your JRE version.

Git repo: https://github.com/fabiofranco85/file-encryptor
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since that page has no documentation, can you tell us a bit about the use cases for this class - what it does, how to use it, and why you found existing tools lacking?
 
Fabio Franco
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your feedback.
Regarding documentation I´ve just added useful informations to README.md
I´d be greatful if you had a look at it and at the comments on the code.
Thank you again.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some comments on your code -

1) Your code loads the whole file into memory before encryption which does not scale well. If one uses the cipher streams there is no need to do this.

2) You seem to be storing the IV is a separate file. There is no need to do this since the IV is not secret. One common approach is to store the IV at the start of ciphertext file.

3) The IV comment of 2) also applies to the salt and iteration count and they can also be stored in the ciphertext file. The only thing that needs to be kept secret and not placed in the ciphertext file is the password!

4) fis.available() does not guarantee to give you the length of the file. To get the length you should use File.length().

5) If you are reading/writing large byte arrays there is no real advantage in wrapping the output stream in buffered streams. Of course if you change to use the cipher streams then there is an advantage in using the buffered streams.


Identifying that a particular file contains ciphertext is not easy with your approach. One approach I have used in the past is to prefix the ciphertext with a digest of the password, salt, iteration count and IV and then follow this with the salt, IV and iteration count and then finally the encrypted data. The digest can then be checked prior to decrypting the ciphertext and the file rejected if the digest does not match.

 
Fabio Franco
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:Some comments on your code -

1) Your code loads the whole file into memory before encryption which does not scale well. If one uses the cipher streams there is no need to do this.

2) You seem to be storing the IV is a separate file. There is no need to do this since the IV is not secret. One common approach is to store the IV at the start of ciphertext file.

3) The IV comment of 2) also applies to the salt and iteration count and they can also be stored in the ciphertext file. The only thing that needs to be kept secret and not placed in the ciphertext file is the password!

4) fis.available() does not guarantee to give you the length of the file. To get the length you should use File.length().

5) If you are reading/writing large byte arrays there is no real advantage in wrapping the output stream in buffered streams. Of course if you change to use the cipher streams then there is an advantage in using the buffered streams.


Identifying that a particular file contains ciphertext is not easy with your approach. One approach I have used in the past is to prefix the ciphertext with a digest of the password, salt, iteration count and IV and then follow this with the salt, IV and iteration count and then finally the encrypted data. The digest can then be checked prior to decrypting the ciphertext and the file rejected if the digest does not match.



First of all thank you for your feedback. I definitely agree on everything you said. To start I will implement the CipherStream and store the iv, salt and interation count at the begining of the ciphertext. I will also follow your suggestion regarding the digest as a prefix to validate the ciphertext.

Thank you again and please feel free to colaborate more.
reply
    Bookmark Topic Watch Topic
  • New Topic