A
even if, a key is generated using the same password used in
Shouldn't another key be two distinct keys?No, all algorithms involved in generating the cryptographic key(PBKDF2, SHA-2) are https://pt.wikipedia.org/wiki/Algoritmo_determin%C3%ADstico . If that wasn't the answer I expected, keep reading.I analyzed your code, and in short, the general picture is:Create https://en.wikipedia.org/wiki/Symmetric-key_algorithm Encrypt content using the AES algorithm with the derived keyExport and Import key in JSON Web Key(JWK) formatDecrypt contentIt seems the biggest problem is to understand how to use https://en.wikipedia.org/wiki/PBKDF2 .
The creation of the key occurred this way:A password is the naked and raw key (keyconst WebCryptoGenerateKey = (password) => {
return crypto.subtle.importKey(
'raw',
UTILS.convertStringToArrayBuffer(password),
{
name: 'PBKDF2'
},
false, // PBKDF2 don't exportable
[ 'deriveKey', 'deriveBits' ]
)
}
This key is created a derived key using the algorithm https://en.wikipedia.org/wiki/PBKDF2 , using cryptographic hash function https://pt.wikipedia.org/wiki/SHA-2 (SHA-2 256bits), with 100,000 iterations(iterations), using as https://pt.wikipedia.org/wiki/Sal_(criptografia) (Jump) a password:const AES_GCM = (CryptoKey, opts) => {
return crypto.subtle.deriveKey(
{
name: 'PBKDF2',
salt: new Uint8Array(UTILS.convertStringToArrayBuffer(opts.password)),
iterations: 100000, // mobile = 100 000, desktop.32bit = 1 000 000, desktop.64bit = 10 000 000
hash: 'SHA-256'
},
CryptoKey,
{
name: 'AES-GCM',
length: 256
},
opts.export, // Extractable is set to false so that underlying key details cannot be accessed.
[ 'encrypt', 'decrypt', 'wrapKey', 'unwrapKey' ]
)
}
In this same step, it is specified that the derived key should be formatted in the size of 256bits, for use in https://pt.wikipedia.org/wiki/Advanced_Encryption_Standard in operation mode https://en.wikipedia.org/wiki/Galois/Counter_Mode : {
name: 'AES-GCM',
length: 256
},
The user-informed password is a naked key, since it falls into the wrong hands, the safe secret is discovered. When this key is derived, it is added to it an armor, thus making it difficult to use it in the safe. PBKDF2 puts several steel layers in this armor, iterations is the property that defines the number of layers. O salt in PBKDF2, represents the weak point of his defense, which can rust his armor. If every key has the same weakness, once the enemy discovers the fault, he will discover all the keys. So it is important to have a different salt for each key.Returning to your code, the ideal would be to change the salt to something like:salt: crypto.getRandomValues(new Uint8Array(8)),
As for your doubt, the same password generate the same key. Take into consideration, that once the value of the salt, or iterations, the resulting key will be totally different. So in this case, the answer is Yes.