views:

342

answers:

1

Here's the encryption portion of my code. It compiles fine but fails with that exception at runtime:

import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

...

byte[] salt = new byte[8];
Random rand = new Random();
rand.nextBytes(salt);

PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 1000);

Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] ciphertext = cipher.doFinal(plaintext);

Does this algorithm not come with Java 1.5? I don't mind using another algorithm, I just don't know what is available. I hope I don't have to use an external library like bouncycastle, because I've struggled for days trying to get it working to no avail (by including it in my .jar application it triggers an "Invalid signature file digest" error).

+1  A: 

Looks like you may have to use some other cipher if you do not wish to rely on some external library like BouncyCastle.

See the documentation about Sun crypto providers for more details about what's supported straight out of the box.

andri
Bless you! This is exactly what I needed. It's a shame I may need to use outdated algorithms like DES but it appears to run well so it's good enough for my purposes, thank you very much.
oskar
AES is pretty good, except for the back door the NSA put in there. :blackhelicopters:
stimms