Forum Stats

  • 3,751,464 Users
  • 2,250,366 Discussions
  • 7,867,433 Comments

Discussions

Hi There, Is it possible to convert a Java Code into Javascript code?

7c7ecf14-1aee-4102-9dac-af04ec88fd6c
edited Aug 13, 2020 2:17AM in JavaScript - Nashorn

Hi There,

i have a Java code for Generating Encryption Key and I would like it to be in Javascript code, please help.

package encrypt;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;

import java.io.UnsupportedEncodingException;

import java.security.NoSuchAlgorithmException;

import java.util.Arrays;

import java.security.Key;

import java.security.MessageDigest;

import javax.crypto.spec.SecretKeySpec;

public class AESEncryptor

{

    private static SecretKeySpec secretKey;

    private static byte[] key;

    private static String key1;

   

    public static void setKey(final String myKey, final int iterations) {

        AESEncryptor.key1 = myKey.toString();

        MessageDigest sha = null;

        try {

            AESEncryptor.key = myKey.getBytes("UTF-8");

            sha = MessageDigest.getInstance("SHA-256");

            for (int i = 1; i <= iterations; ++i) {

                AESEncryptor.key = sha.digest(AESEncryptor.key);

            }

            AESEncryptor.key = Arrays.copyOf(AESEncryptor.key, 16);

            AESEncryptor.secretKey = new SecretKeySpec(AESEncryptor.key, "AES");

        }

        catch (NoSuchAlgorithmException e) {

            e.printStackTrace();

        }

        catch (UnsupportedEncodingException e2) {

            e2.printStackTrace();

        }

    }

}

Answers

  • Ravikiran D-Oracle
    Ravikiran D-Oracle Posts: 11 Employee
    edited Aug 13, 2020 2:17AM

    Hi,

    Please go through the given J2CL document with reference sample code and hope it helps.

    Already written java source code with annotation of @JsType can be referred in javascript file and invoked.

    Designed, developed, and open-sourced by Google, J2CL is a source-to-source compiler that converts Java to Javascript.

    J2CL attempts to solve a different problem than similar Java-to-Javascript frameworks such as GWT, and it is not meant to compete with or replace existing JavaScript frameworks. Instead, J2CL is about interoperability and cross-platform code reuse. According to the developers, "[y]ou can use J2CL to just make some Java code accessible from JavaScript or go all the way to create a whole application with it; whatever best suits your needs."

    J2CL can translate most existing Java code from source but not all Java APIs are supported, such as the Java reflection API. By default, translated code is not public and a number of Java annotations from JsInterop can be used to identify what classes, methods, instance variables, or other code should be exposed. The project includes a very simple "hello world" example to demonstrate usage. A slightly modified excerpt of that example is included below showing an annotated Java class and how it can be used in JavaScript.

    The following Java class, ran through J2CL:

    package com.acme

    import jsinterop.annotations.JsType;

    @JsType

    public class HelloWorld {

      public static String getHelloWorld() {

        return "Hello from Java!";

      }

    }

    can be used in JavaScript like below:

    const HelloWorld = goog.require('com.acme');

    console.log(HelloWorld.getHelloWorld());

    Regards,

    Ravikiran

  • codiant
    codiant Member Posts: 126 Blue Ribbon

    You don't, because Java Code and Javascript code, the two languages operates on different environments. You may possibly "translate" the code itself, but the framework around them is very different. Java code works on command line parameters, which Javascript doesn't have.

  • ErickRecio-Oracle
    ErickRecio-Oracle Member Posts: 3 Employee

    In general terms code in Java cannot be converted to JavaScript. These are completely different languages.

    You can manually rewrite some of the code, and in your case the solution is to google AES and Javascript to get libraries that perform just that.

    Some entries appear on StackOverflow that point to JavaScript libraries like aes.js. Please read their documentation. I believe the operations go somewhat in the lines of:

    Include the library:

    <script src=”http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>

    then encrypt with :

    var encryptedMsg = CryptoJS.AES.encrypt("Sample Message", "yourkey123")

    alert("Encrypted Message =" +encryptedMsg )

    and decrypt with :

    var decryptedMsg = CryptoJS.AES.decrypt(encryptedMsg , "yourkey123");

    There are other libraries out there like: http://bitwiseshiftleft.github.io/sjcl/doc/

    http://bitwiseshiftleft.github.io/sjcl/demo/


    Oracle does not support any of these fyi.