使用JavaScript和WebCrypto API(没有任何外部库),使用从用户提交的密码派生的密钥加密字符串的最佳方法是什么?
这里有一些代码,其中密钥不是派生的,而是由generatekey()
函数生成的。目标是加密字符串,然后解密它以验证我们返回原始字符串:
var secretmessage = "";
var password = "";
var key_object = null;
var promise_key = null;
var encrypted_data = null;
var encrypt_promise = null;
var vector = window.crypto.getRandomValues(new Uint8Array(16));
var decrypt_promise = null;
var decrypted_data = null;
function encryptThenDecrypt() {
secretmessage = document.getElementById("secretmessageField").value; // some string to encrypt
promise_key = window.crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 128
},
false,
["encrypt", "decrypt"]
);
promise_key.then(function(key) {
key_object = key;
encrypt_data();
});
promise_key.catch = function(e) {
alert("Error while generating key: " + e.message);
}
}
function encrypt_data() {
encrypt_promise = window.crypto.subtle.encrypt({name: "AES-GCM", iv: vector}, key_object, convertStringToArrayBuffer(secretmessage));
encrypt_promise.then(
function(result) {
encrypted_data = new Uint8Array(result);
decrypt_data();
},
function(e) {
alert("Error while encrypting data: " + e.message);
}
);
}
function decrypt_data() {
decrypt_promise = window.crypto.subtle.decrypt({name: "AES-GCM", iv: vector}, key_object, encrypted_data);
decrypt_promise.then(
function(result){
decrypted_data = new Uint8Array(result);
alert("Decrypted data: " + convertArrayBuffertoString(decrypted_data));
},
function(e) {
alert("Error while decrypting data: " + e.message);
}
);
}
function convertStringToArrayBuffer(str) {
var encoder = new TextEncoder("utf-8");
return encoder.encode(str);
}
function convertArrayBuffertoString(buffer) {
var decoder = new TextDecoder("utf-8");
return decoder.decode(buffer);
}
适用于所有最近的浏览器。
现在我尝试修改encryptThenDecrypt()
功能,以便从用户提交的密码中获取密钥:
function encryptThenDecrypt() {
secretmessage = document.getElementById("secretmessageField").value; // some string to encrypt
password = document.getElementById("passwordField").value; // some user-chosen password
promise_key = window.crypto.subtle.importKey(
"raw",
convertStringToArrayBuffer(password),
{"name": "PBKDF2"},
false,
["deriveKey"]
);
promise_key.then(function(importedPassword) {
return window.crypto.subtle.deriveKey(
{
"name": "PBKDF2",
"salt": convertStringToArrayBuffer("the salt is this random string"),
"iterations": 500,
"hash": "SHA-256"
},
importedPassword,
{
"name": "AES-GCM",
"length": 128
},
false,
["encrypt", "decrypt"]
);
});
promise_key.then(function(key) {
key_object = key;
encrypt_data();
});
promise_key.catch = function(e) {
alert("Error while importing key: " + e.message);
}
}
失败了。错误消息是:
CryptoKey doesn't match AlgorithmIdentifier
A parameter or an operation is not supported by the underlying object
key.algorithm does not match that of operation
必须要修理一些简单的东西,但我无法理解。任何帮助将不胜感激。
答案 0 :(得分:3)
您的代码中有一点错误。与crypto无关,只是承诺。
Promise在调用.then()
方法时不会更新状态,而是返回新的promise。在代码中看到,您正在通过调用密钥派生函数来丢弃产生的承诺。然后,在加密数据时,您将重用密码承诺,而不是密钥。
您应该将密钥派生中产生的承诺保存在一个新变量中:
let promise_derived_key = promise_key.then(function(importedPassword) {
return window.crypto.subtle.deriveKey(
// [...]
);
});
promise_derived_key.then(function(key) {
// [...]
或将调用链接到.then()
:
promise_key = window.crypto.subtle.importKey(
// [...]
).then(function(importedPassword) {
return window.crypto.subtle.deriveKey(
// [...]
);
});
promise_key.then(function(key) {
// [...]
顺便说一下,你会想要使用很多比500更多的PBKDF2迭代。(info)