0

I have to make cryptograph to using Node.js on tonight. I have tried but i can't.

what is wrong ? Thanks to your help!!!

function encrypt(data,j) {
    for(var i = 0, length = data.length; i<length; i++) {
         j = data.charCodeAt(i);
        //console.log(j);
        String.fromCharCode(j);
        process.stdout.write(j);
    }
    return j;
}

function decrypt(data) {
    return data;
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

process.stdout.write('암호화할 문장을 입력(INPUT) : ' );

process.stdin.on('data',function(data,j) {
    //data = data.trim();
    process.stdout.write('평문 (uncoded) :' + data);
    process.stdout.write('암호문(encrypt) :');
    encrypt();
    process.stdout.write('복호문(decrypt) :');

    process.exit(1);
    });

Microsoft Windows [Version 6.2.9200]
(c) 2007 Microsoft Corporation. All rights reserved.

C:\Users\Minji>cd ..

C:\Users>cd ..

C:\>cd workspace

C:\workspace>node 3112minji

암호화할 문장을 입력(INPUT) : abc

평문 (uncoded) :abc

암호문(encrypt) :

C:\workspace\3112minji.js:2

        for(var i = 0, length = data.length; i<length; i++) {
                                    ^
TypeError: Cannot read property 'length' of undefined

    at encrypt (C:\workspace\3112minji.js:2:30)

    at ReadStream.<anonymous> (C:\workspace\3112minji.js:24:2)

    at ReadStream.EventEmitter.emit (events.js:95:17)

    at ReadStream.<anonymous> (_stream_readable.js:720:14)

    at ReadStream.EventEmitter.emit (events.js:92:17)

    at emitReadable_ (_stream_readable.js:392:10)

    at emitReadable (_stream_readable.js:388:5)

    at readableAddChunk (_stream_readable.js:150:9)

    at ReadStream.Readable.push (_stream_readable.js:113:10)

    at TTY.onread (net.js:511:21)


C:\workspace>
0

2 Answers 2

1

You never pass anything to your encrypt method, so when it tries to access data it crashes.

encrypt(data, j);

Sign up to request clarification or add additional context in comments.

Comments

0

You should do the next:

process.stdin.on('data',function(data,j) {
    //data = data.trim();
    process.stdout.write('평문 (uncoded) :' + data);
    process.stdout.write('암호문(encrypt) :');
    encrypt( data,j ); // Pass the parameters to the encrypt function.
    process.stdout.write('복호문(decrypt) :');

    process.exit(1);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.