将byte []转换为String然后再转换为byte []

时间:2021-11-28 20:14:02

I am working on a proxy server. I am getting data in byte[] which I convert into a String to perform certain operations. Now when i convert this new String back into a byte[] it causes unknown problems.

我正在使用代理服务器。我在byte []中获取数据,我将其转换为String以执行某些操作。现在,当我将这个新String转换回byte []时,它会导致未知问题。

So mainly its like I need to know how to correctly convert abyte[] into a String and then back into a byte[] again.

所以主要是我需要知道如何正确地将abyte []转换为String然后再转换回byte []。

I tried to just convert the byte[] to String and then back to byte[] again (to make sure thats its not my operations that are causing problems).

我试图将byte []转换为String然后再转换回byte [](以确保它不是我的操作导致问题)。

So it's like:

所以它就像:

// where reply is a byte[]

String str= new String(reply,0, bytesRead);
streamToClient.write(str.getBytes(), 0, bytesRead);

is not equivalent to

不等于

streamToClient.write(reply, 0, bytesRead);

my proxy works fine when I just send the byte[] without any conversion but when I convert it from byte[] to a String and then back to a byte[] its causes problems.

当我发送byte []而没有任何转换时,我的代理工作正常,但是当我将它从byte []转换为String然后返回到byte []时会导致问题。

Can some one please help? =]

有人可以帮忙吗? =]

4 个解决方案

#1


9  

The best way to convert a byte[] to String and back into a byte[] is not to do it at all.

将byte []转换为String并返回byte []的最佳方法是不要这样做。

If you have to, you must know the encoding that was used to produce the byte[], otherwise the operation uses the platform default encoding, which can corrupt the data because not all encodings can encode all possible strings, and not all possible byte sequences are legal in all encodings. This is what's happening in your case.

如果必须,您必须知道用于生成byte []的编码,否则操作使用平台默认编码,这会破坏数据,因为并非所有编码都可以编码所有可能的字符串,而不是所有可能的字节序列在所有编码中都是合法的。这就是你的情况。

As for how to find out the encoding, that depends:

至于如何找出编码,这取决于:

  • If you're using HTTP, look at the Content-Type header
  • 如果您使用的是HTTP,请查看Content-Type标头
  • If your data is XML, you should be using an XML parser, which will handle the encoding for you
  • 如果您的数据是XML,那么您应该使用XML解析器,它将为您处理编码
  • If your data is HTML pages, there might also be a <meta http-equiv> header
  • 如果您的数据是HTML页面,则可能还有 标头

If there is no way to find out the encoding you have random garbage, not text data.

如果没有办法找出编码,你有随机垃圾,而不是文本数据。

#2


4  

If its signed byte array then the simplest solution that I found was encode the byte array with BASE64EncoderStream which will convert it into unsigned bytes. Then you will have to use BASE64DecoderStream to decode the bytes to get back the original signed byte array.

如果它的有符号字节数组,那么我找到的最简单的解决方案是使用BASE64EncoderStream对字节数组进行编码,将其转换为无符号字节。然后,您将不得不使用BASE64DecoderStream来解码字节以获取原始的有符号字节数组。

POM Dependency for BASE64 : com.sun.mail javax.mail 1.4.4

POM对BASE64的依赖:com.sun.mail javax.mail 1.4.4

public class EncryptionUtils {

private static String ALGO = "AES";
private static  Cipher cipher;




public static String encrypt(String message, String keyString) {
    cipher = Cipher.getInstance(ALGO);
        Key key = generateKey(keyString);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return new String(BASE64EncoderStream.encode(cipher.doFinal( message.getBytes())));
}

public static String decrypt(String message, String keyString)  {

       cipher = Cipher.getInstance(ALGO);
        Key key = generateKey(keyString);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(BASE64DecoderStream.decode(message.getBytes()))); 

}

private static Key generateKey(String keyString) throws NoSuchAlgorithmException {
    byte[] keyBytes = BASE64DecoderStream.decode(keyString.getBytes());
    Key key = new SecretKeySpec(keyBytes, ALGO);
    return key;
}

public static void main(String args[]) {
    byte[] keyValue = new byte[16];
    new SecureRandom().nextBytes(keyValue);
    String key = new String(BASE64EncoderStream.encode(keyValue));
    String message = "test message";
    String enc = encrypt(message, key);
    String dec = decrypt(enc, key);
    System.out.println(dec);
}}

#3


3  

You will need to know the character encoding used, decode the bytes using that and re-encode using the same character encoding. For example:

您需要知道使用的字符编码,使用它解码字节并使用相同的字符编码重新编码。例如:

String str = new String(reply, 0, Charset.forName("UTF-8"));
bytes[] out = str.getBytes(Charset.forName("UTF-8"));
streamToClient.write(bytes, 0, bytes.length);

If not specified, Java using a default character encoding, which is typically UTF-8 (it may even be mandated as such) but HTML will often be something else. I suspect that's your problem.

如果没有指定,Java使用默认字符编码,通常是UTF-8(甚至可能是强制性的),但HTML通常是其他的。我怀疑那是你的问题。

#4


0  

I have a similar problem when reading from a socket and sending to another, but my problem was that I was writing the output with a BufferedOutputStream, when I change this to Output stream it works. I think that there is a problem with the buffer ouput stream.

