Skip to content
Advertisement

Mixing tweetnacl.js with TweetNaclFast (java) for asymmetric encryption

Our project is using asymmetric encryption with nacl.box and ephemeral keys:

JavaScript

We presently have node.js apps that then decrypt these messages. We would like the option to use jvm languages for some features. There does not seem to be the richness of established players for tweet-nacl on the jvm but it seems

and its recommended implementation

° tweetnacl-fast https://github.com/InstantWebP2P/tweetnacl-java/blob/master/src/main/java/com/iwebpp/crypto/TweetNaclFast.java

were a popular one.

It is unclear what the analog to the asymmetric encryption with ephemeral keys were in that library. Is it supported? Note that I would be open to either java or kotlin if this were not supported in tweetnacl-java.

Advertisement

Answer

tweetnacl-java is a port of tweetnacl-js. It is therefore to be expected that both provide the same functionality. At least for the posted method this is the case, which can be implemented on the Java side with TweetNaclFast as follows:

JavaScript

In order to demonstrate that both sides are compatible, in the following a plaintext is encrypted on the Java side and decrypted on the JavaScript side:

  • First, a key pair is needed on the JavaScript side, whose public key (publicKeyJS) is passed to the Java side. The key pair on the JavaScript side can be generated as follows:

    JavaScript

    with the following sample output:

    JavaScript
  • The encryption on the Java side is then using the encrypt method posted above (and publicKeyJS):

    JavaScript

    with the following sample output:

    JavaScript
  • The decryption on the JS side gives the original plaintext (using secretKeyJS):

    JavaScript
    JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement