0

今晚我必须制作密码才能使用 Node.js。我试过了,但我做不到。

怎么了 ?感谢您的帮助!!!

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>
4

2 回答 2

1

您永远不会将任何内容传递给您的 encrypt 方法,因此当它尝试访问数据时会崩溃。

encrypt(data, j);

于 2013-05-19T06:43:05.963 回答
0

您应该执行以下操作:

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);
});
于 2013-05-19T09:07:44.930 回答