sentence
stringlengths
1
1.38k
label
stringclasses
3 values
Salts are not necessary for symmetric encryption.
o
IVs serve a similar purpose, and are prepended to the ciphertext by most (all?
o
) crypto libraries.
o
@Nick: Read PKCS
o
Salts are necessary for PBKDF2, which is why the API for password-based encryption requires them as input for key derivation.
o
Without salts, a dictionary attack could be used, enabling a pre-computed list of the most likely symmetric encryption keys.
o
Cipher IVs and key-derivation salts serve different purposes.
o
IVs allow one reuse the same key for multiple messages.
o
Salts prevent dictionary attacks on the key.
o
Does this usage of SecureKey and PBEKeySpec produce a Password-based key that is RFC2898-compliant?
o
URL_http://www.ietf.org/rfc/rfc2898.txt .
o
Yes, that is the "PBKDF2" of the SecretKeyFactory algorithm name.
o
It is referring to PKCS PBKDF2 (Password-Based Key Derivation Function
o
If you were storing encrypted data in a database using the method above, would you store the ciphertext (blob) and the iv (char) in the database, and the password and salts would be supplied to the client application by the user or in the application's configuration files?
o
In that case, I'd store the ciphertext in one field, the IV in another, and the "salt" and "iterations" together in a third.
o
I'd prompt the user in the client application for the password, and derive the key using the stored salt and iterations.
o
Then initialize the cipher with the derived key and stored IV, and decrypt the content.
o
@erickson: in case you store iv, salt, etc.
o
in the database, why not just use 'PBEWithMD5AndDES' and append the salt to the encrypted text, after loading u can strip the salt and use it for decryption.
p
This way PBE is implemented in jasypt framework: URL_www.jasypt.org.
o
First, that would be DES encryption, not AES.
o
Most providers don't have good support for the PBEwithand algorithms; for example, the SunJCE doesn't provide and PBE for AES.
p
Second, enabling jasypt is a non-goal.
o
A package that purports to offer security without requiring an understanding of the underlying principles seems dangerous prima facie.
o
@erickson so the lack of support is the only criteria not to use 'PBEWithMD5AndDES'?
o
Are there any further criterias not to use this algo for enryption of data stored in a db?
o
Although MD5 is "broken" in some applications, I don't think that it is a problem here.
n
But the weakness of DES is definitely a problem.
n
The tiny key of DES can be broken in hours or days using a few thousand dollars worth of equipment.
n
I've implemented @erickson's answer as a class: URL_https://github.com/mrclay/jSecureEdit/tree/master/src/org/mrclay/crypto (PBE does the work, PBEStorage is a value object for storing the IV/ciphertext together.
o
).
o
@erickson Could you post your answer implemented in the example: URL_http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html , I am not sure how the password and salt should be correct specified or generated in the latter case?
o
@Astron Sorry, I don't really understand your question.
n
Are you asking why the example you linked to doesn't use salt and password?
o
If so, that's because they aren't doing password-based encryption there; the key is a random sequence of bytes, not derived from a password.
o
@erickson Sorry, I meant I am looking for a working implementation of the example linked using your password-based encryption.
n
I tried plugging in your respective examples into the example but have yet been able to get it to work correctly.
p
I could start a new question but figured it would be helpful for others if it were here as I am not looking for any deviation.
p
This solution does not provide authenticated encryption, does it?
o
Don't we also need to MAC the ciphertext to get authenticated encryption?
o
@EricConner You're right, there is no integrity or authentication provided, only privacy.
o
In order to get authentication, I'd recommend a different mode, like CCM, if your provider supports it (the SunJCE provider doesn't implement any authenticating cipher modes, at least not up through Java 7).
p
Trying to implement this with Doug's code below.
o
erickson says to "Share the password (a char[]) and salt (a byte[]8" with recipient but then "send the ciphertext and the iv to the recipient" which is it?
o
the iv or the salt?
o
They are different correct?
o
@wufoo They different, and you share both.
o
You'll probably share the salt (for key derivation) one time, at the same time you set up the password itself.
o
But every message you send after that should include the unique IV that was used to encrypt that message (and that message.
o
@wufoo By the way, Doug's code has a number of security flaws.
o
I can't recommend it as an example.
n
@erickson - thanks for the reply.
o
Planning on posting my fixes for feedback when I get to that point.
o
It's not an ideal place for neophytes (bouncycastle license won't work for me), but with 83k views to the thread a more polished example is really needed.
p
I see you've graciously offered to do that below, so I'll wait and see.
p
@wufoo The problem with a more developed example is that doing things the "right way" would require some encoded structures for transmitting the message and IV, and the PBE parameters.
o
It ends up being not fitting well into this Q&A format, and it's code that you probably don't need to write yourself.
p
@erickson I'm new here, not sure how this works but would you be interested in bounty points to fix up Dougs code?
o
Or maybe a link to a more developed example?
o
I've combed google for days and this exact thread keeps coming up.
o
I've only got a couple other examples: URL_http://www.digizol.org/2009/10/java - encrypt-decrypt-jce-salt.html and URL_http://cs.saddleback.edu/rwatkins/CS4B/Crypto/FileEncryptor.html but they are not complete.
o
Requring, as Doug put it, Frankensteining something.
o
@erickson I am just storing hashed/encrypted passwords in a database.
p
Can your example be used for safe storage of passwords, rather than using a sha1sum of the password and a salt?
o
@AndyNuss This example is for reversible encryption, which generally should not be used for passwords.
o
You use the PBKDF2 key derivation to "hash" passwords securely.
o
That means that in the example above, you'd store the result of tmp.getEncoded() as the hash.
o
You should also store the salt and the iterations (65536 in this example) so that you can recompute the hash when someone tries to authenticate.
o
In this case, generate the salt with a cryptographic random number generator each time the password is changed.
p
For running this code, make sure you have the right Unlimited Strength Jurisdiction Policy Files in your JRE as stated in URL_http://www.ngs.ac.uk/tools/jcepolicyfiles .
o
Is there a way of doing this without Unlimited Strength Jurisdiction Policy files?
o
@ArchimedesTrajano Not with the Java Cryptography Architecture.
o
I would suggest using AES-128.
o
AES-128 is good.
p
NIST recommends it for applications through 2030.
p
If you really need AES-256 and can't get it through the JCA, you can use a non-standard API, like BouncyCastle's "lightweight" crypto API directly to bypass the restrictions.
p
I can't speak to the legal ramifications of doing so (your software might be illegal to import into certain regions).
n
+1 but if you send the encrypted data over the network as implied in the answer you have to make sure that integrity of the message is ensured.
p
In other words you need to generate another key to use a MAC, or you could use an authenticated mode of encryption such as GCM.
p
After reading through erickson's suggestions, and gleaning what I could from a couple other postings and this example URL_http://cs.saddleback.edu/rwatkins/CS4B/Crypto/FileEncryptor.html [here] , I've attempted to update Doug's code with the recommended changes.
p
Feel free to edit to make it better.
p
Initialization Vector is no longer fixed encryption key is derived using code from erickson 8 byte salt is generated in setupEncrypt() using SecureRandom() decryption key is generated from the encryption salt and password decryption cipher is generated from decryption key and initialization vector removed hex twiddling in lieu of org.apache.commons URL_http://commons.apache.org/codec/download_codec.cgi [codec] Hex routines Some notes: This uses a 128 bit encryption key - java apparently won't do 256 bit encryption out-of-the-box.
o
Implementing 256 requires installing some extra files into the java install directory.
o
Also, I'm not a crypto person.
o
Take heed.
o
CODESNIPPET_JAVA1 .
o
Consider using the URL_http://docs.spring.io/spring- security/site/docs/3.1.x/reference/crypto.html [Spring-Security-Crypto-Module] The Spring Security Crypto module provides support for symmetric encryption, key generation, and password encoding.
o
The code is distributed as part of the core module but has no dependencies on any other Spring Security (or Spring) code.
o
It's provides a simple abstraction for encryption and seems to match what's required here, The "standard" encryption method is 256-bit AES using PKCS PBKDF2 (Password-Based Key Derivation Function
p
This method requires Java 6.
o
The password used to generate the SecretKey should be kept in a secure place and not be shared.
p
The salt is used to prevent dictionary attacks against the key in the event your encrypted data is compromised.
p
A 16-byte random initialization vector is also applied so each encrypted message is unique.
p
A look at the URL_http://grepcode.com/file/repo1.maven.org/maven2/org.springfr amework.security/spring-security-core/3.1.4.RELEASE/org/springframework/securi ty/crypto/encrypt/AesBytesEncryptor.java#53 [internals] reveals a structure similar to URL_http://stackoverflow.com/a/992413/277307 [erickson's-answer] .
o
As noted in the question, this also requires the _Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy_ (else you'll encounter URL_http://stackoverflow.com/questions/3862800/invalidkeyexception-illegal- key-size/3864325 [CODETERM1] ).
o
It's downloadable for URL_http://www.oracle.co m/technetwork/java/javase/downloads/jce-6-download-429243.html [Java-6] and UR L_ URL_http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-43212 4.html [Java-7] .
o
Example usage CODESNIPPET_JAVA1 .
o
And sample output, CODESNIPPET_JAVA2 .
o
What I've done in the past is hash the key via something like SHA256, then extract the bytes from the hash into the key byte[].
o
After you have your byte[] you can simply do: CODESNIPPET_JAVA1 .
o