I have a function that decrypt AES-GCM encrypted text in Javascript. I would like to make it work in Php, so I can decipher messages sent by Javascript.
I have two functions: the first one imports and derives a password to a CryptoKey (with salt), the second one uses the CryptoKey to decipher the encrypted message.
async importAESGCMKeyFromPassword(password, salt){
const enc = new TextEncoder()
const keyMaterial = await window.crypto.subtle.importKey(
'raw',
enc.encode(password),
{ name: 'PBKDF2' },
false,
['deriveBits', 'deriveKey']
)
return window.crypto.subtle.deriveKey(
{
'name': 'PBKDF2',
salt: Buffer.from(salt, 'base64'),
'iterations': 1000,
'hash': 'SHA-256'
},
keyMaterial,
{ 'name': 'AES-GCM', 'length': 256},
true,
[ 'encrypt', 'decrypt' ]
)
},
async decryptAESGCM(key, iv, message){
if( typeof message != 'string' ) return null
let result
try{
result = await window.crypto.subtle.decrypt(
{
name: 'AES-GCM',
iv: Buffer.from(iv, 'base64')
},
key,
Buffer.from(message, 'base64')
)
}catch(error){
return null
}
const dec = new TextDecoder()
return dec.decode(result)
},
How can I convert them in php code ?
I tried this way https://stackoverflow.com/questions/65930881/decrypting-a-string-encrypted-by-php-with-aes256-gcm-using-openssl-in-c-sharp But it does not seems to work...