整数ID模糊技术 [英] Integer ID obfuscation techniques
问题描述
我在寻找混淆整数ID的一个简单的和可逆的方法。理想情况下,我会希望得到的混淆是在长度和非顺序最八个字,即1混淆看起来应该像什么混淆为2,依此类推。
I'm looking for an easy and reversible method of obfuscating integer IDs. Ideally, I'd want the resulting obfuscation to be at most eight characters in length and non-sequential, meaning that the obfuscation of "1" should look nothing like the obfuscation for "2" and so on.
这并不意味着以任何方式是安全的,因此这不是一个大问题。此外,我会混淆整数并不大 - 一到10,000之间 - 但我不希望有任何冲突,无论是
This isn't meant to be secure by any means, so this isn't a huge concern. Additionally, the integers I'll be obfuscating aren't large - between one and 10,000 - but I don't want any collisions, either.
没有任何人有任何想法的东西,这将符合这一标准?
Does anybody have any ideas for something that would fit this criteria?
谢谢!结果
克里斯
Thanks!
Chris
推荐答案
我来源于皮尔森哈希将任意输入工作为好,不只是32位整数的想法。我不知道这是完全相同的格雷格的答案,但我不能在他的意思得到。但我知道的是,内存的要求不断在这里。无论多么大的投入,这仍是一个可靠的模糊处理/加密伎俩。
I derived an idea from Pearson hashing which will work for arbitrary inputs as well, not just 32-bit integers. I don't know if this is the exact same as Greg answer, but I couldn't get at what he meant. But what I do know is that the memory requirements are constant here. No matter how big the input, this is still a reliable obfuscation/encryption trick.
有关的记录,这种方法是不散列,并且它不具有冲突。这是混淆一个字节串的完美声音的方法。
For the record, this method is not hashing, and it does not have collisions. It's a perfectly sound method of obfuscating a byte string.
您需要为这个工作什么是秘密密钥 _encryptionTable
这是包含的范围0..255的随机排列。您可以使用此围绕洗牌字节。为了使它真的很难扭转它使用XOR混合字节串了一下。
What you need for this to work is a secret key _encryptionTable
which is a random permutation of the inclusive range 0..255. You use this to shuffle bytes around. To make it really hard to reverse it uses XOR to mix the byte string a bit.
public byte[] Encrypt(byte[] plaintext)
{
if (plaintext == null)
{
throw new ArgumentNullException("plaintext");
}
byte[] ciphertext = new byte[plaintext.Length];
int c = 0;
for (int i = 0; i < plaintext.Length; i++)
{
c = _encryptionTable[plaintext[i] ^ c];
ciphertext[i] = (byte)c;
}
return ciphertext;
}
您可以再使用BitConverter值和字节数组或某些转换之间去基地64或32得到的文本再presentation。基地32编码可如果这是很重要的URL友好。解密是简单地通过计算的逆扭转操作 _encryptionTable
。
You can then use the BitConverter to go between values and byte arrays or some convert to base 64 or 32 to get a textual representation. Base 32 encoding can be URL friendly if that's important. Decrypting is as simply as reversing the operation by computing the inverse of the _encryptionTable
.
public byte[] Decrypt(byte[] ciphertext)
{
if (ciphertext == null)
{
throw new ArgumentNullException("ciphertext");
}
byte[] plaintext = new byte[ciphertext.Length];
int c = 0;
for (int i = 0; i < ciphertext.Length; i++)
{
plaintext[i] = (byte)(_decryptionTable[ciphertext[i]] ^ c);
c = ciphertext[i];
}
return plaintext;
}
您还可以做其他有趣的事情,如果你在一个32位整数工作,只关心数字大于或等于0,这使得它更难猜测的模糊数字。
You can also do other fun things if you're working on a 32-bit integer and only care about the numbers greater than or equal to 0 which makes it harder to guess an obfuscated number.
我也用一个秘密字播种一个伪数生成器并使用它来设置初始置换。这就是为什么我可以简单地知道什么秘诀的话,我用来创建每一件事中获得的价值。
I also use a secret word to seed a pseudo number generator and use that to setup the initial permutation. That's why I can simply get the value by knowing what secret word I used to create every thing.
var mt = new MersenneTwister(secretKey.ToUpperInvariant());
var mr = new byte[256];
for (int i = 0; i < 256; i++)
{
mr[i] = (byte)i;
}
var encryptionTable = mt.NextPermutation(mr);
var decryptionTable = new byte[256];
for (int i = 0; i < 256; i++)
{
decryptionTable[encryptionTable[i]] = (byte)i;
}
this._encryptionTable = encryptionTable;
this._decryptionTable = decryptionTable;
这是有点安全,这里的最大的缺陷就是加密,XOR 0,恰好是异的身份,不改变值( A ^ 0 == A
)。因此,第一加密的字节再present该字节的随机位置。要解决这一点,你可以挑选 C
A的初始值,即不是恒定的,基于只需询问PRNG(种子初始化后)密钥的一个随机字节。这样,它非常更加困难,即使有大样本破解加密,只要你无法观察的输入和输出。
This is somewhat secure, the biggest flaw here is that the encryption, XOR with 0, happens to be the identity of XOR and doesn't change the value (a ^ 0 == a
). Thus the first encrypted byte represent the random position of that byte. To work around this you can pick a initial value for c
, that is not constant, based of the secret key by just asking the PRNG (after init with seed) for a random byte. That way it's immensely more difficult even with a large sample to crack the encryption as long as you can't observe input and output.
这篇关于整数ID模糊技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!