Bar*_*cki -1 java string inputstream outputstream
我正在尝试进行此类转换,但我遇到了一些问题.
假设我有一个以下字符串:
String in = "1234567890123456";
Run Code Online (Sandbox Code Playgroud)
然后我将它转换为ByteArrayInputStream,如下所示:
ByteArrayInputStream bais = new ByteArrayInputStream(in.getBytes("UTF-8"));
Run Code Online (Sandbox Code Playgroud)
我也有:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Run Code Online (Sandbox Code Playgroud)
然后我加密:
ch.encrypt(bais, baos);
Run Code Online (Sandbox Code Playgroud)
所以现在我的"输出"了baos.当我这样做的时候:
byte[] b2 = baos.toByteArray();
int[] i2 = toUnsignedIntArray(b2);
writeIntegersAsHex(i2);
Run Code Online (Sandbox Code Playgroud)
哪里(我知道它不是最优雅的方式,但它仅用于测试):
public static void writeIntegersAsHex(int[] integers) {
int height = integers.length;
for (int i = 0; i < height; i++) {
System.out.print(Integer.toHexString(integers[i]) + ",");
}
System.out.println("");
}
Run Code Online (Sandbox Code Playgroud)
我得到这样的输出:
d1,68,a0,46,32,37,25,64,67,71,17,df,ee,ef,2,12,
Run Code Online (Sandbox Code Playgroud)
并且该输出是正确的,因为当我处理包含相同字符串的文件时,in该输出是相同的.但我无法从中获得正确的字符串baos.
请不要问我为什么这样做,因为这不是我的电话.我是学生,这是一个例外.
算法(顺便说一句,它是aes128)工作正常,文件,但我不能得到字符串输入流和输出流到字符串工作正常.
但我无法从baos中获得正确的字符串.
此时,您的输出只是任意二进制数据.它不是编码文本 - 它只是一堆比特.
要将其转换为合理的字符串,可以将其转换回原始字节,您应该使用hex或base64.有一个公共领域base64库,根据我的经验,或许多其他替代品(对于base64和hex)都很好.
| 归档时间: |
|
| 查看次数: |
8799 次 |
| 最近记录: |