AES cifrado / descifrado en el Nodo JS similar a Java

Prashant Kumar Sharma:

Estoy tratando de replicar el Java de código para AES Cifrado y descifrado en el Nodo JS .

Código de Java

    SecretKeySpec skeySpec;
    String key = "a4e1112f45e84f785358bb86ba750f48";

    public void encryptString(String key) throws Exception {
        try {
            skeySpec = new SecretKeySpec(key.getBytes(), "AES");
            cipher = Cipher.getInstance("AES");
            cipher.init(1, skeySpec);
            byte encstr[] = cipher.doFinal(message.getBytes());
            String encData = new String(encstr, "UTF-8");
            System.out.println(encData);
        } catch (NoSuchAlgorithmException nsae) {
            throw new Exception("Invalid Java Version");
        } catch (NoSuchPaddingException nse) {
            throw new Exception("Invalid Key");
        }
    }

nodo JS

    var encryptKey = function (text) {
        var cipher = crypto.createCipher('aes256', 'a4e1112f45e84f785358bb86ba750f48');
        var crypted = cipher.update(text,'utf8', 'hex')
        crypted += cipher.final('hex');
        console.log(crypted);
        return crypted;
    }

Soy incapaz de conseguir la exacta texto cifrado en el Nodo JS , que estoy recibiendo en Java .

Prashant Kumar Sharma:

Finalmente, después de la revisión de Java Docs y Nodo JS Crypto Docs lograron obtener el resultado. Tenemos que usar crypto.createCipheriv()en lugar de crypto.createCiphercon un IV. Aquí habrá iv null.

código:

    let crypto = require('crypto');

    var iv = new Buffer.from('');   //(null) iv 
    var algorithm = 'aes-256-ecb';
    var password = 'a4e1112f45e84f785358bb86ba750f48';      //key password for cryptography

    function encrypt(buffer){
        var cipher = crypto.createCipheriv(algorithm,new Buffer(password),iv)
        var crypted = Buffer.concat([cipher.update(buffer),cipher.final()]);
        return crypted;
    }

    console.log(encrypt(new Buffer('TextToEncrypt')).toString())

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=478775&siteId=1
Recomendado
Clasificación