1

I was trying for amusement purposes to program RSA in JavaScript,however when i get to the encryption or decryption part and i have to power the message(which is essentially a number in ASCII, (im encrypting each individual character)) sometimes im dealing with very large numbers, for example:

Math.pow(48,103);

And from what im experiencing the result is not correct. With a little research realised JS is not precise enough to deal with really big numbers(>2^(253 - 1)). So im asking if there's a way where i can perform this power operation via some other language to obtain the accurate answer and then export it to JS?

  • In java you can use BigInteger in java , but I am not sure whether after export the results would be same or not – Saurabh Verma Jan 23 '18 at 16:19
  • I think your upper bound for integers has a typo? Should be `2^53 - 1` I think: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER. – Mauro Bringolf Jan 23 '18 at 16:20
  • Take a look at [bignumber.js](https://github.com/MikeMcl/bignumber.js/). – gforce301 Jan 23 '18 at 16:20
  • Also there are a host of libraries like `bignumber.js` https://github.com/MikeMcl/bignumber.js/ which allow for arbitrary precision arithmetic. – Mauro Bringolf Jan 23 '18 at 16:21
  • newbie question here. I tried to install the library bignumber.js via terminal by writting npm install bignumber.js but i get this error: npm WARN enoent ENOENT: no such file or directory, open '/home/user/Desktop/package.json' npm WARN Desktop No description npm WARN Desktop No repository field. npm WARN Desktop No README data npm WARN Desktop No license field. – user8848592 Jan 23 '18 at 16:49
  • Thanks. Later this afternoon i managed to solve the problem. What i did was: npm install bignumber.js (i wrote this on the terminal to install the library), and on the first line of my javascript file i wrote: var BigNumber = require('bignumber.js') to make my javascript code have acess to the library. After this i could work with really big numbers without losing precision. – user8848592 Jan 23 '18 at 20:36

0 Answers0