Skip to Main Content

Programming Languages & Frameworks

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

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

7c7ecf14-1aee-4102-9dac-af04ec88fd6cJul 28 2020 — edited Aug 13 2020

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();

        }

    }

}

Comments

Processing

Post Details

Added on Jul 28 2020
2 comments
2,008 views