Reference:
Last active
March 6, 2022 07:44
-
-
Save WJWang/1855ce92c2b1bac1313e1d1484297926 to your computer and use it in GitHub Desktop.
QR code encryption code for Taiwanese electronic invoice in nodejs (QRCode電子發票AES加密)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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