Search code examples
c++11hashencryption-symmetric

How to find the answer to a XOR hash if I have the key & one number


I have a little routine that makes up a cumulative XOR hash. It's as if it is a savings account which gets bigger, cumulatively daily.. but in this sense we're saying the answer is being generated cumulatively and the key is always present.

I have taken a string of chars

pseudo code:

char H[10] = { "ABCDEFGHI", "\0" };

and I used 9 32-bit numeric keys to hash them in XOR encryption.

I did it like this:

for (i;i<10;i++)
    bitset<32> K ^= H[i] ^ NUMKEY[i];

Now this makes it impervious without the calculus plotting I did (see what I did there?) So K is an accumulation of calculus points, which are completely predictable according to calculus.

as far as I know, to undo it, I do

for (i;i<10;i++) {
    X=0;
    X ^= K ^ NUMKEY[i];
}

Is there other math involved? I think I have to take that X and do a little K - X to find the true derivative.

Here's the current routine I have for it. But I'm not getting what I'm looking for.

for_each (std::istreambuf_iterator<char>(in), \
    std::istreambuf_iterator<char>(), \
    [&] (long x) {
    t=s_nop(t,0);
    cred.push_back(t);
    alpha = static_cast<long>(cred[size]);
    delta = static_cast<long>(x);
    lambda ^= (alpha ^ delta);
    size++;
});

for (;i<bn;i++) {
    alpha =  static_cast<unsigned long>(cred[bn-1-i]);
    int m = lambda.to_ulong(), n = alpha.to_ulong();

    long hash1 = abs((m-n-1)%256-1);
    delta = static_cast<unsigned long>(hash1);
    btrace.push_back(hash1);
    cout << hash1 << " ";
}

Please have a safe and Merry Christmas. Thank you in advance!


Solution

  • I think what you might really want is a one time pad. (the snippet is javascript as pseudocode moving in that direction)

    //ignore these top 3 functions (they're just for printing output)
    function appendLine(text, target, space, color) {
      var line = document.createElement("div");
      line.style.border = "1px solid " + color;
      line.style.padding = line.style.margin = space;
      line.style["font-family"] = "monospace";
      line.appendChild(document.createTextNode(text));
      target.appendChild(line);
      return line;
    }
    function makeSection(title) {
      var d = appendLine(title, document.body, "5px", "#dddddd");
      var results = document.createElement("div");
      d.appendChild(results);
      return function(result) {
        appendLine(result, results, "2px", "#eeeeee");
      };
    }
    function toHex(arr) {
      return arr.map(function(n){
        var h = (n >>> 0).toString(16).toUpperCase();
        while(h.length < 8) h = "0" + h;
        return h;
      }).join(",");
    }
    
    //your message
    var H = "ABCDEFGHI".split("").map(function(c){return c.charCodeAt(0)});
    //your secret encoding key
    var NUMKEY = Array.apply(null, Array(9)).map(function(){return Math.random() * Math.pow(2, 32)});
    
    //what you're doing
    (function() {
      var section = makeSection("what you're doing:");
      section("ABCDEFGHI as array of 32bit numbers: " + toHex(H));
      section("NUMKEY: " + toHex(NUMKEY));
      var K = 0;
      for (var i = 0; i < 10; i++) {
        K ^= H[i] ^ NUMKEY[i];
      }
      section("K: " + toHex([K]));
      var X = 0;
      for (var i = 0; i < 10; i++) {
        X ^= K ^ NUMKEY[i];
      }
      section("X: " + toHex([X]));
    })();
    
    //what you're trying to do
    (function() {
      var section = makeSection("what you're trying to do:");
      section("ABCDEFGHI as array of 32bit numbers: " + toHex(H));
      section("NUMKEY: " + toHex(NUMKEY));
      var Hs_XORd = 0;
      for (var i = 0; i < 10; i++) {
        Hs_XORd ^= H[i];
      }
      section("H's XOR'd together: " + toHex([Hs_XORd]));
      var NUMKEYs_XORd = 0;
      for (var i = 0; i < H.length; i++) {
        NUMKEYs_XORd ^= NUMKEY[i];
      }
      section("NUMKEY's XOR'd together: " + toHex([NUMKEYs_XORd]));
      var K = NUMKEYs_XORd ^ Hs_XORd;
      section("K: " + toHex([K]));
      var X = K ^ NUMKEYs_XORd;
      section("X (should be the same as H's XOR'd together): " + toHex([X]));
    })();
    
    
    //what I think you mean to be doing (one time pad)
    (function() {
      var section = makeSection("what I think you mean to be doing (one time pad):");
      section("ABCDEFGHI as array of 32bit numbers: " + toHex(H));
      section("NUMKEY: " + toHex(NUMKEY));
      var K = [];
      for (var i = 0; i < H.length; i++) {
        K[i] = H[i] ^ NUMKEY[i];
      }
      section("K (encoded message using NUMKEY as one time pad): " + toHex(K));
      var X = [];
      for (var i = 0; i < K.length; i++) {
        X[i] = K[i] ^ NUMKEY[i];
      }
      section("X (decoded message, should be the same as ABCDEFGHI): " + toHex(X));
    })();