gpt4 book ai didi

javascript - 使用 AES key 包装 RSA 私钥然后解包

转载 作者:行者123 更新时间:2023-12-03 11:24:00 25 4
gpt4 key购买 nike

我一直在尝试用密码保护使用 Web Cryptography API 生成的 RSA 私钥。为此,

  1. 我首先生成一个 RSA key 对
  2. 然后我从密码中导出一个 AES 对称 key
  3. 然后我用步骤 2 中的 AES key 包装步骤 1 中的 RSA 私钥。
  4. 当我完成后,我尝试通过立即将所有这些传递给 unwrap 方法来验证它是否有效,我尝试在该方法中解包

代码如下:

<html>
<script>
function wrap(password) {
var iterations = 1000000;

// Utility function
var stringToByteArray = function(s){
if ("TextEncoder" in window) {
encoder = new window.TextEncoder;
return encoder.encode(s);
}
var result = new Uint8Array(s.length);
for (var i=0; i<s.length; i++){
result[i] = s.charCodeAt(i);
}
return result;
}

var saltBytes = stringToByteArray("NaCl");
var passphraseBytes = stringToByteArray(password);

return crypto.subtle.generateKey({
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: {name: "SHA-256"}
}, true, ["encrypt", "decrypt"]).then(function(keyPair) {
return crypto.subtle.importKey(
"raw", passphraseBytes, {name: "PBKDF2"}, false, ["deriveKey"]
).then(function(baseKey) {
return window.crypto.subtle.deriveKey(
{name: "PBKDF2", salt: saltBytes, iterations: iterations, hash: "SHA-1"},
baseKey,
{name: "AES-CBC", length: 256},
false,
["encrypt", "decrypt", "wrapKey", "unwrapKey"]
).then(function(wrapperKey) {
var iv = crypto.getRandomValues(new Uint8Array(16));
return crypto.subtle.wrapKey(
"pkcs8",
keyPair.privateKey,
wrapperKey,
{name: "AES-CBC", iv: iv }
).then(function(wrappedKey) {
return {
iv: iv,
wrapper: wrapperKey,
wrapped: wrappedKey
}
})
});
}).catch(function(err) {
console.log(err);
});
})
}


function unwrap(account) {
console.log(account);
crypto.subtle.unwrapKey(
"pkcs8",
account.wrapped,
account.wrapper,
{
name: "AES-CBC",
iv: account.iv
},
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: {name: "SHA-256"}
},
true,
['decrypt', 'encrypt']
).then(function(privateKey) {
console.log("unwrapped = ", privateKey);
}).catch(function(e) {
console.log(e)
})
}

// Finally I call "wrap" and then "unwrap"
wrap("password").then(unwrap)

</script>
</html>

但是代码不起作用。包装代码不会引发任何错误并且似乎会生成 key (尽管我不知道这些是否有效)。但是当我尝试运行 unwrapKey 方法时,出现以下错误:

DOMException: Cannot create a key using the specified key usages.

在过去的 24 小时里,我一直在苦恼,因为我无法让它发挥作用。谁能发现问题?这是一段完全独立的代码,因此您可以通过复制并粘贴到 HTML 文件并在浏览器中打开来试用它。

最佳答案

您尝试做的是用对称 key 包装公钥/私钥,这不是包装/解包的预期工作方式。

  • wrapKey, allowing the key to wrap a symmetric key for usage (transfer, storage) in unsecure environments.
  • unwrapKey, allowing the key to unwrap a symmetric key for usage (transfer, storage) in unsecure environments.

您可以包装/解包对称 key ,但不能包装/解包公钥/私钥对(或 key ),因为包装/解包可能希望包装的 key 是对称 key 而不是非对称 key 。

The SubtleCrypto.wrapKey() method returns a Promise of a wrapped symmetric key for usage (transfer, storage) in unsecure environments. The wrapped buffer returned is in the format given in parameters, and contained the key wrapped by the give wrapping key with the given algorithm.

The SubtleCrypto.unwrapKey() method returns a Promise of a CryptoKey corresponding to the wrapped key given in parameter.

关于javascript - 使用 AES key 包装 RSA 私钥然后解包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48038395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com