string Java 字符串到字节数组回字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1587280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:33:02  来源:igfitidea点击:

Java string to bytearray back to string

stringbytearrayxor

提问by NewJavaProgrammer

I have a client and server java application that needs have encrypted text passing through each other. I am using an XOR encryption the encrypt the text that I want.

我有一个客户端和服务器 java 应用程序,需要加密文本相互传递。我正在使用 XOR 加密来加密我想要的文本。

The problem is that readline() doesn't accept string that has been XORed and will only if accepted if it is in bytes.

问题是 readline() 不接受经过异或运算的字符串,并且只有在以字节为单位接受时才会接受。

So I've converted my plaintext (string) into a byte array on the client side, and tried to convert back to a string on the server side.

所以我在客户端将我的纯文本(字符串)转换为字节数组,并尝试在服务器端转换回字符串。

Sadly, the result I am looking for is still jibberish and not the plaintext that I seeked.

可悲的是,我正在寻找的结果仍然是胡言乱语,而不是我寻求的明文。

Does anyone know how to make bytearrays change back to the original string? Or is there a better way to send through an XOR encrypted text through the readline() function?

有谁知道如何让字节数组变回原始字符串?或者有没有更好的方法通过 readline() 函数发送 XOR 加密文本?

回答by Jon Skeet

After you've applied something like XOR, you end up with arbitrary binary data - notan encoded string.

在应用了 XOR 之类的东西之后,你最终会得到任意的二进制数据——而不是编码字符串。

The conventional safe way to convert arbitrary binary into text is to use base64- don'ttry to just create a new string from it. So your process would be something like this:

将任意二进制文件转换为文本的传统安全方法是使用base64-不要试图仅从它创建一个新字符串。所以你的过程会是这样的:

  • Start with plain text as a string.
  • Encode the plain text using UTF-8, UTF-16 or something similar. Don'tuse the platform default encoding, or anything restrictive like ASCII. You now have a byte array.
  • Apply your encryption. (XOR is pretty weak, but let's leave that to one side.) You still have a byte array.
  • Apply base64 encoding to get a string.
  • 从纯文本作为字符串开始。
  • 使用 UTF-8、UTF-16 或类似的东西对纯文本进行编码。不要使用平台默认编码或任何限制性的东西,比如 ASCII。您现在有一个字节数组。
  • 应用您的加密。(XOR 非常弱,但让我们把它放在一边。)您仍然有一个字节数组。
  • 应用 base64 编码以获取字符串。

Then when you need to decrypt...

那么当你需要解密...

  • Apply base64 decodingto convert your string into a byte array.
  • Apply your binary decryption process (e.g. XOR again). You still have a byte array.
  • Nowdecode that byte array into a string, e.g. with new String(data, utf8Charset)to get back the original string.
  • 应用 base64解码将您的字符串转换为字节数组。
  • 应用您的二进制解密过程(例如再次 XOR)。你仍然有一个字节数组。
  • 现在将该字节数组解码为字符串,例如使用new String(data, utf8Charset)以取回原始字符串。

There are various Java base64 libraries available, such as this classin Apache Commons Codec. (You'd want the encodeToString(byte[])and decode(String)methods.)

有可用的各种Java的base64库,比如这个类的Apache共享编解码器。(你会想要encodeToString(byte[])decode(String)方法。)

回答by Ibrahim

answer from http://www.mkyong.com/java/how-do-convert-byte-array-to-string-in-java/

来自http://www.mkyong.com/java/how-do-convert-byte-array-to-string-in-java/ 的回答

            String example = "This is an example";
               //Convert String to byte[] using .getBytes() function
            byte[] bytes = example.getBytes();
               //Convert byte[] to String using new String(byte[])      
            String s = new String(bytes);

回答by malaverdiere

1st of all, don't call XORing encryption. Seriously, the CipherOutputStream and CipherInputStream are there for you if you need to protect your data.

首先,不要调用异或加密。说真的,如果您需要保护数据,CipherOutputStream 和 CipherInputStream 可以为您提供帮助。

Thanks for stream chaining, you can have DataOutputStream -> CipherOutputStream -> ByteArrayOutputStream. This will get you properly encrypted bytes. Use the reverse APIs on the other end and you'll get your string back.

感谢流链接,您可以拥有 DataOutputStream -> CipherOutputStream -> ByteArrayOutputStream。这将使您正确加密字节。在另一端使用反向 API,您将恢复您的字符串。