在Javascript中使用AES加密ByteArray

时间:2018-11-29 12:26:15

标签: javascript aes

我一直试图在JavaScript中使用AES加密字节数组,但是遇到了一些麻烦。 这是我在Java中的代码,而在JavaScript中我无法达到相同的结果。 有人可以帮我吗?我曾尝试使用CriptoJ,Forge-Node,AES-js,但都无法正常工作。

public static byte[] Cipher() throws Exception {
    byte[] msgCifrada = null;
    byte[] keyBytes = [85, -9, -79, -24, -80, 87, -3, 73, 87, -75, -120, 125, 90, -124, 109, -102];
    byte[] dados = [-88, -93, 5, -78, -116, 36, -49, -11, 106, 111, 110, 97, 116, 104, 97, 115, 46, 115, 105, 108, 118, 97];

    try {
        SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.ENCRYPT_MODE, secretKey);
        msgCifrada = c.doFinal(dados);
    } catch (InvalidKeyException ike) {
        throw new Exception("Chave simétrica inválida.");
    } catch (Exception e) {
        throw new Exception("Erro na cifragem da mensagem: "+ .getMessage());
    }
    return msgCifrada;
}

Java中的结果是ByteArray:

[-90,91,44,29,-113,116,-1,-29,42,47,122,-117,-12,-12,-49,-94,59,-50, 110,115,105,-21,80,93,-15,13,71,-108,23,-80,56,-96]

谢谢!

0 个答案:

没有答案