加密逗号分隔的 ID 列表是否会使其更小以适合 cookie?
假设我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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?
加密本身不会压缩数据。不过你可以考虑压缩它。请记住,不同的值会压缩不同的量,因此,如果您接近可压缩的限制,您可能会发现它有时不合适。如果您的空间不足,您可能需要寻找不同的方法。
请注意,加密后您无法对其进行有意义的压缩。加密将其变成看似随机的数据,并且不适合压缩。
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.
加密不一定会压缩(通常不会),并且您所做的任何压缩都可以在不加密的情况下完成。
我可以建议您查看协议缓冲区,以找到一种有效存储此类简单数据的简单方法。
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.
如果您只对压缩数据感兴趣,您可以使用 .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
您可以使用 VInt/Vlong 的实现对每个数字尝试进行小压缩。压缩会根据您的数据而有所不同。内径越高,压缩率越低。
这是使用 Vint 的实现:
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: