Skip to content

Instantly share code, notes, and snippets.

@WJWang
Last active March 6, 2022 07:44
Show Gist options
  • Save WJWang/1855ce92c2b1bac1313e1d1484297926 to your computer and use it in GitHub Desktop.
Save WJWang/1855ce92c2b1bac1313e1d1484297926 to your computer and use it in GitHub Desktop.
QR code encryption code for Taiwanese electronic invoice in nodejs (QRCode電子發票AES加密)
const crypto = require('crypto');
const QRCodeAesEncodeKey = Buffer.from(process.env.QRCODE_AES_KEY, 'hex');
const EINVOICE_IV = Buffer.from('Dt8lyToo17X/XkXaQvihuA==', 'base64');
const ALGORITHM = 'aes-128-cbc';
module.exports = {
qrcodeEncryptor: (str) => {
const cipher = crypto.createCipheriv(ALGORITHM, QRCodeAesEncodeKey, EINVOICE_IV);
const encodedDataBuf = Buffer.concat([
cipher.update(str),
cipher.final()
]);
return Buffer.from(encodedDataBuf).toString('base64').padEnd(0x18);
},
qrcodeDecryptor: (encryptStrWith64) => {
const encryptdata = Buffer.from(encryptStrWith64, 'base64');
const decipher = crypto.createDecipheriv(ALGORITHM, QRCodeAesEncodeKey, EINVOICE_IV);
const decodedBuf = Buffer.concat([
decipher.update(encryptdata),
decipher.final()
]);
return Buffer.from(decodedBuf).toString();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment