我的应用程序密码学目前使用锻造库进行加密、解密、派生密钥和导入密钥。我最近开始阅读关于作为HTML5规范一部分的新加密特性的文章,并想做一个POC,看看它是否可行,以及性能影响。
这个功能现在看起来很难用。我甚至无法进口我的任何钥匙。
Byte编码密钥:"#a×iKº|UF?îçàÂ{ÙîµËËã-cØÊz"
B64编码密钥:"I2HXaUu6fFVGP4fu5+CJwh57HtnutcvL4y0XY9icyno="
无符号8位整数数组密钥表示:[35, 97, 215, 105, 75, 186, 124, 85, 70, 63, 135, 238, 231, 224, 137, 194, 30, 123, 30, 217, 238, 181, 203, 203, 227, 45, 23, 99, 216, 156, 202, 122]
我试着用JWK导入我的密钥
window.crypto.subtle.importKey(
"jwk", //can be "jwk" or "raw"
{ //this is an example jwk key, "raw" would be an ArrayBuffer
kty: "oct",
k: "I2HXaUu6fFVGP4fu5+CJwh57HtnutcvL4y0XY9icyno=",
alg: "A256GCM",
ext: true,
},
{ //this is the algorithm options
name: "AES-GCM",
},
false, //whether the key is extractable (i.e. can be used in exportKey)
["encrypt", "decrypt"] //can "encrypt", "decrypt", "wrapKey", or "unwrapKey"
)
.then(function(key){
//returns the symmetric key
console.log(key);
})
.catch(function(err){
console.error(err);
});但这只会带来一个永远无法解决的承诺。然后,我尝试使用'raw‘类型导入我的密钥,并将上面的arrayBuffer传递给它:
window.crypto.subtle.importKey(
"raw", //can be "jwk" or "raw"
arrayBuffer,
{ //this is the algorithm options
name: "AES-GCM",
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["encrypt", "decrypt"] //can "encrypt", "decrypt", "wrapKey", or "unwrapKey"
)
.then(function(key){
//returns the symmetric key
console.log(key);
})
.catch(function(err){
console.error(err);
});但这也只是带来了一个永远无法解决的承诺。
如何使用WebCrypto接口导入密钥?
发布于 2018-04-28 04:12:43
您的base64编码是正确的,但是JWK需要使用base64url。在该编码中,键为:I2HXaUu6fFVGP4fu5-CJwh57HtnutcvL4y0XY9icyno。
当我将k更改为该值时,我可以成功地导入您的密钥。
https://stackoverflow.com/questions/41495303
复制相似问题