Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var bitcore = require('bitcore-lib');
- var ECIES = require('bitcore-ecies');
- var message = "foobar";
- var testIdentity= {
- type: 'ethereum',
- display: '0xedc8774555d634ec3cdc755296382d572de2a492',
- privateKey: '1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c',
- publicKey: '644c70f5c80c0e0d950659fc3a5ca3e4079072af5b89bdc3d74bf4c43cda5202d294070d3712dc15a1667f1be8541ea5d587b9c041670ce351d031e5c9df563d',
- foreign: false
- };
- /**
- * encrypt the message with the publicKey of identity
- * @param {{privateKey: ?string, publicKey: string}} identity
- * @param {string} message
- * @return {string}
- */
- var pkiEncrypt = function(identity, message) {
- /*
- * this key is used as false sample, because bitcore would crash when alice has no privateKey
- */
- var privKey = new bitcore.PrivateKey('1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c');
- console.log("private key=>" + privKey);
- var alice = ECIES().privateKey(privKey).publicKey(new bitcore.PublicKey(identity.publicKey));
- console.log("Alice's public key=>" + alice);
- var encrypted = alice.encrypt(message);
- console.log("encrypted message by public key of Alice=>"+ encrypted.toString('hex'));
- return encrypted.toString('hex');
- };
- /**
- * decrypt the message with the privateKey of identity
- * @param {{privateKey: ?string, publicKey: string}} identity
- * @param {string} encrypted
- * @return {string} message
- */
- var pkiDecrypt = function(identity, encrypted) {
- var privKey = new bitcore.PrivateKey(identity.privateKey);
- console.log("Alice Private key=> " + privKey);
- var alice = ECIES().privateKey(privKey);
- console.log("Allice private key again=>" + alice);
- var decryptMe = new Buffer(encrypted, 'hex');
- console.log("decrypted message=>" + decryptMe);
- var decrypted = alice.decrypt(decryptMe);
- console.log("final decrypted message=>"+decrypted.toString('ascii'));
- return decrypted.toString('ascii');
- };
- var enc = pkiEncrypt(testIdentity, message);
- var dec = pkiDecrypt(testIdentity, enc);
- if(dec!=message){
- console.log("failure");
- }else{
- console.log("Success");
- }
- $ node pki_Stack_Exchange.js
- private key=>1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c
- /home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore-lib/lib/publickey.js:176
- throw new TypeError('Invalid DER format public key');
- ^
- TypeError: Invalid DER format public key
- at Function.PublicKey._transformDER (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore
- -lib/lib/publickey.js:176:11)
- at PublicKey._classifyArgs (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore-lib/lib/
- publickey.js:81:22)
- at new PublicKey (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore-lib/lib/publickey.
- js:50:19)
- at pkiEncrypt (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/routes/pki_Stack_Exchange.js:27:59)
- at Object.<anonymous> (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/routes/pki_Stack_Exchange.js:54:11
- )
- at Module._compile (module.js:570:32)
- at Object.Module._extensions..js (module.js:579:10)
- at Module.load (module.js:487:32)
- at tryModuleLoad (module.js:446:12)
- at Function.Module._load (module.js:438:3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement