加密逗号分隔的 ID 列表是否会使其更小以适合 cookie?

发布于 2024-08-14 05:47:03 字数 172 浏览 5 评论 0原文

假设我想将 ID 存储在 cookie 中:

123,1232,3443,2343,2344422,2342

看到 cookie 有 4kb 限制(或其他),加密该值是否会以某种方式允许更多存储?

如果是这样,哪种加密最好? (并不是真的担心安全,只是想用更少的空间存储更多内容)

say I want to store ID's in a cookie:

123,1232,3443,2343,2344422,2342

seeing that a cookie has a 4kb limit (or whatever), would encrypting the value allow for more storage somehow?

if so, which encryption would be best? (not really worried about security, just want to store more with less footprint)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秋意浓 2024-08-21 05:47:03

4k字节可以存储819个四位数。您确定您真的需要存储更多吗?

在 cookie 中存储一个密钥,通过简单的数据库查询即可找到超长的数字序列,这不是更容易吗?

With 4k bytes, you can store 819 four digit numbers. Are you sure you really do need to store more?

Wouldn't it be maybe then easier to just store in the cookie a key that would bring you to your overlong sequence of numbers with a simple DB query?

千纸鹤带着心事 2024-08-21 05:47:03

加密本身不会压缩数据。不过你可以考虑压缩它。请记住,不同的值会压缩不同的量,因此,如果您接近可压缩的限制,您可能会发现它有时不合适。如果您的空间不足,您可能需要寻找不同的方法。

请注意,加密后您无法对其进行有意义的压缩。加密将其变成看似随机的数据,并且不适合压缩。

Encryption itself isn't going to compress the data. You could look at compressing it though. Keep in mind that different values will compress by different amounts, so if you get near the limits of what can be compressed, you may find that it sometimes doesn't fit. If you're running out of space you may want to look for a different approach instead.

Note that you can't meaningfully compress it after encrypting it. The encryption changes it into seemingly random data and that does not lend itself to compression.

葬花如无物 2024-08-21 05:47:03

加密不一定会压缩(通常不会),并且您所做的任何压缩都可以在不加密的情况下完成。

我可以建议您查看协议缓冲区,以找到一种有效存储此类简单数据的简单方法。

Encryption will not necessarily compress (it usually won't) , and any compression you do can be done without encryption.

Can I suggest you look at Protocol Buffers for an easy way to store simple data like this efficiently.

勿忘心安 2024-08-21 05:47:03

如果您只对压缩数据感兴趣,您可以使用 .Net 中的流压缩类。 http://msdn.microsoft.com/en-我们/library/system.io.compression.gzipstream.aspx

If you are only interested in compressing the data you could just use the stream compression classes in .Net. http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

只是在用心讲痛 2024-08-21 05:47:03

您可以使用 VInt/Vlong 的实现对每个数字尝试进行小压缩。压缩会根据您的数据而有所不同。内径越高,压缩率越低。

这是使用 Vint 的实现:

class CookieConverter
{
    private readonly Encoding _enc = Encoding.GetEncoding("iso-8859-1");
    public string PackCookieString(List<int> numbers)
    {
        MemoryStream memoryStream = new MemoryStream();
        foreach (int number in numbers)
        {
            byte[] bytes = GetVIntBytes(number);
            memoryStream.Write(bytes, 0, bytes.Length);
        }
        return _enc.GetString(memoryStream.ToArray());
    }

    public List<int> UnpackCookieString(string cookie)
    {
        byte[] bytes = _enc.GetBytes(cookie);
        List<int> numbers = new List<int>();
        int startIndex = 0;
        while (startIndex < bytes.Length)
        {
            numbers.Add(GetVInt(bytes, ref startIndex));
        }
        return numbers;
    }

    public byte[] GetVIntBytes(int value)
    {
        byte[] buffer = new byte[5];
        byte length = 0;
        while ((value & ~0x7F) != 0)
        {
            buffer[length] = (byte) ((value & 0x7f) | 0x80);
            value = value >> 7;
            length++;
        }
        buffer[length] = (byte) value;
        byte[] result = new byte[length+1];
        Array.Copy(buffer, 0, result, 0, result.Length);
        return result;
    }

    public int GetVInt(byte[] buffer, ref int startIndex)
    {
        byte b = buffer[startIndex];
        startIndex++;
        int i = b & 0x7F;
        for (int shift = 7; (b & 0x80) != 0; shift += 7)
        {
            b = buffer[startIndex];
            i |= (b & 0x7F) << shift;
            startIndex++;
        }
        return i;
    }

You could try small compression on each number using an implementation of VInt/Vlong. Compression will vary on your data. Higher id's will yield lower compression.

Here's an implementation using vint:

class CookieConverter
{
    private readonly Encoding _enc = Encoding.GetEncoding("iso-8859-1");
    public string PackCookieString(List<int> numbers)
    {
        MemoryStream memoryStream = new MemoryStream();
        foreach (int number in numbers)
        {
            byte[] bytes = GetVIntBytes(number);
            memoryStream.Write(bytes, 0, bytes.Length);
        }
        return _enc.GetString(memoryStream.ToArray());
    }

    public List<int> UnpackCookieString(string cookie)
    {
        byte[] bytes = _enc.GetBytes(cookie);
        List<int> numbers = new List<int>();
        int startIndex = 0;
        while (startIndex < bytes.Length)
        {
            numbers.Add(GetVInt(bytes, ref startIndex));
        }
        return numbers;
    }

    public byte[] GetVIntBytes(int value)
    {
        byte[] buffer = new byte[5];
        byte length = 0;
        while ((value & ~0x7F) != 0)
        {
            buffer[length] = (byte) ((value & 0x7f) | 0x80);
            value = value >> 7;
            length++;
        }
        buffer[length] = (byte) value;
        byte[] result = new byte[length+1];
        Array.Copy(buffer, 0, result, 0, result.Length);
        return result;
    }

    public int GetVInt(byte[] buffer, ref int startIndex)
    {
        byte b = buffer[startIndex];
        startIndex++;
        int i = b & 0x7F;
        for (int shift = 7; (b & 0x80) != 0; shift += 7)
        {
            b = buffer[startIndex];
            i |= (b & 0x7F) << shift;
            startIndex++;
        }
        return i;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文