2

Encryption is done is python & decryption to be done in Node js but facing following issues in JS

  1. I guess some characters are getting escape from string within JS like 'backslash', '_', ''

Expected - Python utf-8 string should match with Node JS String

Node js

var a = 'b22KTGxtQmtRei9CTEtUKy0OY1qefbey0brGbNYaskVbrdclYyXFlkqSnolziVDMEguUB5Xx7+9vix8UpwUn8uAbQvmW/uVRM7gRAO063i4tpPD2Ao3wrgapLQQBYnUo+aB2uS5t3a4jzldKq8OUVsY9QWXRJ28vTvJuOnyR6+bpN9yDaiMHP0rdI510PRetIw=='
var lenSize = Buffer.from(a, 'base64')
console.log(lenSize.toString().length)

Buffer size is 145 but toString length is 139

VS Python code

import base64
a = 'b22KTGxtQmtRei9CTEtUKy0OY1qefbey0brGbNYaskVbrdclYyXFlkqSnolziVDMEguUB5Xx7+9vix8UpwUn8uAbQvmW/uVRM7gRAO063i4tpPD2Ao3wrgapLQQBYnUo+aB2uS5t3a4jzldKq8OUVsY9QWXRJ28vTvJuOnyR6+bpN9yDaiMHP0rdI510PRetIw=='
lenSize = base64.b64decode(a)
print(len(lenSize))

Length is 145

Thanks in-advance...

  • In NodeJS the length must be determined with `lenSize.length`. `lenSize.toString()` performs a UTF8 decoding. _Arbitrary_ binary data will be corrupted by this. – Topaco Sep 17 '21 at 10:35
  • @Topaco utf8 decoding is getting different response in node js as compared to python and I need to further decrypt utf-8 so it is not corrupted. Only problem is JS is escaping characters from my utf-8 string. – Hitesh Panjawani Sep 17 '21 at 10:41
  • If you hex encode your data (instead of Base64), you will find that it's not UTF8 compliant. If you expect UTF8 decodable data, you may have a problem with the derivation. – Topaco Sep 17 '21 at 10:53
  • @Topaco this is working code from years it is just we want to migrate from Python to JS so facing issue in that. So derivation should not be the problem over here I feel. – Hitesh Panjawani Sep 17 '21 at 11:07
  • 1
    I don't doubt that the Python code works. Only your assumption is wrong that `b64decode()` does a UTF8 decoding. `b64decode()` returns the data under Python 2.7 as _binary_ string and not as UTF8 decoded string. Therefore, the implicit UTF8 decoding in the NodeJS code with `toString()` is wrong! Again: The Base64 decoded data is not UTF8 compliant (you can check this yourself, e.g. with https://cryptii.com/pipes/base64-to-hex) and a UTF8 decoding corrupts it. – Topaco Sep 17 '21 at 15:15
  • Consider changing the title of your question to a plain formulated question. I think that would attract more people to your issue. [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Dennis Sep 24 '21 at 18:39
  • Sure will try @Dennis – Hitesh Panjawani Oct 13 '21 at 06:27

0 Answers0