繁体   English   中英

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

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

我正在开发一个跨平台的加密系统。 要求之一就是轻松地对应用程序代码中的字符串进行加密和解密。

加密类可以完美地工作,但是我在Java方面无法进行字符串编码。

当前,我有以下静态方法:

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();
    }
}

解密时我的单元测试失败。 我跑了测试,其中我比较了编码的UTF-8数据的字节数组encoded_dataIOUtils.toString( encoded_data , "UTF-8").getBytes("UTF-8")由于某种原因,他们原来是不同的阵列共。 难怪我的解密算法失败了。

从Java字符串转换为UTF-8字节数组然后再返回Java字符串的正确过程是什么?

问题是您正在将加密的数据转换为字符串。 加密的数据是二进制数据,而不是字符串数据。 UTF-8是具有特定编码格式的字符集。 任意二进制数据不是有效的UTF-8数据。 当您将加密数据转换为字符串时,“无效”字符很可能会被替换为? 无效的字符。

如果要将任意二进制数据(也称为加密数据)转换为字符串,则需要使用一些二进制->文本转换,例如Base64。

我将首先尝试检查您的加密方法的输出是否与单元测试所期望的相匹配。

另外,在加密后使用Base64是一个好主意,这样您就可以将其转换为字符串。

另一个常见的问题是将int转换为字节,就好像它们是无符号int一样。 字节范围是-128到127。

暂无
暂无

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

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