Home > Mobile >  Decryptor Function gives output a list of numbers instead of string
Decryptor Function gives output a list of numbers instead of string

Time:10-23

I have a encryptor and decryptor function in my code as you can see below, it encrypts and decrypts a plain string using same algorithm Rijndael-js with the help of crypto and PKCS7-Padding packages. The encryptor function is working fine but a decryptor function should give me the original plain string which was encrypted with encryptor function.

Here is my code

import RijndaelManaged from 'rijndael-js'
import padder from 'pkcs7-padding'
import crypto from 'crypto'

let plainText, padded, keyData, ivData, cipher, encryptedData

const encryptor = async () => {
    plainText = Buffer.from('Here is my plain text', 'utf8')
    padded = padder.pad(plainText, 32)
    keyData = crypto.randomBytes(32)
    ivData = crypto.randomBytes(32)
    cipher = new RijndaelManaged(keyData, 'cbc')
    encryptedData = cipher.encrypt(padded, 256, ivData)
    console.log(encryptedData)
}
encryptor()

const decryptor = () => {
    const key = keyData
    const iv = ivData
    const encrypted = encryptedData
    const decipher = new RijndaelManaged(key, 'cbc')
    const decryptedPadded = decipher.decrypt(encrypted, 256, iv)
    const decrypted = padder.unpad(decryptedPadded, 32)
    const clearText = decrypted.toString('utf-8')
    console.log(clearText)
}
decryptor()

Output of encryptor function

[
  246, 222, 109, 114, 198, 100,  11,  68,
  181, 180,   9, 250, 188, 228, 215, 150,
   48, 141, 225,  32,   0, 118,  98,  54,
  235, 104, 196,   1, 146, 229, 122, 137
]

Output of decryptor function

72,101,114,101,32,105,115,32,109,121,32,112,108,97,105,110,32,116,101,120,116,11,11,11,11,11,11,11,11,11,11,11

Expected output of decryptor function

Here is my plain text

I tried to console.log the typeof clearText variable it was string

CodePudding user response:

const clearText = Buffer.from(decrypted).toString()
  • Related