简体   繁体   English

在字符串之间对UTF-8字节数组进行编码和解码

[英]Encoding and decoding UTF-8 byte arrays from and to strings

I'm working on a cross-platform encryption system. 我正在开发一个跨平台的加密系统。 One of the requirements is to easily encrypt and decrypt strings in out application code. 要求之一就是轻松地对应用程序代码中的字符串进行加密和解密。

The encryption class works flawlessly, but I'm having trouble with string encoding on the java side. 加密类可以完美地工作,但是我在Java方面无法进行字符串编码。

Currently, I have the following static methods: 当前,我有以下静态方法:

public static String encrypt(String key, String data)
{
    byte[] decoded_key;
    byte[] decoded_data;
    try
    {
        decoded_key = key.getBytes("UTF-8");
        decoded_data = data.getBytes("UTF-8");
    }
    catch (Exception e)
    {
        //Not Supposed to happen.
        throw new RuntimeException();
    }

    if(decoded_key.length != 16) 
        throw new IllegalArgumentException("Key length must be of 16 bytes. Given is " + decoded_key.length + ".");

    try
    {
        return(IOUtils.toString(encrypt(decoded_key, decoded_data), "UTF-8"));
    }
    catch (Exception e)
    {
        //Not Supposed to happen.
        throw new RuntimeException();
    }
}

public static String decrypt(String key, String data)
{
    byte[] decoded_key;
    byte[] decoded_data;
    try
    {
        decoded_key = key.getBytes("UTF-8");
        decoded_data = data.getBytes("UTF-8");
    }
    catch (Exception e)
    {
        //Not Supposed to happen.
        throw new RuntimeException();
    }

    if(decoded_key.length != 16) 
        throw new IllegalArgumentException("Key length must be of 16 bytes. Given is " + decoded_key.length + ".");

    try
    {
        return(IOUtils.toString(decrypt(decoded_key, decoded_data), "UTF-8"));
    }
    catch (Exception e)
    {
        //Not Supposed to happen.
        throw new RuntimeException();
    }
}

My unit tests are failing when decrypting. 解密时我的单元测试失败。 I ran a test where I compared a byte array of encoded UTF-8 data encoded_data with IOUtils.toString( encoded_data , "UTF-8").getBytes("UTF-8") and for some reason they turned out to be different arrays altogether. 我跑了测试,其中我比较了编码的UTF-8数据的字节数组encoded_dataIOUtils.toString( encoded_data , "UTF-8").getBytes("UTF-8")由于某种原因,他们原来是不同的阵列共。 No wonder my decryption algorithm is failing. 难怪我的解密算法失败了。

What is the proper procedure to convert from a java string to a UTF-8 byte array and back to a java string? 从Java字符串转换为UTF-8字节数组然后再返回Java字符串的正确过程是什么?

the problem is that you are converting your encrypted data to a String. 问题是您正在将加密的数据转换为字符串。 encrypted data is binary, not String data. 加密的数据是二进制数据,而不是字符串数据。 UTF-8 is a charset with a specific encoding format. UTF-8是具有特定编码格式的字符集。 arbitrary binary data is not valid UTF-8 data. 任意二进制数据不是有效的UTF-8数据。 when you convert the encrypted data into a String, the "invalid" characters are most likely getting replaced with the ? 当您将加密数据转换为字符串时,“无效”字符很可能会被替换为? invalid char. 无效的字符。

If you want to convert arbitrary binary data (aka encrypted data) into a String, you need to use some binary->text conversion like Base64. 如果要将任意二进制数据(也称为加密数据)转换为字符串,则需要使用一些二进制->文本转换,例如Base64。

I would try out checking first that the output of your encrypt method matches the one you are expecting with a unit test. 我将首先尝试检查您的加密方法的输出是否与单元测试所期望的相匹配。

Also it's a good idea to use Base64 after the encryption so you can convert it to a string. 另外,在加密后使用Base64是一个好主意,这样您就可以将其转换为字符串。

Another common issue is converting int to bytes as if they were unsigned ints. 另一个常见的问题是将int转换为字节,就好像它们是无符号int一样。 Bytes range is -128 to 127. 字节范围是-128到127。

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

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