Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. var bitcore = require('bitcore-lib');
  2. var ECIES = require('bitcore-ecies');
  3. var message = "foobar";
  4.  
  5. var testIdentity= {
  6. type: 'ethereum',
  7. display: '0xedc8774555d634ec3cdc755296382d572de2a492',
  8. privateKey: '1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c',
  9. publicKey: '644c70f5c80c0e0d950659fc3a5ca3e4079072af5b89bdc3d74bf4c43cda5202d294070d3712dc15a1667f1be8541ea5d587b9c041670ce351d031e5c9df563d',
  10. foreign: false
  11. };
  12.  
  13.  
  14. /**
  15. * encrypt the message with the publicKey of identity
  16. * @param {{privateKey: ?string, publicKey: string}} identity
  17. * @param {string} message
  18. * @return {string}
  19. */
  20. var pkiEncrypt = function(identity, message) {
  21.  
  22. /*
  23. * this key is used as false sample, because bitcore would crash when alice has no privateKey
  24. */
  25. var privKey = new bitcore.PrivateKey('1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c');
  26. console.log("private key=>" + privKey);
  27. var alice = ECIES().privateKey(privKey).publicKey(new bitcore.PublicKey(identity.publicKey));
  28. console.log("Alice's public key=>" + alice);
  29. var encrypted = alice.encrypt(message);
  30. console.log("encrypted message by public key of Alice=>"+ encrypted.toString('hex'));
  31.  
  32. return encrypted.toString('hex');
  33. };
  34.  
  35. /**
  36. * decrypt the message with the privateKey of identity
  37. * @param {{privateKey: ?string, publicKey: string}} identity
  38. * @param {string} encrypted
  39. * @return {string} message
  40. */
  41. var pkiDecrypt = function(identity, encrypted) {
  42. var privKey = new bitcore.PrivateKey(identity.privateKey);
  43. console.log("Alice Private key=> " + privKey);
  44. var alice = ECIES().privateKey(privKey);
  45. console.log("Allice private key again=>" + alice);
  46. var decryptMe = new Buffer(encrypted, 'hex');
  47. console.log("decrypted message=>" + decryptMe);
  48. var decrypted = alice.decrypt(decryptMe);
  49. console.log("final decrypted message=>"+decrypted.toString('ascii'));
  50. return decrypted.toString('ascii');
  51. };
  52.  
  53.  
  54. var enc = pkiEncrypt(testIdentity, message);
  55. var dec = pkiDecrypt(testIdentity, enc);
  56.  
  57. if(dec!=message){
  58. console.log("failure");
  59. }else{
  60. console.log("Success");
  61. }
  62.  
  63. $ node pki_Stack_Exchange.js
  64. private key=>1d71471e33462a2484cd828643da29f2f643e2f55f6922c16c9a9df2982f408c
  65. /home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore-lib/lib/publickey.js:176
  66. throw new TypeError('Invalid DER format public key');
  67.  
  68. ^
  69.  
  70. TypeError: Invalid DER format public key
  71. at Function.PublicKey._transformDER (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore
  72. -lib/lib/publickey.js:176:11)
  73. at PublicKey._classifyArgs (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore-lib/lib/
  74. publickey.js:81:22)
  75. at new PublicKey (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/node_modules/bitcore-lib/lib/publickey.
  76. js:50:19)
  77. at pkiEncrypt (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/routes/pki_Stack_Exchange.js:27:59)
  78. at Object.<anonymous> (/home/vikas/nodeExamples/nodeJS10Project/API_Handling_working_fine_7_JUL/routes/pki_Stack_Exchange.js:54:11
  79. )
  80. at Module._compile (module.js:570:32)
  81. at Object.Module._extensions..js (module.js:579:10)
  82. at Module.load (module.js:487:32)
  83. at tryModuleLoad (module.js:446:12)
  84. at Function.Module._load (module.js:438:3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement