I am using the Ethereum keystore file format to encrypt any other data such a plain text or JSON.
Here is an example of pseudocode of the implementation:
MY_PASSWORD = "VeryDifficultPasword432131!@!#%"
MY_TEXT = "This is my secret text"
iv = randomBytes(16)
kdfparams = {
dklen: 32,
n: 262144,
r: 1,
p: 8,
salt: randomBytes(32)
}
key = scrypt(MY_PASSWORD, kdfparams)
ciphertext = encryptCipheriv("aes-128-ctr", MY_TEXT, key, iv)
mac = sha3(key[16:32], ciphertext)
With this code I can generate the keystore file that looks like this:
{
"crypto" : {
"cipher" : "aes-128-ctr",
"cipherparams" : {
"iv" : "83dbcc02d8ccb40e466191a123791e0e"
},
"ciphertext" : "d172bf743a674da9cdad04534d56926ef8358534d458fffccd4e6ad2fbde479c",
"kdf" : "scrypt",
"kdfparams" : {
"dklen" : 32,
"n" : 262144,
"r" : 1,
"p" : 8,
"salt" : "ab0c7876052600dd703518d6fc3fe8984592145b591fc8fb5c6d43190334ba19"
},
"mac": "102aedfe8c308c735a76295f9908d9b7de40fb1c07f2f71e05de7a0b2f323a12"
}
}
This is the standard file that store millions of dollars of people in Ethers.
Questions:
- Is this secure?
- If not, why?
- If yes, is also secure encrypting plain text or JSON (no hexadecimal format)?
- Is secure to set a smaller
kdfparams.n
?
Edit: To clarify why I'm asking this is because another person said this:
This is not secure at all. This implementation contains many basic errors. One example is using an insecure random number generator, which utterly destroys the security of the encryption. Another is using AES-128-CTR without authentication.