Porting Java AES encryption code to node.js
<p>I'm trying to port following AES encryption code to nodejs from java.</p>

<pre><code>import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class AESEncryption {

    public static void main(String[] args) {
        try {
            String key = "AsKUMDPsr7dfuk6fxgpfRdwm6de+MArf4SdAds9aq";
            byte[] hashedKey = MessageDigest.getInstance("SHA1").digest(key.getBytes("UTF-8"));
            SecretKeySpec keySpec = new SecretKeySpec(hashedKey, 2, 16, "AES"); // don't know how to port this to nodejs

            String plainText = "test123";
            Cipher cipher = Cipher.getInstance("AES");
            cipher1.init(1, keySpec);
            System.out.println(Base64.encodeBase64String(cipher.doFinal(plainText.getBytes("UTF-8"))));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
</code></pre>

<p>Tried writing following nodejs code but encrypted texts are different.</p>

<pre><code>crypto = require('crypto');

var key = 'AsKUMDPsr7dfuk6fxgpfRdwm6de+MArf4SdAds9aq';
var shasum = crypto.createHash('sha1');
shasum.update(key);
var encrypted_key = shasum.digest('').slice(2, 18).toString('utf8');
console.log(encrypted_key);

var plain_text = 'test123';
var cipher = crypto.createCipheriv('aes-128-ecb', encrypted_key, '');
var encrypted_text = cipher.update(plain_text, 'utf-8', 'base64');
encrypted_text += cipher.final('base64');
console.log(encrypted_text);
</code></pre>

<p>How can I port <code>SecretKeySpec</code> which created from byte array?</p>