I’m not an expert on cryptography, but I read a lot about it and I’m very interested. Your question is a little broad and so is the subject. If we talk in cryptography there are several standards like: AES, RSA, Openpgp... There are also Hash patterns of a pathway like MD5, SHA-1, SHA-2, SHA-256...
But taking the example of Keepass (which I use). It uses the AES standard, according to the official website:
Keepass Supports the Advanced Encryption Standard (AES, Rijndael) and
the Twofish Algorithm to Encrypt its password Databases. Both of These
Ciphers are regarded as being very Secure. AES e.g. became effective
as a U.S. Federal Government standard and is Approved by the National
Security Agency (NSA) for top secret information.
What does that tell us?
This tells us that Keepass uses the AES standard to encrypt the password base and other fields you store in the kdbx files. But if you use Keepass, you must have noticed that you can copy the passwords, right? So they need to be encrypted and decrypted back. AES allows this.
With a key (I believe Keepass that sets the key by default and are 256-bit keys) it encrypts and decrypts the password database.
About Hash
Now let’s go to the next part. Keepass also asks for a master password, the so-called Master Password. About this Master Password the official website tells us:
SHA-256 is used as password hash. SHA-256 is a 256-bit
cryptographically Secure one-way hash Function. Your master password
is hashed using this Algorithm and its output is used as key for the
Encryption Algorithms. In Contrast to Many other hashing Algorithms,
no Attacks are known yet Against SHA-256.
Here is already a more common situation that we find almost all over the internet. We set a password to access the content, in this case the kdbx file. If the password is correct, access, otherwise deny access. But this password is not saved in a "full text" way because this would be a serious security breach. Then a password hash is created using the SHA-256 algorithm and then saved.
Once the hash is created, there is no return. That is, there is no recovery. So when you lose your password, most websites will create a new password. If the site sends your password in the email, rest assured, that site is unsafe.
About weak and strong passwords
The issue of weak or strong passwords is for protection against brute force attacks, dictionaries, raibow Tables... 'Cause it gets easier to guess easy passwords.
See more about brute force attacks here.
About the keys
They are simple strings of bytes. If they are bytes they can be stored anywhere: HD, Database, memory, USB sticks... It does matter if the key is 8-bit or 4096-bit. The higher the key, the more security (the hacker will spend much more time trying to guess a larger key), but more processing time will be spent on encrypting/decrypting information.
Practical example with Java
Although I gave an example of Java code on the web, I gave an adapted and simplified one. It’s working. It encrypts and decrypts a phrase using AES. Take a look. I think it’s easy to understand.
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Crypt {
// Chave
private static byte[] key = { 0x74, 0x68, 0x69, 0x73, 0x49, 0x73, 0x41,
0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x4b, 0x65, 0x79 };
public static void main(String[] args) {
String info = "Frase a ser criptografada";
// Criptografa
byte[] fraseCriptografada = Crypt.encrypt(info);
for(byte b : fraseCriptografada) {
System.out.print(b);
}
System.out.println();
// Decriptografa a Frase
String fraseDecript = Crypt.decrypt(fraseCriptografada);
System.out.println(fraseDecript);
}
public static byte[] encrypt(String strToEncrypt) {
byte[] encryptedString = null;
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
encryptedString = cipher.doFinal(strToEncrypt.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return encryptedString;
}
public static String decrypt(byte[] byteToDecrypt) {
String decryptedString = null;
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
decryptedString = new String(cipher.doFinal(byteToDecrypt));
} catch (Exception e) {
e.printStackTrace();
}
return decryptedString;
}
}
Console output from the above example:
-82-704594-7111794-17-59262-52-9734-27110-1143353-74-8794-8710211643-16-1111178741103
Frase a ser criptografada
Thanks for the answer. I understood that a 256bit hash is created with the Master Password and this hash is used with key for the AES256 algorithm. But is this key (the result of the hash) stored somewhere or generated each time you enter the Master Password? If stored, it would be a security breach?
– Yuri Castilho Wermelinger
Bigown Vi what you wrote, you can erase if you want...
– Yuri Castilho Wermelinger
@Yuricastilhowermelinger, I don’t believe that the password (hash) is used as a key for encryption. Now, if the AES key is generated every time you open kdbx and closes and recreates everything, just look at the code itself and see how it works. But that would be possible yes.
– humungs
I understood that the hash is the key to encryption because in your reply there is a mention of a text from the official website that says "Your master password is hashed using this Algorithm and its output is used as key for the Encryption Algorithms."
– Yuri Castilho Wermelinger
@Yuricastilhowermelinger, you are correct. Keepass uses the Master Password hash as the AES key. I searched a little further and confirmed. "In order to generate the 256-bit key for the block Ciphers, the Secure Hash Algorithm SHA-256 is used. This Algorithm compresses the user key provided by the user (consisting of password and/or key file) to a Fixed-size key of 256 bits. This Transformation is one-way, i.e. it is computationally infeasible to invert the hash Function or find a Second message that compresses to the same hash." http://keepass.info/help/base/security.html
– humungs