Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypt then decrypt node issue with more than 16 chars message #47

Closed
AquiGorka opened this issue May 20, 2019 · 15 comments
Closed

Encrypt then decrypt node issue with more than 16 chars message #47

AquiGorka opened this issue May 20, 2019 · 15 comments

Comments

@AquiGorka
Copy link

@AquiGorka AquiGorka commented May 20, 2019

Case

Bug

Issue

Encrypt -> Decrypt is not working with node@v12.2.0 with phrases more than 16 chars in length

Info

  • Environment: node (originally found with node + browser, but could repro in node only too)

Code

  const publicKey =
     '030dddc2e1cb1f9c5c3463399f0b539e2f2a9d42678f596cef95ce301087c05836'
   const privateKey =
     '6b463fdd6b15df6da6753fe8d1a5973b133c7a4aed77b38b145b11987cf27876'
   let randomStr
   let answer
   let challenge
   try {
     randomStr = crypto.randomBytes(6).toString('hex')
     //randomStr = '1234567889012345'
     console.log('randomStr: ', randomStr)
     challenge = await encryptWithPublicKey(publicKey, Buffer.from(randomStr))
     console.log('challenge: ', challenge)                                                                                                                        
     answer = await decryptWithPrivateKey(privateKey, challenge)
   } catch (e) {
     console.log('Error: ', JSON.stringify(e, null, 2))
   }
 
  console.log('----> ', randomStr === answer)

Error thrown (only node@12.2.0):

Error:  {
  "library": "digital envelope routines", 
  "function": "EVP_DecryptFinal_ex",
  "reason": "bad decrypt",
  "code": "ERR_OSSL_EVP_BAD_DECRYPT"
}
---->  false

Node@11.4.0 gives a satisfactory: ----> true

@HP213
Copy link

@HP213 HP213 commented Jul 8, 2019

Yes it is not decrypting message with more than 16 characters

pubkey added a commit that referenced this issue Aug 3, 2019
@pubkey
Copy link
Owner

@pubkey pubkey commented Aug 3, 2019

I added a test in the attached commit.
I think this has something to do with the openssl version. Can you run openssl version and post the output?

@pubkey
Copy link
Owner

@pubkey pubkey commented Aug 3, 2019

https://travis-ci.org/pubkey/eth-crypto/builds/567377666

Some things I noticed:
npm install fails because of ecdh.target.mk:113: recipe for target 'Release/obj.target/ecdh/ecdh.o' failed see here

Then because of the failed build there is a MODULE_NOT_FOUND with Reverting to browser version. On the browser-version there is a different kind of padding of the public key which results in the error EVP_DecryptFinal_ex.

The current workaround is to downgrade to node v11.
I think this must be solved by eccrypto or secp256k1

@pubkey
Copy link
Owner

@pubkey pubkey commented Aug 3, 2019

I opened bitchan/eccrypto#42

@pubkey
Copy link
Owner

@pubkey pubkey commented Sep 26, 2019

Fixed by 45e7029.
I released 1.5.0.
Please update.

@pubkey pubkey closed this Sep 26, 2019
@AquiGorka
Copy link
Author

@AquiGorka AquiGorka commented Sep 26, 2019

Awesome @pubkey 🎉 🎉
Thank you

@GeekRishabh
Copy link

@GeekRishabh GeekRishabh commented Jan 24, 2020

@pubkey Still getting the same issue tried 1.5.1 as well .

@GeekRishabh
Copy link

@GeekRishabh GeekRishabh commented Jan 27, 2020

This is only working if we have the debugger enabled in React Native.

Node : - 10.15.3
"react": "16.9.0",
"react-native": "0.61.2",
"eth-crypto": "1.5.1",

Sample Code

const data =
  "https://test.com/alpha/sudotest?txnid=FR001&txnref=FR001&amt=1234567&cc=USD"; // message
const encrypted = EthCrypto.encryptWithPublicKey(
"be11a49bbc3ea85d2fc8cf097bfef8e2d48ed363ccfafa4de04adbe47527df374f651b437425bb9d453eef040fcbddce3363d990d15181e2e1cce694f4dc32e6", // publicKey
  data // message
);
encrypted
  .then(data1 => {
    const str = EthCrypto.cipher.stringify(data1);
    console.log(str);
  })
  .catch(e => {
    console.log("error", e);
  });

Decrypt Code

const encryptedObject = EthCrypto.cipher.parse(
  "10b2dac2cc0fed11187eadf33bc4936c0292a2ea5a8384647fb26191311e2c455b539a289097b97b5b6a1a25a5c3880cb61b97e613c8d6d5959e91326a63fef17a47ec4540f08c204da77666290cba4a71a6518e5bc33a7cc381f11a9cdd851f45f957850ef1c61f766ace51f4b1b6063250cac109490ba1282d6fe2eda93c1e774a8a0ce9913e3d654628433a124c28093110059bae66ae6a65c62316ad47072b3b7b74006e50941a761b381731646fe7ea4c93f224a73b7da3c2eb3127b6b303"
);


const message = EthCrypto.decryptWithPrivateKey(
  "0x6cd8176f53335af2726d30ef9b2624e40425f099aca9fcf8995cceb410a030fb", // privateKey
  encryptedObject
);
message
  .then(data => {
    console.log(data);
    const Obj = queryString.parse(data, {
      arrayFormat: "index"
    });
    console.log(Obj, "Obj");
  })
  .catch(e => {
    console.log("error", e);
  });

I am only getting 4567&cc=USD from data after decryption in production while in debug mode i am able to get complete data.
@pubkey @AquiGorka @HP213 Any clue?

@pubkey
Copy link
Owner

@pubkey pubkey commented Jan 27, 2020

@GeekRishabh I have no clue and I think this should be a new issue.
Maybe you can check with the debugger which module-path is taken here and then we can isolate the problem. This is likely something with secp256k1 in react native.

@vuhuykhoi
Copy link

@vuhuykhoi vuhuykhoi commented Mar 3, 2020

I still have this error at Version 1.5.2
@pubkey can you check it again?

@pubkey
Copy link
Owner

@pubkey pubkey commented Mar 3, 2020

@vuhuykhoi I cannot reproduce this and have spend too much time on it.
If you have this error, please debug into it and check whats is wrong.

@recepaltas
Copy link

@recepaltas recepaltas commented Jun 8, 2020

Same Here. Can you help me for this. The decrypt text is showing debug console of Chrome but not showing the app.
Error :unable to decrypt data

Any Solution ?

İt has an error more than 16 character.

@GeekRishabh
Copy link

@GeekRishabh GeekRishabh commented Jun 9, 2020

Hello @recepaltas You can check my repo here It has the fix for more than 16 characters

@pubkey
Copy link
Owner

@pubkey pubkey commented Jun 9, 2020

Can you make a PR?

@GeekRishabh
Copy link

@GeekRishabh GeekRishabh commented Jun 9, 2020

Hello @pubkey My repo code has few old dependencies and also I will need to fix the test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants