Inconsistent cypher result with bas64 output encode

217 Views Asked by At

I need to cypher a textual payload, save it somewhere and after a while read it and decypher it again.

I started with some code and I noticed that there is an inconsistent behavior when I tell nodejs standard cypher to give me the output in base64. Some characters at the end of the result are missing. Changing the output encoding solve completely the problem. See the example.

import crypto from 'crypto'

const key = 'base64:Zm9vYmFy'

const payload = 'the cat is on the table'

const cypher = crypto.createCipheriv('RC4', key, null)
const decipher = crypto.createDecipheriv('RC4', key, null)

// Missing characters on result
const cypheredPayload = cypher.update(payload, 'ascii', 'base64')
const decipheredPayload = decipher.update(cypheredPayload, 'base64', 'ascii')

// All works fine
//const cypheredPayload = cypher.update(payload, 'ascii', 'ascii')
//const decipheredPayload = decipher.update(cypheredPayload, 'ascii', 'ascii')

console.log(decipheredPayload)

The result when I choose the output as base64 is the cat is on the tab (the last two characters are missing), if I switch to the version with the ascii output there is no problem.

What's happening here?

0

There are 0 best solutions below