I want to use Salsa 20 in Java, so I downloaded Bouncy Castle, and... it makes no sense to me. I've got it working, but most of my choices were essentially random. But I can't see there anything leading me to the right path. The interface CipherParameters accepts nearly any nonsense and a key size of 257 bits works perfectly.

private static StreamCipher newCipher(byte[] password, byte[] salt) {
    int iterations = 50000; // really?
    PBEParametersGenerator generator =
        new PKCS5S2ParametersGenerator(); // really this one?
    generator.init(password, salt, iterations);
    Arrays.fill(password, (byte) 0);
    final CipherParameters cipherParameters =
        generator.generateDerivedParameters(256, 64); // would 257 be better? :D
    final StreamCipher result = new Salsa20Engine();
    result.init(true, cipherParameters);

As I wrote, it works, but is this the right way? Especially,

  • Is PKCS5S2 with 50000 iterations a good choice?
  • The password gets stored nowhere, so I'm assuming, the only thread would be an attacker having access to both the plaintext and the ciphertext, xoring them together, and using it for verification of their password guesses. Am I right?
  • Concerning the practical POV, is there a way how to verify I did nothing seriously wrong (like switching password and salt or using a inappropriate method)?
  • $\begingroup$ Downvoter, feel free to add an answer. If the code is crap... well, then I'm right as I expected it. $\endgroup$ – maaartinus Oct 13 '14 at 15:59
  • 1
    $\begingroup$ The question is off topic for this site. $\endgroup$ – ddddavidee Oct 13 '14 at 17:08
  • $\begingroup$ @ddddavidee How is it different from e.g. this one or this one? I just didn't ask explicitly about what key derivation algorithm should I use, etc., but the question is there. And assuming it's not appropriate here, where should it be? Buddhism? Pets? Just don't say SO or CR, as the kind of knowledge I need is not there. $\endgroup$ – maaartinus Oct 13 '14 at 17:18
  • $\begingroup$ Maybe you should ask your question in the general way, without any bound to a particular library. (btw I'm not the downvoter) $\endgroup$ – ddddavidee Oct 13 '14 at 17:20
  • 4
    $\begingroup$ I'm voting to close this question as off-topic because it is a programming problem, not a cryptography problem. $\endgroup$ – CodesInChaos Jun 29 '15 at 10:27

My code above is a fascinating huge damping pile of... you know.

generator.init(password, salt, iterations);
Arrays.fill(password, (byte) 0);

BC fails to copy the content of the password array, so my security "improvement" of clearing the password after use, clears it before use and leads to a password of all zeros.

Btw., the mailing list was no better than this site. No answer.

| improve this answer | |
  • 1
    $\begingroup$ @e-sushi While your suggestion helps to avoid close-votes, it won't help to get a sensible answer. Posting it on SO (which I know pretty well) is a sort of waste of time. See also my comments below my question. $\endgroup$ – maaartinus Jul 2 '15 at 16:56
  • 1
    $\begingroup$ Honestly, looking at things again I´ve started to realize that too. Sorry for having bugged you with a comment which was (as it turns out) rather “superfluous” because you obviously seem to know your ways around the SE network already. Oh well, at least I was able to drop some benign votes while I was at it… $\endgroup$ – e-sushi Jul 2 '15 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.