我发现使用aes-256-gcm,iv和TAG解密字符串的困难方法。 这是我得到的参数。
$password = 'code1234';
$data = 'f4oGPQaf9ZolRc9oh8VLRiJWK2B35tkHq9L6WvrE+jbjS1mfi0fJB7re+0kVM2RRLiAEPBSFtIQzlJ5055yRlntIZwwpRvAN1cUBS0ze3hb+FrcO43vtBuuqftDw0exfmorPM5SOaKj3KtEQjB2ypwtbnQNCtegNizs6niqAdvs/XztlZPbq08Y=';
$ivstring = 'D7GVFFLn+K0sNj+YRskGTw==';
$tagstring = '9GSCia0QDujWTn3zpIRbCw==';
我使用此功能解密
openssl_decrypt($data, 'aes-256-gcm', $password, OPENSSL_RAW_DATA, $ivstring, $tagstring);
但是它总是返回以下错误
设置AEAD密码解密的标签失败
如何使用IV和TAG在PHP中解密aes-256-gcm
更新: 加密来自使用节点js的主应用程序。我试图在nodejs应用程序中解密,它正常工作。我试图将解密函数转换为PHP,但始终会出错。这里的加密和解密脚本功能
const crypto = require('crypto');
// Demo implementation of using `aes-256-gcm` with node.js's `crypto` lib.
const aes256gcm = (key) => {
const ALGO = 'aes-256-gcm';
const aes_password = key || 'code1234';
const aes_key = crypto.createHash('md5').update(aes_password, 'utf-8').digest('hex').toUpperCase();
// encrypt returns base64-encoded ciphertext
const encrypt = (str) => {
const iv = new Buffer(crypto.randomBytes(16), 'utf8');
const cipher = crypto.createCipheriv(ALGO, aes_key, iv);
let enc = cipher.update(str, 'utf8', 'base64');
enc += cipher.final('base64');
const ivString = iv.toString('base64');
const authTagString = cipher.getAuthTag().toString('base64')
return [enc, ivString, authTagString];
};
// decrypt decodes base64-encoded ciphertext into a utf8-encoded string
const decrypt = (enc, ivString, authTagString) => {
const iv = new Buffer(ivString, 'base64');
const authTag = new Buffer(authTagString, 'base64');
//console.log(iv);
// console.log(authTag);
const decipher = crypto.createDecipheriv(ALGO, aes_key, iv);
// console.log(iv);
decipher.setAuthTag(authTag);
let str = decipher.update(enc, 'base64', 'utf8');
console.log(str);
str += decipher.final('utf8');
return str;
};
return {
encrypt,
decrypt,
};
};
module.exports = aes256gcm;