Frage

So I'm making a simple steganography tool (encrypting messages within images) and exposing it as a web service via Node.js. I am very new to Javascript and Node.js in particular. The app first converts a text string into a binary string by changing each character into an 8-bit ASCII encoding, resulting in one large binary string. I then encrypt the message within the pixels. Even values of pixels represent 0s from the binary, and odd values represent 1s. The end of the string is marked as 3 pixels of value 100 in a row (this is temporary, until I figure out a better way to mark the end). I'm using a node.js library called 'pngjs' that gives me pixel-level access to png images.

So I have a problem with the decodeMessage function. It builds up the string message, and is then meant to return it, however the return call at the end results in undefined.

How can I fix it?

Thanks in advance for the help!

function encodeMessage(image, mes) {

    var message = mes;

    var fs = require('fs'),
    PNG = require('pngjs').PNG;

    fs.createReadStream(image)
    .pipe(new PNG({
        filterType: 4
    }))
    .on('parsed', function() {

        for (var y = 0; y < this.height; y++) {
            for (var x = 0; x < this.width; x++) {
                var idx = (this.width * y + x);// << 2;
                //console.log(idx); 
                if (idx < message.length) {

                    var item = message.charAt(idx);

                    /* if the character in the encoded string is 0 */
                    if (item == 0) {
                        /* if the pixel is odd, we want it to be even */
                        if (this.data[idx] % 2 == 1) {
                        /* if the pixel is 0, add 1 to it */
                        if (this.data[idx] == 0) {
                            this.data[idx] = this.data[idx] + 1;
                        } else {
                            /* else substract 1 */
                            this.data[idx] = this.data[idx] - 1;
                        }
                        }
                    } else {
                        /* if the character in the encoded string is 1 */
                        if (this.data[idx] % 2 == 0) {
                        if (this.data[idx] == 0) {
                            this.data[idx] = this.data[idx] + 1;
                        } else {
                            this.data[idx] = this.data[idx] - 1;
                        }
                        }
                    }

                    //console.log(this.data[idx]);

                } else if (idx === message.length) {

                    /* do something to the first pixel following the end of the string */
                    this.data[idx] = 100;
                    this.data[idx+1] = 100;
                    this.data[idx+2] = 100;
                    //console.log(this.data[idx]);

                } else {

                    /* do something to the remaining pixels */

                }                  
            }
        }
        this.pack().pipe(fs.createWriteStream('encoded_' + image));
    });
}

function decodeMessage(image) {
    var message = "";

    var fs = require('fs'),
    PNG = require('pngjs').PNG;

    fs.createReadStream(image)
    .pipe(new PNG({
        filterType: 4
    }))
    .on('parsed', function() {


        dance:
        for (var y = 0; y < this.height; y++) {
            for (var x = 0; x < this.width; x++) {

                var idx = (this.width * y + x);// << 2;

                if (this.data[idx] == 100 && this.data[idx+1] == 100 && this.data[idx+2] == 100) {
                    break dance;
                } else {

                    if (this.data[idx] % 2 == 0) {
                        message += "0";
                    } else {
                        message += "1";
                    }

                }

            }
        }
        /* the message outputs correctly over here */
        console.log(message);
        //return message;
    });

    /* but the return of the variable here doesn't work */
    return message;
}

exports.encodeMessage = encodeMessage;
exports.decodeMessage = decodeMessage;
War es hilfreich?

Lösung

The parsed event is fired asynchronously, so you cannot return a value from decodeMessage.

function decodeMessage(image, cb) {

  // Code
  .on('parsed', function() {
    // Code

    console.log(message);
    cb(message);
  });
}

Then you must pass a callback to your decodeMessage function.

decodeMessage(image, function(decoded){
  // Here is the decoded data.
});

The same is true for your encodeMessage function. The function will return before encoding has finished. If you want to know when it is done, you need to pass a callback the same way.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top