function StreamWrite(algo, keylen, message, n, len) { var written = n * len; var bits = written * 8; var kbits = bits / (1024); var privateKey = RSA_PrivatePem[keylen]; var publicKey = RSA_PublicPem[keylen]; for (var i = 0; i < n; i++) { var enc = crypto.privateEncrypt(privateKey, message); crypto.publicDecrypt(publicKey, enc); } bench.end(kbits); }
var verifyWithKey = function(pubKey) { var enc_hmac_digest = new Buffer(message.hmac_from,'base64'); var dec_hmac_digest = ""; try { dec_hmac_digest = crypto.publicDecrypt({key:pubKey,padding:constants.RSA_PKCS1_NO_PADDING},enc_hmac_digest).toString(); } catch(e) {console.log(e,enc_hmac_digest,pubKey);} // Test if HMAC is correct (are we recipient?) const hmac = crypto.createHmac('sha256', edichain.config.fromAddress.toLowerCase()); hmac.update(edichain.config.pubRegistrarAddress.toLowerCase()); var hmac_digest=hmac.digest('base64'); if(hmac_digest==dec_hmac_digest) { message.verifiedSender=true; } else { message.verifiedSender=false; } if(cb) cb(message); }
socket.on('message', function (data) { var incomingMessage = JSON.parse(data.toString('utf8')); switch (incomingMessage.code) { case 100: //获取服务器公钥 RSAPubKey = new Buffer(incomingMessage.RSAPubKey, 'base64').toString('utf8'); //获取客户端ECDH的PublicKey var ECDHClientPublicKey = ecdh.getPublicKey(); //将客户端的ECDHClientPublicKey用RSA公钥加密 socket.send(JSON.stringify({ code: 101, PublicKey: crypto.publicEncrypt(RSAPubKey, ECDHClientPublicKey).toString('base64'), timestamp: new Date().getTime() })); console.log(colors.green("Sended ECDHClientPublicKey: ") + ECDHClientPublicKey.toString('base64')); break; case 101: //解密服务器ECDH的PublicKey var ECDHServerPublicKey = crypto.publicDecrypt(RSAPubKey, new Buffer(incomingMessage.PublicKey, 'base64')).toString("base64"); console.log(colors.green('Recive a ECDHServerPublicKey from server: ') + ECDHServerPublicKey); DESKEY = ecdh.computeSecret(ECDHServerPublicKey, 'base64', 'base64'); //获取DESKEY console.log(colors.green('Generator DESKEY: ') + DESKEY); socket.send(DESCrypto.Cipher({ code: 10, msg: "hello server", DESKEY: DESKEY }, DESKEY)); break; case 102: var MessageDecipher = DESCrypto.Decipher(incomingMessage.data, DESKEY); var hash = MessageDecipher[1].hash; var data = MessageDecipher[0]; if (crypto.createHash('sha512').update(JSON.stringify(data)).digest('base64') == hash) { console.log(colors.green("Verify hash success.")); if (data.DESKEY == DESKEY) { console.log(colors.green("Verify DESKEY success.")); switch (data.code) { case 10: console.log(colors.green("MSG from server: ") + data.msg); // socket.send(MessageCipherfun({ code: 10, msg: "hello server", DESKEY: DESKEY })); break; } } else { console.log(colors.green("Verify DESKEY success.")); } } else { console.log(colors.green("Verify hash success.")); } break; } });
//这是文件的解密 function decryptStr(cryptBuffer) { var result = crypto.publicDecrypt(public_key,cryptBuffer); console.log('解密后的buffer = ',result); return result; }
assert.throws(() => { crypto.publicDecrypt({ key: rsaKeyPemEncrypted, passphrase: [].concat.apply([], Buffer.from('password')) }, encryptedBuffer); }, decryptError);
passphrase: 'password' }, bufferToEncrypt); decryptedBufferWithPassword = crypto.privateDecrypt({ key: rsaKeyPemEncrypted, passphrase: 'password' }, encryptedBuffer); assert.strictEqual(decryptedBufferWithPassword.toString(), input); encryptedBuffer = crypto.privateEncrypt({ key: rsaKeyPemEncrypted, passphrase: Buffer.from('password') }, bufferToEncrypt); decryptedBufferWithPassword = crypto.publicDecrypt({ key: rsaKeyPemEncrypted, passphrase: Buffer.from('password') }, encryptedBuffer); assert.strictEqual(decryptedBufferWithPassword.toString(), input); encryptedBuffer = crypto.publicEncrypt(certPem, bufferToEncrypt); decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer); assert.strictEqual(decryptedBuffer.toString(), input); encryptedBuffer = crypto.publicEncrypt(keyPem, bufferToEncrypt); decryptedBuffer = crypto.privateDecrypt(keyPem, encryptedBuffer); assert.strictEqual(decryptedBuffer.toString(), input); encryptedBuffer = crypto.privateEncrypt(keyPem, bufferToEncrypt);
exports.publicDecrypt = function(public_str, data) { return crypto.publicDecrypt(public_str, data); };
assert.throws(() => { crypto.publicDecrypt({ key: rsaKeyPemEncrypted, passphrase: Buffer.from('wrong') }, encryptedBuffer); }, decryptError);