我从套接字读取并发送到另一个时有类似的问题,但我的问题是我用BufferedOutputStream编写输出,当我将其更改为输出流时,它可以工作。我认为缓冲输出流存在问题。

String mensaje ="what I want to send";
String ip = "192.168.161.165";
int port =  2042;
tpSocket = new Socket(ip, port);
os = tpSocket.getOutputStream();
byte[] myBytes= mensaje.getBytes();
ByteArrayInputStream byarris = new ByteArrayInputStream(myBytes);
int resulta =0;
byte[] bufferOutput= new byte[1];
while((resulta = byarris.read(bufferOutput))!= -1) {
    os.write(bufferOutput);
}

#1


9  

The best way to convert a byte[] to String and back into a byte[] is not to do it at all.

将byte []转换为String并返回byte []的最佳方法是不要这样做。

If you have to, you must know the encoding that was used to produce the byte[], otherwise the operation uses the platform default encoding, which can corrupt the data because not all encodings can encode all possible strings, and not all possible byte sequences are legal in all encodings. This is what's happening in your case.

如果必须,您必须知道用于生成byte []的编码,否则操作使用平台默认编码,这会破坏数据,因为并非所有编码都可以编码所有可能的字符串,而不是所有可能的字节序列在所有编码中都是合法的。这就是你的情况。

As for how to find out the encoding, that depends:

至于如何找出编码,这取决于:

  • If you're using HTTP, look at the Content-Type header
  • 如果您使用的是HTTP,请查看Content-Type标头
  • If your data is XML, you should be using an XML parser, which will handle the encoding for you
  • 如果您的数据是XML,那么您应该使用XML解析器,它将为您处理编码
  • If your data is HTML pages, there might also be a <meta http-equiv> header
  • 如果您的数据是HTML页面,则可能还有 标头

If there is no way to find out the encoding you have random garbage, not text data.

如果没有办法找出编码,你有随机垃圾,而不是文本数据。

#2


4  

If its signed byte array then the simplest solution that I found was encode the byte array with BASE64EncoderStream which will convert it into unsigned bytes. Then you will have to use BASE64DecoderStream to decode the bytes to get back the original signed byte array.

如果它的有符号字节数组,那么我找到的最简单的解决方案是使用BASE64EncoderStream对字节数组进行编码,将其转换为无符号字节。然后,您将不得不使用BASE64DecoderStream来解码字节以获取原始的有符号字节数组。

POM Dependency for BASE64 : com.sun.mail javax.mail 1.4.4

POM对BASE64的依赖:com.sun.mail javax.mail 1.4.4

public class EncryptionUtils {

private static String ALGO = "AES";
private static  Cipher cipher;




public static String encrypt(String message, String keyString) {
    cipher = Cipher.getInstance(ALGO);
        Key key = generateKey(keyString);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return new String(BASE64EncoderStream.encode(cipher.doFinal( message.getBytes())));
}

public static String decrypt(String message, String keyString)  {

       cipher = Cipher.getInstance(ALGO);
        Key key = generateKey(keyString);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(BASE64DecoderStream.decode(message.getBytes()))); 

}

private static Key generateKey(String keyString) throws NoSuchAlgorithmException {
    byte[] keyBytes = BASE64DecoderStream.decode(keyString.getBytes());
    Key key = new SecretKeySpec(keyBytes, ALGO);
    return key;
}

public static void main(String args[]) {
    byte[] keyValue = new byte[16];
    new SecureRandom().nextBytes(keyValue);
    String key = new String(BASE64EncoderStream.encode(keyValue));
    String message = "test message";
    String enc = encrypt(message, key);
    String dec = decrypt(enc, key);
    System.out.println(dec);
}}

#3


3  

You will need to know the character encoding used, decode the bytes using that and re-encode using the same character encoding. For example:

您需要知道使用的字符编码,使用它解码字节并使用相同的字符编码重新编码。例如:

String str = new String(reply, 0, Charset.forName("UTF-8"));
bytes[] out = str.getBytes(Charset.forName("UTF-8"));
streamToClient.write(bytes, 0, bytes.length);

If not specified, Java using a default character encoding, which is typically UTF-8 (it may even be mandated as such) but HTML will often be something else. I suspect that's your problem.

如果没有指定,Java使用默认字符编码,通常是UTF-8(甚至可能是强制性的),但HTML通常是其他的。我怀疑那是你的问题。

#4


0  

I have a similar problem when reading from a socket and sending to another, but my problem was that I was writing the output with a BufferedOutputStream, when I change this to Output stream it works. I think that there is a problem with the buffer ouput stream.

我从套接字读取并发送到另一个时有类似的问题,但我的问题是我用BufferedOutputStream编写输出,当我将其更改为输出流时,它可以工作。我认为缓冲输出流存在问题。

String mensaje ="what I want to send";
String ip = "192.168.161.165";
int port =  2042;
tpSocket = new Socket(ip, port);
os = tpSocket.getOutputStream();
byte[] myBytes= mensaje.getBytes();
ByteArrayInputStream byarris = new ByteArrayInputStream(myBytes);
int resulta =0;
byte[] bufferOutput= new byte[1];
while((resulta = byarris.read(bufferOutput))!= -1) {
    os.write(bufferOutput);
}