[GH-ISSUE #320] 能支持AES-128-GCM加密方式吗 #280

Closed
opened 2026-03-03 11:42:10 +03:00 by kerem · 1 comment
Owner

Originally created by @DJPY on GitHub (Sep 19, 2025).
Original GitHub issue: https://github.com/Finb/Bark/issues/320

现在测试还不行

Originally created by @DJPY on GitHub (Sep 19, 2025). Original GitHub issue: https://github.com/Finb/Bark/issues/320 现在测试还不行
kerem closed this issue 2026-03-03 11:42:10 +03:00
Author
Owner

@Finb commented on GitHub (Sep 19, 2025):

这个加密模式配置有问题, 暂时别用,之后会修复一下, 去掉pkcs7填充

如果要加密,要注意 authTag 不能拼到密文中,使用分离模式,最后做一下pkcs7填充

const crypto = require('crypto');

// Manual PKCS#7 padding function
function addPKCS7Padding(data, blockSize = 16) {
    const buffer = Buffer.from(data, 'utf8');
    const paddingLength = blockSize - (buffer.length % blockSize);
    const padding = Buffer.alloc(paddingLength, paddingLength);
    return Buffer.concat([buffer, padding]);
}

const key = Buffer.from('0123456789012345', 'utf8'); // 16 bytes for AES-128
const iv = Buffer.from('012345678901', 'utf8'); // 12 bytes for AES-GCM nonce
const plaintext = 'hello';

// Add PKCS#7 padding manually
const paddedPlaintext = addPKCS7Padding(plaintext);
console.log('Padded plaintext (hex):', paddedPlaintext.toString('hex'));

const cipher = crypto.createCipheriv('aes-128-gcm', key, iv);
let encrypted = cipher.update(paddedPlaintext, null, 'base64');
encrypted += cipher.final('base64');

console.log('Ciphertext (base64, manually padded):', encrypted);
<!-- gh-comment-id:3310415727 --> @Finb commented on GitHub (Sep 19, 2025): 这个加密模式配置有问题, 暂时别用,之后会修复一下, 去掉pkcs7填充 如果要加密,要注意 authTag 不能拼到密文中,使用分离模式,最后做一下pkcs7填充 ```js const crypto = require('crypto'); // Manual PKCS#7 padding function function addPKCS7Padding(data, blockSize = 16) { const buffer = Buffer.from(data, 'utf8'); const paddingLength = blockSize - (buffer.length % blockSize); const padding = Buffer.alloc(paddingLength, paddingLength); return Buffer.concat([buffer, padding]); } const key = Buffer.from('0123456789012345', 'utf8'); // 16 bytes for AES-128 const iv = Buffer.from('012345678901', 'utf8'); // 12 bytes for AES-GCM nonce const plaintext = 'hello'; // Add PKCS#7 padding manually const paddedPlaintext = addPKCS7Padding(plaintext); console.log('Padded plaintext (hex):', paddedPlaintext.toString('hex')); const cipher = crypto.createCipheriv('aes-128-gcm', key, iv); let encrypted = cipher.update(paddedPlaintext, null, 'base64'); encrypted += cipher.final('base64'); console.log('Ciphertext (base64, manually padded):', encrypted); ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/Bark#280
No description provided.