Answers to individual questions:
- I encrypt the field in the java application or in the database?
The best option is to encrypt on the application side as soon as you receive the information to be encrypted.
- And how can I do that?
It is not so simple, but there are several things ready for known algorithms, safe and recommended on the internet, just adapt to your specific use case.
For example, you could use PBKDF2 to store passwords. See an example using common Java libraries: https://gist.github.com/jtan189/3804290
- I have to create a method to encrypt in java code or something is already ready to use?
Behold 2.
=]
Security
Password encryption nay must be reversible. The answer link cited in the comments of the question, should not be considered for password encryption, as it is reversible, and even has security problems in some of the answers.
It is recommended that a strong password "encryption" algorithm be used, such as the bcrypt
or the pbkdf2
. Java has implementations for these algorithms, so just adapt your table (in case usuario
) to store the additional values the chosen algorithm needs.
Modern computers have in the processor instructions that allow very fast execution of hash functions such as SHA and MD5; in addition, the GPU allows multiple hashes to be generated simultaneously, increasing much the amount of tests per second in a brute force attack. These algorithms are made so that, in addition to being mathematically safe, they are slow to be executed especially in a GPU, which has the advantage of parallelism.
In any case, if you want to use something really simple, use something like the following:
Add a column salt
on your table usuario
.
Choose a known collision-free hash, such as SHA2 or SHA3 (and use the 256-bit version or later).
In the password column, store (in pseudo-code):
usuario.senha = hash(concatena(salt, senha_digitada));
- To validate the password, use the expression (in pseudo-code):
senha_valida = usuario.senha == hash(concatena(salt, senha_digitada));
This encryption should be done in the application, take a look at this example: http://stackoverflow.com/questions/1205135/how-to-encrypt-string-in-java
– Guerra