Languages
[Edit]
EN

JavaScript - One-time Pad encryption with XOR

9 points
Created by:
Majid-Hajibaba
972

In this short article, we would like to show a simple JavaScript implementation of the most save encrytion that was invented by humans - One-time Pad encryption with XOR.

Used XOR formula:

 

Quick solution:

1. single byte encryption

2. multiple bytes encryption 

 

Algorithm description

Edit

The algorithm is as strong as perfectly random numbers have been used as secret keys.

The main concept of the algorithm is to:

  1. use XOR formula on the data units to encode and decode,

    Hint: check Wikipedia reference to see other than XOR encryption formula.

  2. store on sender and recipient devices, secret keys composed of perfect random numbers
    (the numbers from computer algorithms may be predictable, so use nature-based approaches),
  3. never share the random numbers with others than the sender and the recipient,
  4. use always only part of secret keys to encrypt or decrypt some sent message (one byte encoded by one secret key),
  5. never use 2 times the same secret keys.

 

Universal implementation

Edit

This implementation provides a cipher that stores information about lastly used keys, preventing against breaking rule 5 (never use 2 times the same secret key).

Conclusions:

  • [97,98,99] encrypted 2 times returned 2 different results: [143,169,133] and [90,104,10] - it is provided by once time secret key usage composed of random numbers.

 

Alternative implementation

Edit

 

See also

Edit
  1. JavaScript - random bytes

  2. JavaScript - own random number generator (custom implementation)

  3. JavaScript - One-time Pad encryption with XOR on strings

  4. JavaScript - convert string to bytes array

  5. JavaScript - convert string to bytes array under Node.js

References

Edit
  1. One-time pad - Wikipedia
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join