gpt4 book ai didi

javascript - 如何从 webcrypto AES-GCM 加密中获取标签

转载 作者:行者123 更新时间:2023-11-30 11:37:29 27 4
gpt4 key购买 nike

我正在尝试使用 webcrypto 加密并在 php 中使用 openssl 解密。要在 php 中解密,我需要一个身份验证标签。有没有一种方法可以从密文中提取它,或者我可以通过其他任何方式获取它? webcrypto 中的加密是:

var stringToArrayBuffer=function(str){
var bytes = new Uint8Array(str.length);
for (var iii = 0; iii < str.length; iii++){
bytes[iii] = str.charCodeAt(iii);
}
return bytes;
}
var arrayBufferToString=function(buffer){
var str = "";
for (var iii = 0; iii < buffer.byteLength; iii++){
str += String.fromCharCode(buffer[iii]);
}
return str;
}
var vector=window.crypto.getRandomValues(new Uint8Array(12)),sendData={},key;
sendData.iv=btoa(arrayBufferToString(new Uint8Array(vector));

crypto.subtle.generateKey({name: "AES-GCM",length: 256,},false,["encrypt", "decrypt"]).then(function(oKey){
console.log(oKey);
key=oKey;
sendData.key=btoa(arrayBufferToString(new Uint8Array(key));
}).catch(function(err){console.error("generateKey:",err);});

crypto.subtle.encrypt({name: "AES-GCM", iv: vector,tagLength: 128,},key,stringToArrayBuffer(input)).then(
function(result){
console.log('encrypted',new Uint8Array(result))
encryptedDataString=arrayBufferToString(new Uint8Array(result));
sendData.dataString=btoa(arrayBufferToString(new Uint8Array(result)));
}
).catch(function(err){console.error("encryptData error:",err);});

最佳答案

需要从加密数据末尾切出tagLength字节

/**
* Gets tag from encrypted data
*
* @param {ArrayBuffer} encrypted Encrypted data
* @param {number} tagLength Tag length in bits. Default 128 bits
* @returns {ArrayBuffer}
*/
function GetTag(encrypted, tagLength) {
if (tagLength === void 0) tagLength = 128;
return encrypted.slice(encrypted.byteLength - ((tagLength + 7) >> 3))
}

关于javascript - 如何从 webcrypto AES-GCM 加密中获取标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43846560/

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