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

Bev*_*vor 50 java string encryption bytearray

关于这个主题有很多问题,同样的解决方案,但这对我不起作用.我有一个加密的简单测试.加密/解密本身是有效的(只要我使用字节数组本身而不是字符串处理此测试).问题是不希望将它作为字节数组处理,而是作为String处理,但是当我将字节数组编码为字符串并返回时,生成的字节数组与原始字节数组不同,因此解密不再起作用.我在相应的字符串方法中尝试了以下参数:UTF-8,UTF8,UTF-16,UTF8.他们都没有工作.生成的字节数组与原始数组不同.任何想法为什么会这样?

加密:

public class NewEncrypter
{
    private String algorithm = "DESede";
    private Key key = null;
    private Cipher cipher = null;

    public NewEncrypter() throws NoSuchAlgorithmException, NoSuchPaddingException
    {
         key = KeyGenerator.getInstance(algorithm).generateKey();
         cipher = Cipher.getInstance(algorithm);
    }

    public byte[] encrypt(String input) throws Exception
    {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] inputBytes = input.getBytes("UTF-16");

        return cipher.doFinal(inputBytes);
    }

    public String decrypt(byte[] encryptionBytes) throws Exception
    {
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
        String recovered = new String(recoveredBytes, "UTF-16");

        return recovered;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试的测试:

public class NewEncrypterTest
{
    @Test
    public void canEncryptAndDecrypt() throws Exception
    {
        String toEncrypt = "FOOBAR";

        NewEncrypter encrypter = new NewEncrypter();

        byte[] encryptedByteArray = encrypter.encrypt(toEncrypt);
        System.out.println("encryptedByteArray:" + encryptedByteArray);

        String decoded = new String(encryptedByteArray, "UTF-16");
        System.out.println("decoded:" + decoded);

        byte[] encoded = decoded.getBytes("UTF-16");
        System.out.println("encoded:" + encoded);

        String decryptedText = encrypter.decrypt(encoded); //Exception here
        System.out.println("decryptedText:" + decryptedText);

        assertEquals(toEncrypt, decryptedText);
    }
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*oni 100

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

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

    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);
Run Code Online (Sandbox Code Playgroud)

其他不丢失数据的常见编码是十六进制base64,但遗憾的是你需要一个帮助程序库.标准API不为它们定义类.

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

  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中的任何字符.


Ale*_*lic 21

如果你String有一些非典型的charcaters,如接受的解决方案,将无法使用š, ž, ?, ?, ?, ?.

以下代码很适合我.

byte[] myBytes = Something.getMyBytes();
String encodedString = Base64.encodeToString(bytes, Base64.NO_WRAP);
byte[] decodedBytes = Base64.decode(encodedString, Base64.NO_WRAP);
Run Code Online (Sandbox Code Playgroud)

  • 我在使用`android.util.Base64`.如果您不想包含`ApacheCommons`,我想您可以随时复制项目中的文件.以下是来源:https://github.com/android/platform_frameworks_base/blob/master/core/java/android/util/Base64.java (3认同)

Bev*_*vor 5

现在,我发现了另一种解决方案......

    public class NewEncrypterTest
    {
        @Test
        public void canEncryptAndDecrypt() throws Exception
        {
            String toEncrypt = "FOOBAR";

            NewEncrypter encrypter = new NewEncrypter();

            byte[] encryptedByteArray = encrypter.encrypt(toEncrypt);
            String encoded = String.valueOf(Hex.encodeHex(encryptedByteArray));

            byte[] byteArrayToDecrypt = Hex.decodeHex(encoded.toCharArray());
            String decryptedText = encrypter.decrypt(byteArrayToDecrypt); 

            System.out.println("decryptedText:" + decryptedText);

            assertEquals(toEncrypt, decryptedText);
        }
    }
Run Code Online (Sandbox Code Playgroud)