-1

we have a java application running on linux. when the app crashes and restarted- we need to recover ( persist ) a very sensitive information that was stored in previous run ( global static var ). no database allowed ( insecure ) . also it's important that no-one can undetectably change this information ( e.g. override a file etc ).

Thanks in advance

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44
  • Sounds like you want to encrypt the data and write it to disk when they change. If reading the data is okay, modifying it is not you can produce a message digest. – Peter Lawrey Sep 12 '11 at 13:31
  • @Peter Lawrey but hashing is one-way, isn't it? So he can't read it back. – Ashkan Aryan Sep 12 '11 at 13:35
  • your use case seem to be a bit incomplete. Are you asking how to encrypt data in Java in general? or ...? Who should be able to decrypt it, and using what credentials? – Ashkan Aryan Sep 12 '11 at 13:37
  • If reading the data is okay, you can write the data in plain text or binary AND a message digest. The data can be read easily, but if the message digest doesn't match it has been changed. – Peter Lawrey Sep 12 '11 at 13:38
  • Another approach is to have the application run as a special userid people don't have access to. (You have to trust root anyway) – Peter Lawrey Sep 12 '11 at 13:39
  • @Peter Lawrey: A message digest is not a safe way to check that some text has not been modified, because an attcker would just have to change the message and digest it again. You need some secret. Saving the encrypted text, and a message digest of the original text would be OK: you decrypt it with your secret key, digest the result and compare it to the stored digest. – JB Nizet Sep 12 '11 at 13:46
  • @JB Nizet, I had assumed the digest would contain some secret as well. I should have been clearer. :| – Peter Lawrey Sep 12 '11 at 13:55

2 Answers2

0

I'd suggest using Jasypt. you can implement Externalizable & encrypt the object stream

BasicPasswordEncryptor encryptor = new BasicPasswordEncryptor();
String cipher = encryptor.encryptPassword(<<stream to encrypt as string>>);
...
if (encryptor.checkPassword(inputPassword, encryptedPassword)) {
  // correct!
} else {
  // bad login!
}
Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
0

To secure the data you need to encrypt it. Use AES-256 in CBC or CTR mode with PKCS7 padding. Do not store the encryption key in your application. You can store the IV/nonce as it does not need to be secure.

To ensure that the data is not changed use HMAC, with SHA-256. You must use a different key for the HMAC than you used for AES.

rossum
  • 15,344
  • 1
  • 24
  • 38
  • thank you for your answer. my question is how can i prevent from the users make a copy paste to the file. so the program will run with the old data (i change the data in my program). – user940625 Sep 12 '11 at 14:36
  • @user940625: Either include the current date/time in the data you HMAC or use a serial version counter. In either case reject any version less than the current counter/date (with allowances for over midnight). – rossum Sep 12 '11 at 15:46