Symmetric Bijective Algorithm for Integers

后端 未结 11 866
甜味超标
甜味超标 2020-12-01 03:33

I need an algorithm that can do a one-to-one mapping (ie. no collision) of a 32-bit signed integer onto another 32-bit signed integer.

My real concern is enough entr

11条回答
  •  死守一世寂寞
    2020-12-01 04:06

    If you don't want to use proper cryptographic algorithms (perhaps for performance and complexity reasons) you can instead use a simpler cipher like the Vigenère cipher. This cipher was actually described as le chiffre indéchiffrable (French for 'the unbreakable cipher').

    Here is a simple C# implementation that shifts values based on a corresponding key value:

    void Main()
    {
      var clearText = Enumerable.Range(0, 10);
      var key = new[] { 10, 20, Int32.MaxValue };
      var cipherText = Encode(clearText, key);
      var clearText2 = Decode(cipherText, key);
    }
    
    IEnumerable Encode(IEnumerable clearText, IList key) {
      return clearText.Select((i, n) => unchecked(i + key[n%key.Count]));
    }
    
    IEnumerable Decode(IEnumerable cipherText, IList key) {
      return cipherText.Select((i, n) => unchecked(i - key[n%key.Count]));
    }
    

    This algorithm does not create a big shift in the output when the input is changed slightly. However, you can use another bijective operation instead of addition to achieve that.

提交回复
热议问题