Hi There, Is it possible to convert a Java Code into Javascript code? — oracle-tech

    Forum Stats

  • 3,681,448 Users
  • 2,238,014 Discussions
  • 7,831,224 Comments

Discussions

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

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-OracleRavikiran D-Oracle Posts: 11 Employee
    edited August 13

    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

Sign In or Register to comment.