0

我想将字符串压缩/转换为新字符串。

即:输入字符串:

USERNAME/REGISTERID

压缩后的输出字符串:

<some-string-in-UTF8-format>

解压后输出字符串:

USERNAME/REGISTERID

这种转换有一些压缩或哈希方法吗?

我更喜欢使用 Java 或具有基本流程步骤的算法的一些解决方案。

我已经阅读并尝试使用霍夫曼转换,但压缩输出由字节出站 UTF-8 字符集组成。

4

5 回答 5

2

您可以使用ZipOutputStream

    ByteArrayOutputStream result = new ByteArrayOutputStream();
    new ZipOutputStream(result).write("myString".getBytes());
    byte[] bytes = result.toByteArray();

你只需要找出正确的字符串编码。这种情况是用Base64 表示来完成的。

于 2009-09-29T16:12:14.707 回答
1

看看Base64commons-codec等。

Commons-code 提供了一个非常简单的Base64 类来使用。

您不能使用散列函数,因为散列函数通常是单向的:即给定一个 MD5 或 SHA1 散列,您应该无法对其进行解码以找出源消息是什么。

于 2009-09-29T16:15:08.870 回答
1

请参阅iconvmb_convert_encoding. 对于编码,也许考虑base64_encode.

于 2009-09-29T16:14:30.833 回答
0

It looks like someone is asking you to obfuscate username/password combinations. This is probably not a good idea, since it suggests security where there is none. You might as well implement a ROT13 encryption for this and use double ROT13 to decrypt.

于 2009-09-29T20:02:21.497 回答
0

如果您的标识符有数据库ID,正如您的名字所暗示的那样,为什么不使用这个数字作为编码?(如果你愿意,可以把它作为字符串)。

您不应该希望使用压缩算法获得更好的压缩,因为它们都需要一些标头,并且标头大小本身可能比您的输入字符串长。

于 2009-09-29T16:12:58.280 回答