2

Tengo un problema con mi codigo de encriptacion de claves usando crypto-js. por defecto genera un unsigned byte array (Uint8Array) y necesito pasarlo a signed byte array (Int8Array). Este es mi codigo en JAvascript (Nodejs -typescript)

var CryptoJS = require("crypto-js");

// Encrypt
var plaintext = originalKey;
var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse(salt));
var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

var encrypted = CryptoJS.AES.encrypt(plaintext, key, {
  mode: CryptoJS.mode.ECB,
  padding: CryptoJS.pad.Pkcs7
});

var ciphertext = encrypted.ciphertext.toString(CryptoJS.enc.Hex);

let uint8Array = new TextEncoder().encode(ciphertext);
console.log(uint8Array.toString());

//EXAMPLE RETURN

98,102,50,53,55,97,52,54,101,52,54,54,48,100,55,50,54,57,53,50,54,49,55,54,55,10 1,50,49,100,98,48,102,51,56,52,98,49,53,53,53,48,97,99,52,55,97,53,49,100,98,48,55,56,52,52,102,48,99,48,57,101,53,49,102,51,97,100,53,51,53,101,99,100,48,48,48,53,100,98,57,99,57,52,98,53,99,57,99,53,53,49,53,52,54,55,100,98,99,99,99,101,48,99,101,54,55,97,56,52,52,56,97,54,54,55,101,48,52,49,100,102,54,102,49,48,54,99,99,55,48,102,98,51,51,52,102,53,50,55,51,97,48,51,55,48,99,53,100,52,98,99,51,54,48,99,100,97,100,52,97

Este es mi codigo en Java:

public byte[] crypto(String data) throws Exception {
    byte[] input = data.getBytes();
    byte[] keyBytes = CLIENT_KEY.getBytes();
    Log.d("CRYPTO",new String(keyBytes));
    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
    //System.out.println(new String(input));
    //o.setText("Original: " + new String(input));

    // encryption pass
    cipher.init(Cipher.ENCRYPT_MODE, key);

    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    //System.out.println(new String(cipherText));
    //e.setText("Encrypted: " + new String(cipherText));
    System.out.println("ENCRYPT: " + ctLength);

    return cipherText;
    //EXAMPLE RETURN:
    /*{57,-16,-30,34,-81,61,-14,-65,-1,83, -81, -78, 43, -26, -45, 77, 113, 
    -89, -91, -86, -11, -117, -28, -105, 45, 46, 103, 23, 11, -87, 86, -46, 
     -15, 52, 31, 11, -62, 115, 119, -56, 71, -125, -108, -41, -19, -116, 
    -76, 3, 97, 29, 77, 99, -33, 100, 41, 54, -2, 69, -50, 100, 41, -45, 54, 
    114, 8, 0, -111, 40, 103, -117, 0, -22, -20, 29, 107, 114, 45, -16, -85, 
    125}*/


}

Si me pueden ayudar se los agradezco!

0

1 respuesta 1

2

Todos los TypedArray comparten el método .from, que permite transformar entre ellos:

let u = Uint8Array.from([0,-1]); // -1 en un unsigned será 255
console.log('Sin signo', u);

let i = Int8Array.from(u); //255 en complemento a 2 vuele a ser -1
console.log('Con signo', i);

1
  • No me estaria funcionando... probe encriptando como lo hago en java pero no sucede nada el 8 mar. 2019 a las 18:40

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.