-2

I have following code in Java.

        String secretString = 'AAABBBCCC'
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom securerandom = SecureRandom.getInstance("SHA1PRNG");
        securerandom.setSeed(secretString.getBytes());
        kgen.init(256, securerandom);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] byteContent = content.getBytes("utf-8");
        byte[] cryptograph = cipher.doFinal(byteContent);
        String enc1 = Base64.getEncoder().encodeToString(cryptograph);
        return enc1;

I need to implement it in JavaScript/Node.js, however I can only figure out the last half in js like below

        'use strict';
        const crypto = require('crypto');
        const ALGORITHM = 'AES-256-ECB';
        const secretString = 'AAABBBCCC'

        // missing part in JS (how to convert secretString to key)

        function encrypt(plaintext, key) {
            const cipher = crypto.createCipheriv(ALGORITHM, key, Buffer.alloc(0));
            return cipher.update(plaintext, 'utf8', 'base64') + cipher.final('base64');
        }

For the previous Java part( from secretString to key generated by KeyGenerator), I don't know how to implement it in JavaScript, neither do I know whether there is a thing like KeyGenerator in JavaScript world could help me to do the heavy lifting work.

kenshinji
  • 2,021
  • 4
  • 25
  • 39

2 Answers2

0

I think this is what you're after:

const crypto = require("crypto-js");


// Encrypt
const ciphertext = crypto.AES.encrypt('SOMETHING SECRET', 'secret key 123');

// Decrypt
const bytes  = crypto.AES.decrypt(ciphertext.toString(), 'secret key 123');
const decryptedData = bytes.toString(crypto.enc.Utf8);

console.log(decryptedData);

https://runkit.com/mswilson4040/5b74f914d4998d0012cccdc0

UPDATE

JavaScript does not have a native equivalent for key generation. The answer is to create your own or use a third party module. I would recommend something like uuid for starters.

mwilson
  • 12,295
  • 7
  • 55
  • 95
  • Thank you for your answer, I think you misunderstood the most important part from my question, which is from KEY to key in java code. Maybe I should update my question to make it easier understood. – kenshinji Aug 16 '18 at 04:13
  • So your question is how to obtain a value that lives in java from JavaScript?? – mwilson Aug 16 '18 at 04:13
  • nope, I don't understand how to implement the process (from secretString to key in Java) in JavaScript. Because what you posted is I've done in my JS code. btw, I updated my post. – kenshinji Aug 16 '18 at 04:17
0

You can use crypto.randomBytes(). As per its documentation:

Generates cryptographically strong pseudo-random data. The size argument is a number indicating the number of bytes to generate.

Also, it uses openssl's RAND_bytes API behind scenes

miradham
  • 2,285
  • 16
  • 26