如何COM preSS一个随机字符串?字符串、COM、preSS

2023-09-04 05:47:50 作者:独恋寒冬雪

我正在上使用的基于RSA非对称算法的一个加密的应用程序。 它生成一个密钥对,用户必须保持。 由于密钥对是长的随机字符串,我希望创建一个让我COM preSS产生基于图案长的随机字符串(密钥对)的函数。

I'm working on a encryptor application that works based on RSA Asymmetric Algorithm. It generates a key-pair and the user have to keep it. As key-pairs are long random strings, I want to create a function that let me compress generated long random strings (key-pairs) based on a pattern.

(比如函数获取包含100个字符的字符串,并返回一个包含30个字符的字符串)的

因此​​,当用户输入的COM pressed串我可以再生根据该图案我玉米pressed用密钥对

So when the user enter the compressed string I can regenerate the key-pairs based on the pattern I compressed with.

但是,一个人告诉我,这是不可能的COM preSS随机的东西,因为它们是随机的!

But a person told me that it is impossible to compress random things because they are Random!

你有什么想法? 有没有办法做到这一点?

What is your idea ? Is there any way to do this ?

感谢

推荐答案

这是不可能的COM preSS(几乎所有)的随机数据。学习一些关于信息论,熵,如何COM pression工作,并鸽巢原理将更为清楚。

It's impossible to compress (nearly any) random data. Learning a bit about information theory, entropy, how compression works, and the pigeonhole principle will make this abundantly clear.

一个例外是,如果以随机字符串,你的意思是,随机数据重新psented在COM pressible形式$ P $,像十六进制。在这种情况下,你可以COM preSS的字符串或(更好的选择)只需连接$ C C字节为基础64 $,而不是把它缩短。例如,

One exception to this rule is if by "random string", you mean, "random data represented in a compressible form, like hexadecimal". In this sort of scenario, you could compress the string or (the better option) simply encode the bytes as base 64 instead to make it shorter. E.g.

// base 16, 50 random bytes (length 100)
be01a140ac0e6f560b1f0e4a9e5ab00ef73397a1fe25c7ea0026b47c213c863f88256a0c2b545463116276583401598a0c36
// base 64, same 50 random bytes (length 68)
vgGhQKwOb1YLHw5KnlqwDvczl6H+JcfqACa0fCE8hj+IJWoMK1RUYxFidlg0AVmKDDY=

您可能代替给用户一个更短的散列值或指纹(例如最近x字节)。然后,通过存储的全键和散列的地方,你可以给他们关键的时候,他们给你的哈希值。你必须有这个哈希足够长的安全不会受到损害。根据应用程序的,这可能击败目的,因为散列将必须是只要键,或者它可能不会成为一个问题。

You might instead give the user a shorter hash or fingerprint of the value (e.g. the last x bytes). Then by storing the full key and hash somewhere, you could give them the key when they give you the hash. You'd have to have this hash be long enough that security is not compromised. Depending on your application, this might defeat the purpose because the hash would have to be as long as the key, or it might not be a problem.