且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字节数组转换为字符串并返回到字节数组的问题

更新时间:2021-09-25 05:36:07

将加密数据存储在字符串中不是一个好主意,因为它们是用于人类可读的文本,而不是任意的二进制数据。对于二进制数据,***使用 byte []

It is not a good idea to store encrypted data in Strings because they are for human-readable text, not for arbitrary binary data. For binary data it's best to use byte[].

但是,如果必须这样做,您应该使用字节和字符之间具有 1对1映射的编码,也就是说,每个字节序列都可以映射到唯一的字符序列并返回。一个这样的编码是 ISO-8859-1 ,即:

However, if you must do it you should use an encoding that has a 1-to-1 mapping between bytes and characters, that is, where every byte sequence can be mapped to a unique sequence of characters, and back. One such encoding is ISO-8859-1, that is:

    String decoded = new String(encryptedByteArray, "ISO-8859-1");
    System.out.println("decoded:" + decoded);

    byte[] encoded = decoded.getBytes("ISO-8859-1"); 
    System.out.println("encoded:" + java.util.Arrays.toString(encoded));

    String decryptedText = encrypter.decrypt(encoded);

其他不丢失数据的常见编码是十六进制 base64 ,可惜你需要一个帮助程序库。标准API没有为他们定义类。

Other common encodings that don't lose data are hexadecimal and base64, but sadly you need a helper library for them. The standard API doesn't define classes for them.

使用UTF-16,程序将失败,原因有二:

With UTF-16 the program would fail for two reasons:


  1. String.getBytes(UTF-16)向输出添加一个字节顺序标记字符,以标识字节顺序。您应该使用UTF-16LE或UTF-16BE来实现此功能。

  2. 不是所有字节序列都可以映射到UTF-16中的字符。首先,以UTF-16编码的文本必须具有偶数个字节。其次,UTF-16具有超越U + FFFF的unicode字符编码的机制。这意味着例如有4个字节的序列映射到只有一个unicode字符。为了可能,4的前2个字节不对UTF-16中的任何字符进行编码。