0
我想加密使用webcrypto并在php中使用openssl进行解密。要解密在PHP我需要一个身份验证标记。有没有一种方法可以从密文中提取它,或者我可以通过任何其他方式来获取它?在webcrypto加密是:如何从webcrypto AES-GCM加密获取标签
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
字节? –我如何从var“result”中切片? – gerteb
在php中,标签是一个长度为16的utf-8字符串。如果我在JavaScript中加密了一个长度为10的字符串,我得到了一个长度为26的加密的utf-8字符串。我是否最终将16个字符切片并使用他们作为标签在PHP? – gerteb