Forum Stats

  • 3,876,265 Users
  • 2,267,083 Discussions


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

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.util.Arrays;



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) {



        catch (UnsupportedEncodingException e2) {






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


    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;


    public class HelloWorld {

      public static String getHelloWorld() {

        return "Hello from Java!";



    can be used in JavaScript like below:

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




  • 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=”"></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:

    Oracle does not support any of these fyi.