简体   繁体   English

将ByteArray从PHP发送到Java

[英]Sending ByteArray from PHP to Java

I have problems with sending AES encrypted data from JAVA to PHP. 将JAVA的AES加密数据发送到PHP时遇到问题。

My encrypt function: 我的加密功能:

public static byte[] encrypt(String input, String key, String iv) {

    byte[] raw = key.getBytes(Charset.forName("UTF8"));
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    try {
        Cipher cipher = Cipher.getInstance(CIPHER_MODE);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv.getBytes()));
        return cipher.doFinal(padString(input).getBytes("UTF-8"));
    } catch (Exception e) {
    }

    return new byte[0];
}

encrypt function is returning data in byte array To send POST data, I'm using apache HttpClient/HttpPost (preferred, but not necessary) 加密函数返回字节数组中的数据要发送POST数据,我使用的是apache HttpClient / HttpPost(首选,但不是必需的)

    HttpEntity params = MultipartEntityBuilder.create()
            .addTextBody("data1", new String(encodedData1, Charset.forName("UTF8")))
            .addTextBody("data2", new String((encodedData2, Charset.forName("UTF8")))
            .addTextBody("data3", "data3").build();

Now, I recieve data with PHP server (i can't add functionality here) 现在,我通过PHP服务器接收数据(我无法在此处添加功能)

        $data1 = filter_input(INPUT_POST, 'data1');
        $data2 = filter_input(INPUT_POST, 'data2');
        $data1decoded = DecryptAES($data1, $key, $iv);
        $data2decoded = DecryptAES($data2, $key, $iv);

What is a proper way to send encoded byte[] from JAVA to PHP? 将已编码的byte []从JAVA发送到PHP的正确方法是什么? I know, that Base64.encode would be probably best option, but unfortunately (as I have mentioned earlier) I can't modify PHP server-side code... new String(encodedData1, Charset.forName("UTF8")) is not working. 我知道,Base64.encode可能是最好的选择,但是不幸的是(正如我前面提到的),我无法修改PHP服务器端代码... new String(encodedData1,Charset.forName(“ UTF8”))是不工作。

In node.js I can use toString('binary') to send data in proper format. 在node.js中,我可以使用toString('binary')以正确的格式发送数据。

You can't just create new String (Java is too fancy for that). 您不能只创建新的String (Java对此太花哨了)。

Use 采用

public static String byteToString(byte[] bytes) {
    StringBuilder b = new StringBuilder();
    for (byte c : bytes) {
        b.append((char) (c >= 0 ? c : 256 + c));
    }
    return b.toString();
}

Using your example 用你的例子

HttpEntity params = MultipartEntityBuilder.create()
          .addTextBody("data", byteToString(data))
          .build();

Instead of 代替

new String(encodedData1, Charset.forName("UTF8"))

you should use 你应该使用

new String(encodedData1)

Your byte array doesn't contain an UTF8 text anymore once it has been encrypted. 加密后,您的字节数组不再包含UTF8文本。 You would preserve the exact structure of your byte aray by sending it in its original form with the addBinaryBody method instead of casting it to a String in order to use the addTextBody method. 通过使用addBinaryBody方法以原始形式发送字节,可以保留字节的精确结构,而不是为了使用addTextBody方法将其强制转换为String。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM